Polished all examples

This commit is contained in:
Trustin Lee 2009-07-23 09:05:53 +00:00
parent 6321bb406f
commit 3920158bc0
24 changed files with 179 additions and 250 deletions

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
@ -53,7 +52,6 @@ public class DiscardClient {
String host = args[0]; String host = args[0];
int port = Integer.parseInt(args[1]); int port = Integer.parseInt(args[1]);
int firstMessageSize; int firstMessageSize;
if (args.length == 3) { if (args.length == 3) {
firstMessageSize = Integer.parseInt(args[2]); firstMessageSize = Integer.parseInt(args[2]);
} else { } else {
@ -61,19 +59,14 @@ public class DiscardClient {
} }
// Configure the client. // Configure the client.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory); // Set up the default event pipeline.
DiscardClientHandler handler = new DiscardClientHandler(firstMessageSize); DiscardClientHandler handler = new DiscardClientHandler(firstMessageSize);
//bootstrap.getPipeline().addLast("executor", new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(16, 0, 0)));
bootstrap.getPipeline().addLast("handler", handler); bootstrap.getPipeline().addLast("handler", handler);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
//bootstrap.setOption("bufferFactory", DirectChannelBufferFactory.getInstance());
// Start the connection attempt. // Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
@ -82,6 +75,6 @@ public class DiscardClient {
future.getChannel().getCloseFuture().awaitUninterruptibly(); future.getChannel().getCloseFuture().awaitUninterruptibly();
// Shut down thread pools to exit. // Shut down thread pools to exit.
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
} }
} }

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
@ -41,18 +40,14 @@ public class DiscardServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory); // Set up the default event pipeline.
DiscardServerHandler handler = new DiscardServerHandler(); DiscardServerHandler handler = new DiscardServerHandler();
bootstrap.getPipeline().addLast("handler", handler); bootstrap.getPipeline().addLast("handler", handler);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
//bootstrap.setOption("child.bufferFactory", DirectChannelBufferFactory.getInstance());
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
@ -55,7 +54,6 @@ public class EchoClient {
String host = args[0]; String host = args[0];
int port = Integer.parseInt(args[1]); int port = Integer.parseInt(args[1]);
int firstMessageSize; int firstMessageSize;
if (args.length == 3) { if (args.length == 3) {
firstMessageSize = Integer.parseInt(args[2]); firstMessageSize = Integer.parseInt(args[2]);
} else { } else {
@ -63,17 +61,14 @@ public class EchoClient {
} }
// Configure the client. // Configure the client.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory); // Set up the default event pipeline.
EchoHandler handler = new EchoHandler(firstMessageSize); EchoHandler handler = new EchoHandler(firstMessageSize);
bootstrap.getPipeline().addLast("handler", handler); bootstrap.getPipeline().addLast("handler", handler);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
// Start the connection attempt. // Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
@ -82,6 +77,6 @@ public class EchoClient {
future.getChannel().getCloseFuture().awaitUninterruptibly(); future.getChannel().getCloseFuture().awaitUninterruptibly();
// Shut down thread pools to exit. // Shut down thread pools to exit.
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
} }
} }

View File

@ -83,7 +83,7 @@ public class EchoHandler extends SimpleChannelUpstreamHandler {
ChannelHandlerContext ctx, ChannelStateEvent e) { ChannelHandlerContext ctx, ChannelStateEvent e) {
// Send the first message. Server will not send anything here // Send the first message. Server will not send anything here
// because the firstMessage's capacity is 0. // because the firstMessage's capacity is 0.
//e.getChannel().write(firstMessage); e.getChannel().write(firstMessage);
} }
@Override @Override

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
@ -42,21 +41,19 @@ public class EchoServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory); // Set up the default event pipeline.
EchoHandler handler = new EchoHandler(); EchoHandler handler = new EchoHandler();
bootstrap.getPipeline().addLast("handler", handler); bootstrap.getPipeline().addLast("handler", handler);
bootstrap.setOption("backlog", 4096);
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));
// Start performance monitor. // Start performance monitor.
//new ThroughputMonitor(handler).start(); new ThroughputMonitor(handler).start();
} }
} }

