Can't close a Netty Client

B.L. Zeebub roger.varley at googlemail.com
Wed Jul 20 04:42:51 EDT 2011


Hi

Below is the code which starts my NettyClient. The class NettyClientService
is created and started by an external application. When the external
application terminates, it calls NettyClientService.shutdown() which hangs.

As the code is written, it's hanging on the
channel.close().awaitUninterruptibly(). If I change that to simple
channel.close(), then the class hangs on
bootstrap.releaseExternalResources();

Any suggestions on what's wrong, or on how I can further diagnose the
problem?

Regards
Roger



package com.blackbox.x.comms.client;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;

import com.blackbox.x.comms.common.CommandExecutor;



public class NettyClientService implements Runnable {

	private static final Logger logger = Logger
	.getLogger(NettyClientService.class.getName());
	
	private Channel channel;

	private String host;

	private CommandExecutor executor;
	
	private ClientBootstrap bootstrap;
	
	public NettyClientService(String host, CommandExecutor executor) {
		this.host = host;
		this.executor = executor;
	}
	
	public void run() {
		
		logger.info("Trying to connect to host " + host); 
				
		bootstrap = new ClientBootstrap(new
NioClientSocketChannelFactory(Executors
				.newCachedThreadPool(),Executors.newCachedThreadPool()));
		
		// Set up the event pipeline factory.
		Timer timer = new HashedWheelTimer();
		bootstrap.setPipelineFactory(new ClientPipelineFactory(executor,timer));
		bootstrap.setOption("keepAlive", true);
		
		// Make a new connection.
		ChannelFuture future = bootstrap.connect(new InetSocketAddress(
				host, 5956));

		channel = future.awaitUninterruptibly().getChannel();
		if (!channel.isConnected()) {
			
			logger.error("Failed to connect to " + host);
			executor.close();
			bootstrap.releaseExternalResources();
			return;
			}
		
		logger.info("Connected to " + host);
	}

	public void shutdown() {
		
		
		if (channel != null) {
			channel.close().awaitUninterruptibly();
			}
		if (bootstrap != null) {
			bootstrap.releaseExternalResources();
			}
		
	}

	
}

--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Can-t-close-a-Netty-Client-tp6601815p6601815.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list