View File

@ -48,6 +48,7 @@ public class BigIntegerDecoder extends FrameDecoder {
if (buffer.readableBytes() < 4) { if (buffer.readableBytes() < 4) {
return null; return null;
} }
int dataLength = buffer.getInt(buffer.readerIndex()); int dataLength = buffer.getInt(buffer.readerIndex());
// Wait until the whole data is available. // Wait until the whole data is available.

View File

@ -27,7 +27,6 @@ import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
@ -59,17 +58,14 @@ public class FactorialClient {
throw new IllegalArgumentException("count must be a positive integer."); throw new IllegalArgumentException("count must be a positive integer.");
} }
// Set up. // Configure the client.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory);
// Set up the event pipeline factory.
bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count)); bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count));
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
// Make a new connection. // Make a new connection.
ChannelFuture connectFuture = ChannelFuture connectFuture =
@ -83,10 +79,10 @@ public class FactorialClient {
(FactorialClientHandler) channel.getPipeline().getLast(); (FactorialClientHandler) channel.getPipeline().getLast();
// Print out the answer. // Print out the answer.
System.out.format( System.err.format(
"Factorial of %,d is: %,d", count, handler.getFactorial()); "Factorial of %,d is: %,d", count, handler.getFactorial());
// Shut down all thread pools to exit. // Shut down all thread pools to exit.
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
} }
} }

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
@ -42,16 +41,13 @@ public class FactorialServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory);
// Set up the event pipeline factory.
bootstrap.setPipelineFactory(new FactorialServerPipelineFactory()); bootstrap.setPipelineFactory(new FactorialServerPipelineFactory());
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));

View File

@ -40,6 +40,7 @@ public class HttpStaticFileServer {
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool())); Executors.newCachedThreadPool()));
// Set up the event pipeline factory.
bootstrap.setPipelineFactory(new HttpStaticFileServerPipelineFactory()); bootstrap.setPipelineFactory(new HttpStaticFileServerPipelineFactory());
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.

View File

@ -28,7 +28,6 @@ import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.http.CookieEncoder; import org.jboss.netty.handler.codec.http.CookieEncoder;
@ -68,12 +67,12 @@ public class HttpClient {
} }
// Configure the client. // Configure the client.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory); // Set up the event pipeline factory.
bootstrap.setPipelineFactory(new HttpClientPipelineFactory()); bootstrap.setPipelineFactory(new HttpClientPipelineFactory());
// Start the connection attempt. // Start the connection attempt.
@ -83,25 +82,25 @@ public class HttpClient {
Channel channel = future.awaitUninterruptibly().getChannel(); Channel channel = future.awaitUninterruptibly().getChannel();
if (!future.isSuccess()) { if (!future.isSuccess()) {
future.getCause().printStackTrace(); future.getCause().printStackTrace();
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
return; return;
} }
// Send the HTTP request. // Send the HTTP request.
HttpRequest request = new DefaultHttpRequest( HttpRequest request = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString()); HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());
request.addHeader(HttpHeaders.Names.HOST, host); request.setHeader(HttpHeaders.Names.HOST, host);
request.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
CookieEncoder httpCookieEncoder = new CookieEncoder(false); CookieEncoder httpCookieEncoder = new CookieEncoder(false);
httpCookieEncoder.addCookie("my-cookie", "foo"); httpCookieEncoder.addCookie("my-cookie", "foo");
httpCookieEncoder.addCookie("another-cookie", "bar"); httpCookieEncoder.addCookie("another-cookie", "bar");
request.addHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
channel.write(request); channel.write(request);
// Wait for the server to close the connection. // Wait for the server to close the connection.
channel.getCloseFuture().awaitUninterruptibly(); channel.getCloseFuture().awaitUninterruptibly();
// Shut down executor threads to exit. // Shut down executor threads to exit.
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
} }
} }

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
@ -39,15 +38,13 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class HttpServer { public class HttpServer {
public static void main(String[] args) { public static void main(String[] args) {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory); // Set up the event pipeline factory.
bootstrap.setPipelineFactory(new HttpServerPipelineFactory()); bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));

View File

@ -64,13 +64,17 @@ public class HttpTunnelingClientExample {
URI uri = new URI(args[0]); URI uri = new URI(args[0]);
String scheme = uri.getScheme() == null? "http" : uri.getScheme(); String scheme = uri.getScheme() == null? "http" : uri.getScheme();
// Configure the client.
ClientBootstrap b = new ClientBootstrap( ClientBootstrap b = new ClientBootstrap(
new HttpTunnelingClientSocketChannelFactory( new HttpTunnelingClientSocketChannelFactory(
new OioClientSocketChannelFactory(Executors.newCachedThreadPool()))); new OioClientSocketChannelFactory(Executors.newCachedThreadPool())));
// Set up the default event pipeline.
b.getPipeline().addLast("decoder", new StringDecoder()); b.getPipeline().addLast("decoder", new StringDecoder());
b.getPipeline().addLast("encoder", new StringEncoder()); b.getPipeline().addLast("encoder", new StringEncoder());
b.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO)); b.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO));
// Set additional options required by the HTTP tunneling transport.
b.setOption("serverName", uri.getHost()); b.setOption("serverName", uri.getHost());
b.setOption("serverPath", uri.getRawPath()); b.setOption("serverPath", uri.getRawPath());
@ -83,10 +87,13 @@ public class HttpTunnelingClientExample {
return; return;
} }
ChannelFuture channelFuture = b.connect(new InetSocketAddress(uri.getHost(), uri.getPort())); // Make the connection attempt.
ChannelFuture channelFuture = b.connect(
new InetSocketAddress(uri.getHost(), uri.getPort()));
channelFuture.awaitUninterruptibly(); channelFuture.awaitUninterruptibly();
System.out.println("Enter text ('quit' to exit)");
// Read commands from the stdin. // Read commands from the stdin.
System.out.println("Enter text ('quit' to exit)");
ChannelFuture lastWriteFuture = null; ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (; ;) { for (; ;) {
@ -108,6 +115,7 @@ public class HttpTunnelingClientExample {
// Wait until the connection is closed or the connection attempt fails. // Wait until the connection is closed or the connection attempt fails.
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly(); channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
// Shut down all threads.
b.releaseExternalResources(); b.releaseExternalResources();
} }
} }

View File

@ -27,19 +27,15 @@ import java.io.InputStreamReader;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelDownstreamHandler;
import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory; import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory;
import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory; import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory;
import org.jboss.netty.channel.local.LocalAddress; import org.jboss.netty.channel.local.LocalAddress;
import org.jboss.netty.example.echo.EchoHandler; import org.jboss.netty.example.echo.EchoHandler;
import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder; import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.logging.LoggingHandler;
import org.jboss.netty.logging.InternalLogLevel;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
@ -48,24 +44,35 @@ import org.jboss.netty.handler.codec.string.StringEncoder;
*/ */
public class LocalExample { public class LocalExample {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ChannelFactory factory = new DefaultLocalServerChannelFactory(); // Address to bind on / connect to.
ServerBootstrap bootstrap = new ServerBootstrap(factory);
EchoHandler handler = new EchoHandler();
LocalAddress socketAddress = new LocalAddress("1"); LocalAddress socketAddress = new LocalAddress("1");
bootstrap.getPipeline().addLast("handler", handler);
bootstrap.bind(socketAddress);
ChannelFactory channelFactory = new DefaultLocalClientChannelFactory(); // Configure the server.
ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory); ServerBootstrap sb = new ServerBootstrap(
clientBootstrap.getPipeline().addLast("decoder", new StringDecoder()); new DefaultLocalServerChannelFactory());
clientBootstrap.getPipeline().addLast("encoder", new StringEncoder());
clientBootstrap.getPipeline().addLast("handler", new PrintHandler()); // Set up the default server-side event pipeline.
ChannelFuture channelFuture = clientBootstrap.connect(socketAddress); EchoHandler handler = new EchoHandler();
sb.getPipeline().addLast("handler", handler);
// Start up the server.
sb.bind(socketAddress);
// Configure the client.
ClientBootstrap cb = new ClientBootstrap(
new DefaultLocalClientChannelFactory());
// Set up the default client-side event pipeline.
cb.getPipeline().addLast("decoder", new StringDecoder());
cb.getPipeline().addLast("encoder", new StringEncoder());
cb.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO));
// Make the connection attempt to the server.
ChannelFuture channelFuture = cb.connect(socketAddress);
channelFuture.awaitUninterruptibly(); channelFuture.awaitUninterruptibly();
System.out.println("Enter text (quit to end)");
// Read commands from the stdin. // Read commands from the stdin.
System.out.println("Enter text (quit to end)");
ChannelFuture lastWriteFuture = null; ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (; ;) { for (; ;) {
@ -83,22 +90,12 @@ public class LocalExample {
lastWriteFuture.awaitUninterruptibly(); lastWriteFuture.awaitUninterruptibly();
} }
channelFuture.getChannel().close(); channelFuture.getChannel().close();
// Wait until the connection is closed or the connection attempt fails. // Wait until the connection is closed or the connection attempt fails.
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly(); channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
}
@ChannelPipelineCoverage("all") // Release all resources used by the local transport.
static class PrintHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler { cb.releaseExternalResources();
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) sb.releaseExternalResources();
throws Exception {
System.err.println(e);
ctx.sendUpstream(e);
}
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
throws Exception {
System.err.println(e);
ctx.sendDownstream(e);
}
} }
} }

View File

@ -26,50 +26,50 @@ import java.util.concurrent.TimeUnit;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelDownstreamHandler;
import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.channel.ChannelUpstreamHandler;
import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory; import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory;
import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory; import org.jboss.netty.channel.local.DefaultLocalServerChannelFactory;
import org.jboss.netty.channel.local.LocalAddress; import org.jboss.netty.channel.local.LocalAddress;
import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder; import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import org.jboss.netty.handler.logging.LoggingHandler;
import org.jboss.netty.logging.InternalLogLevel;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Andy Taylor (andy.taylor@jboss.org) * @author Andy Taylor (andy.taylor@jboss.org)
* @version $Rev: 1482 $, $Date: 2009-06-19 19:48:17 +0200 (ven., 19 juin 2009) $ * @author Frederic Bregier (fredbregier@free.fr)
* @version $Rev$, $Date$
*/ */
public class LocalExampleMultiple { public class LocalExampleMultthreaded {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
OrderedMemoryAwareThreadPoolExecutor orderedMemoryAwareThreadPoolExecutor = LocalAddress socketAddress = new LocalAddress("1");
OrderedMemoryAwareThreadPoolExecutor eventExecutor =
new OrderedMemoryAwareThreadPoolExecutor( new OrderedMemoryAwareThreadPoolExecutor(
5, 1000000, 10000000, 100, 5, 1000000, 10000000, 100,
TimeUnit.MILLISECONDS); TimeUnit.MILLISECONDS);
ChannelFactory factory = new DefaultLocalServerChannelFactory();
ServerBootstrap bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new LocalServerPipelineFactory(orderedMemoryAwareThreadPoolExecutor));
LocalAddress socketAddress = new LocalAddress("1");
bootstrap.bind(socketAddress);
ChannelFactory channelFactory = new DefaultLocalClientChannelFactory(); ServerBootstrap sb = new ServerBootstrap(
ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory); new DefaultLocalServerChannelFactory());
clientBootstrap.getPipeline().addLast("decoder", new StringDecoder());
clientBootstrap.getPipeline().addLast("encoder", new StringEncoder()); sb.setPipelineFactory(new LocalServerPipelineFactory(eventExecutor));
clientBootstrap.getPipeline().addLast("handler", new PrintHandler()); sb.bind(socketAddress);
ClientBootstrap cb = new ClientBootstrap(
new DefaultLocalClientChannelFactory());
cb.getPipeline().addLast("decoder", new StringDecoder());
cb.getPipeline().addLast("encoder", new StringEncoder());
cb.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO));
// Read commands from array // Read commands from array
String []commands = { String[] commands = { "First", "Second", "Third", "quit" };
"First", "Second", "Third", "quit"
};
for (int j = 0; j < 5 ; j++) { for (int j = 0; j < 5 ; j++) {
System.err.println("Start "+j); System.err.println("Start "+j);
ChannelFuture channelFuture = clientBootstrap.connect(socketAddress); ChannelFuture channelFuture = cb.connect(socketAddress);
channelFuture.awaitUninterruptibly(); channelFuture.awaitUninterruptibly();
if (! channelFuture.isSuccess()) { if (! channelFuture.isSuccess()) {
System.err.println("CANNOT CONNECT"); System.err.println("CANNOT CONNECT");
@ -91,24 +91,10 @@ public class LocalExampleMultiple {
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly(); channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
System.err.println("End "+j); System.err.println("End "+j);
} }
clientBootstrap.releaseExternalResources();
bootstrap.releaseExternalResources();
orderedMemoryAwareThreadPoolExecutor.shutdownNow();
}
@ChannelPipelineCoverage("all") // Release all resources
static class PrintHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler { cb.releaseExternalResources();
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) sb.releaseExternalResources();
throws Exception { eventExecutor.shutdownNow();
System.err.println(e);
ctx.sendUpstream(e);
}
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
throws Exception {
System.err.println(e);
ctx.sendDownstream(e);
} }
} }
}

View File

@ -22,6 +22,8 @@
*/ */
package org.jboss.netty.example.local; package org.jboss.netty.example.local;
import java.util.concurrent.Executor;
import org.jboss.netty.channel.ChannelDownstreamHandler; import org.jboss.netty.channel.ChannelDownstreamHandler;
import org.jboss.netty.channel.ChannelEvent; import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelHandlerContext;
@ -34,24 +36,26 @@ import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder; import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.execution.ExecutionHandler; import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
/** /**
* @author frederic * @author The Netty Project (netty-dev@lists.jboss.org)
* * @author Andy Taylor (andy.taylor@jboss.org)
* @author Frederic Bregier (fredbregier@free.fr)
* @version $Rev$, $Date$
*/ */
public class LocalServerPipelineFactory implements ChannelPipelineFactory { public class LocalServerPipelineFactory implements ChannelPipelineFactory {
OrderedMemoryAwareThreadPoolExecutor orderedMemoryAwareThreadPoolExecutor;
public LocalServerPipelineFactory(OrderedMemoryAwareThreadPoolExecutor orderedMemoryAwareThreadPoolExecutor) { private final Executor eventExecutor;
this.orderedMemoryAwareThreadPoolExecutor =
orderedMemoryAwareThreadPoolExecutor; public LocalServerPipelineFactory(Executor eventExecutor) {
this.eventExecutor = eventExecutor;
} }
public ChannelPipeline getPipeline() throws Exception { public ChannelPipeline getPipeline() throws Exception {
final ChannelPipeline pipeline = Channels.pipeline(); final ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("pipelineExecutor", new ExecutionHandler( pipeline.addLast("executor", new ExecutionHandler(eventExecutor));
orderedMemoryAwareThreadPoolExecutor));
pipeline.addLast("handler", new EchoCloseServerHandler()); pipeline.addLast("handler", new EchoCloseServerHandler());
return pipeline; return pipeline;
} }
@ -64,7 +68,6 @@ public class LocalServerPipelineFactory implements ChannelPipelineFactory {
final MessageEvent evt = (MessageEvent) e; final MessageEvent evt = (MessageEvent) e;
String msg = (String) evt.getMessage(); String msg = (String) evt.getMessage();
if (msg.equalsIgnoreCase("quit")) { if (msg.equalsIgnoreCase("quit")) {
// TRY COMMENT HERE, then it works
Channels.close(e.getChannel()); Channels.close(e.getChannel());
return; return;
} }
@ -72,13 +75,11 @@ public class LocalServerPipelineFactory implements ChannelPipelineFactory {
ctx.sendUpstream(e); ctx.sendUpstream(e);
} }
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) {
throws Exception {
if (e instanceof MessageEvent) { if (e instanceof MessageEvent) {
final MessageEvent evt = (MessageEvent) e; final MessageEvent evt = (MessageEvent) e;
String msg = (String) evt.getMessage(); String msg = (String) evt.getMessage();
if (msg.equalsIgnoreCase("quit")) { if (msg.equalsIgnoreCase("quit")) {
// COMMENT OR NOT, NO PROBLEM
Channels.close(e.getChannel()); Channels.close(e.getChannel());
return; return;
} }

View File

@ -31,7 +31,6 @@ import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
@ -62,16 +61,13 @@ public class LocalTimeClient {
} }
// Set up. // Set up.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory);
// Configure the event pipeline factory.
bootstrap.setPipelineFactory(new LocalTimeClientPipelineFactory()); bootstrap.setPipelineFactory(new LocalTimeClientPipelineFactory());
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
// Make a new connection. // Make a new connection.
ChannelFuture connectFuture = ChannelFuture connectFuture =
@ -90,7 +86,7 @@ public class LocalTimeClient {
channel.close().awaitUninterruptibly(); channel.close().awaitUninterruptibly();
// Shut down all thread pools to exit. // Shut down all thread pools to exit.
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
// Print the response at last but not least. // Print the response at last but not least.
Iterator<String> i1 = cities.iterator(); Iterator<String> i1 = cities.iterator();

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
@ -42,16 +41,13 @@ public class LocalTimeServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory);
// Set up the event pipeline factory.
bootstrap.setPipelineFactory(new LocalTimeServerPipelineFactory()); bootstrap.setPipelineFactory(new LocalTimeServerPipelineFactory());
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.example.echo.EchoClient; import org.jboss.netty.example.echo.EchoClient;
@ -61,17 +60,14 @@ public class ObjectEchoClient {
} }
// Configure the client. // Configure the client.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory); // Set up the default event pipeline.
ObjectEchoHandler handler = new ObjectEchoHandler(firstMessageSize); ObjectEchoHandler handler = new ObjectEchoHandler(firstMessageSize);
bootstrap.getPipeline().addLast("handler", handler); bootstrap.getPipeline().addLast("handler", handler);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
// Start the connection attempt. // Start the connection attempt.
bootstrap.connect(new InetSocketAddress(host, port)); bootstrap.connect(new InetSocketAddress(host, port));

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.example.echo.EchoServer; import org.jboss.netty.example.echo.EchoServer;
@ -42,17 +41,14 @@ public class ObjectEchoServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory); // Set up the default event pipeline.
ObjectEchoHandler handler = new ObjectEchoHandler(); ObjectEchoHandler handler = new ObjectEchoHandler();
bootstrap.getPipeline().addLast("handler", handler); bootstrap.getPipeline().addLast("handler", handler);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));

View File

@ -60,6 +60,8 @@ public class HexDumpProxy {
Executor executor = Executors.newCachedThreadPool(); Executor executor = Executors.newCachedThreadPool();
ServerBootstrap sb = new ServerBootstrap( ServerBootstrap sb = new ServerBootstrap(
new NioServerSocketChannelFactory(executor, executor)); new NioServerSocketChannelFactory(executor, executor));
// Set up the event pipeline factory.
ClientSocketChannelFactory cf = ClientSocketChannelFactory cf =
new NioClientSocketChannelFactory(executor, executor); new NioClientSocketChannelFactory(executor, executor);

View File

@ -29,7 +29,6 @@ import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.example.telnet.TelnetClient; import org.jboss.netty.example.telnet.TelnetClient;
@ -59,17 +58,13 @@ public class SecureChatClient {
int port = Integer.parseInt(args[1]); int port = Integer.parseInt(args[1]);
// Configure the client. // Configure the client.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory);
SecureChatClientHandler handler = new SecureChatClientHandler(); SecureChatClientHandler handler = new SecureChatClientHandler();
bootstrap.setPipelineFactory(new SecureChatPipelineFactory(handler)); bootstrap.setPipelineFactory(new SecureChatPipelineFactory(handler));
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
// Start the connection attempt. // Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
@ -78,7 +73,7 @@ public class SecureChatClient {
Channel channel = future.awaitUninterruptibly().getChannel(); Channel channel = future.awaitUninterruptibly().getChannel();
if (!future.isSuccess()) { if (!future.isSuccess()) {
future.getCause().printStackTrace(); future.getCause().printStackTrace();
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
return; return;
} }
@ -112,6 +107,6 @@ public class SecureChatClient {
channel.close().awaitUninterruptibly(); channel.close().awaitUninterruptibly();
// Shut down all thread pools to exit. // Shut down all thread pools to exit.
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
} }
} }

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.example.telnet.TelnetServer; import org.jboss.netty.example.telnet.TelnetServer;
@ -42,17 +41,14 @@ public class SecureChatServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory);
SecureChatServerHandler handler = new SecureChatServerHandler(); SecureChatServerHandler handler = new SecureChatServerHandler();
bootstrap.setPipelineFactory(new SecureChatPipelineFactory(handler)); bootstrap.setPipelineFactory(new SecureChatPipelineFactory(handler));
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));

View File

@ -29,7 +29,6 @@ import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
@ -57,17 +56,13 @@ public class TelnetClient {
int port = Integer.parseInt(args[1]); int port = Integer.parseInt(args[1]);
// Configure the client. // Configure the client.
ChannelFactory factory = ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ClientBootstrap bootstrap = new ClientBootstrap(factory);
TelnetClientHandler handler = new TelnetClientHandler(); TelnetClientHandler handler = new TelnetClientHandler();
bootstrap.setPipelineFactory(new TelnetPipelineFactory(handler)); bootstrap.setPipelineFactory(new TelnetPipelineFactory(handler));
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
// Start the connection attempt. // Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
@ -76,7 +71,7 @@ public class TelnetClient {
Channel channel = future.awaitUninterruptibly().getChannel(); Channel channel = future.awaitUninterruptibly().getChannel();
if (!future.isSuccess()) { if (!future.isSuccess()) {
future.getCause().printStackTrace(); future.getCause().printStackTrace();
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
return; return;
} }
@ -110,6 +105,6 @@ public class TelnetClient {
channel.close().awaitUninterruptibly(); channel.close().awaitUninterruptibly();
// Shut down all thread pools to exit. // Shut down all thread pools to exit.
factory.releaseExternalResources(); bootstrap.releaseExternalResources();
} }
} }

View File

@ -26,7 +26,6 @@ import java.net.InetSocketAddress;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/** /**
@ -41,17 +40,13 @@ public class TelnetServer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Configure the server. // Configure the server.
ChannelFactory factory = ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory( new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()); Executors.newCachedThreadPool()));
ServerBootstrap bootstrap = new ServerBootstrap(factory);
TelnetServerHandler handler = new TelnetServerHandler(); TelnetServerHandler handler = new TelnetServerHandler();
bootstrap.setPipelineFactory(new TelnetPipelineFactory(handler)); bootstrap.setPipelineFactory(new TelnetPipelineFactory(handler));
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind and start to accept incoming connections. // Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(8080)); bootstrap.bind(new InetSocketAddress(8080));