Rename (Server)Bootstrap.(child)initializer to (child)handler

- The handler you specify with initializer() is actually simply added
  to the pipeline and that's all.  It's ChannelInitializer which does
  additional work.  For example, a user can specify just a single
  handler with initializer() and it will still work.  This is especially
  common for Bootstrap, so I renamed initializer to handler, which makes
  more sense.
This commit is contained in:
Trustin Lee 2012-06-03 01:00:16 -07:00
parent e241b3d6a2
commit 3b2c25e8ed
33 changed files with 59 additions and 97 deletions

View File

@ -190,7 +190,7 @@ public abstract class AbstractSocketSpdyEchoTest {
final SpdyEchoTestServerHandler sh = new SpdyEchoTestServerHandler();
final SpdyEchoTestClientHandler ch = new SpdyEchoTestClientHandler(frames);
sb.childInitializer(new ChannelInitializer<SocketChannel>() {
sb.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(
@ -200,12 +200,7 @@ public abstract class AbstractSocketSpdyEchoTest {
}
});
cb.initializer(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(ch);
}
});
cb.handler(ch);
Channel sc = sb.localAddress(0).bind().sync().channel();
int port = ((InetSocketAddress) sc.localAddress()).getPort();

View File

@ -17,8 +17,6 @@ package io.netty.example.discard;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioEventLoop;
import io.netty.channel.socket.nio.NioSocketChannel;
@ -43,12 +41,7 @@ public class DiscardClient {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.remoteAddress(host, port)
.initializer(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardClientHandler(firstMessageSize));
}
});
.handler(new DiscardClientHandler(firstMessageSize));
// Make the connection attempt.
ChannelFuture f = b.connect().sync();

View File

@ -39,7 +39,7 @@ public class DiscardServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler());

View File

@ -51,7 +51,7 @@ public class EchoClient {
.channel(new NioSocketChannel())
.option(ChannelOption.TCP_NODELAY, true)
.remoteAddress(new InetSocketAddress(host, port))
.initializer(new ChannelInitializer<SocketChannel>() {
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(

View File

@ -48,13 +48,13 @@ public class EchoServer {
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(new InetSocketAddress(port))
.childOption(ChannelOption.TCP_NODELAY, true)
.initializer(new ChannelInitializer<ServerSocketChannel>() {
.handler(new ChannelInitializer<ServerSocketChannel>() {
@Override
public void initChannel(ServerSocketChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
}
})
.childInitializer(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(

View File

@ -42,7 +42,7 @@ public class FactorialClient {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.remoteAddress(host, port)
.initializer(new FactorialClientInitializer(count));
.handler(new FactorialClientInitializer(count));
// Make a new connection.
ChannelFuture f = b.connect().sync();

View File

@ -37,7 +37,7 @@ public class FactorialServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new FactorialServerInitializer());
.childHandler(new FactorialServerInitializer());
b.bind().sync().channel().closeFuture().sync();
} finally {

View File

@ -33,7 +33,7 @@ public class HttpStaticFileServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new HttpStaticFileServerInitializer());
.childHandler(new HttpStaticFileServerInitializer());
b.bind().sync().channel().closeFuture().sync();
} finally {

View File

@ -65,7 +65,7 @@ public class HttpSnoopClient {
try {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.initializer(new HttpSnoopClientInitializer(ssl))
.handler(new HttpSnoopClientInitializer(ssl))
.remoteAddress(new InetSocketAddress(host, port));
// Make the connection attempt.

View File

@ -41,7 +41,7 @@ public class HttpSnoopServer {
try {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.childInitializer(new HttpSnoopServerInitializer())
.childHandler(new HttpSnoopServerInitializer())
.localAddress(new InetSocketAddress(port));
Channel ch = b.bind().sync().channel();

View File

@ -38,7 +38,7 @@ public class AutobahnServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new AutobahnServerInitializer());
.childHandler(new AutobahnServerInitializer());
ChannelFuture f = b.bind().sync();
System.out.println("Web Socket Server started at port " + port);

View File

@ -86,7 +86,7 @@ public class WebSocketClient {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.remoteAddress(uri.getHost(), uri.getPort())
.initializer(new ChannelInitializer<SocketChannel>() {
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();

View File

@ -53,7 +53,7 @@ public class WebSocketServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new WebSocketServerInitializer());
.childHandler(new WebSocketServerInitializer());
Channel ch = b.bind().sync().channel();
System.out.println("Web socket server started at port " + port + '.');

View File

@ -52,7 +52,7 @@ public class WebSocketSslServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new WebSocketSslServerInitializer());
.childHandler(new WebSocketSslServerInitializer());
Channel ch = b.bind().sync().channel();
System.out.println("Web socket server started at port " + port + '.');

View File

@ -52,13 +52,13 @@ public class LocalEcho {
sb.eventLoop(new LocalEventLoop(), new LocalEventLoop())
.channel(new LocalServerChannel())
.localAddress(addr)
.initializer(new ChannelInitializer<LocalServerChannel>() {
.handler(new ChannelInitializer<LocalServerChannel>() {
@Override
public void initChannel(LocalServerChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
}
})
.childInitializer(new ChannelInitializer<LocalChannel>() {
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(
@ -70,7 +70,7 @@ public class LocalEcho {
cb.eventLoop(new NioEventLoop())
.channel(new LocalChannel())
.remoteAddress(addr)
.initializer(new ChannelInitializer<LocalChannel>() {
.handler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(

View File

@ -48,7 +48,7 @@ public class LocalTimeClient {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.remoteAddress(host, port)
.initializer(new LocalTimeClientInitializer());
.handler(new LocalTimeClientInitializer());
// Make a new connection.
Channel ch = b.connect().sync().channel();

View File

@ -37,7 +37,7 @@ public class LocalTimeServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new LocalTimeServerInitializer());
.childHandler(new LocalTimeServerInitializer());
b.bind().sync().channel().closeFuture().sync();

View File

@ -46,7 +46,7 @@ public class ObjectEchoClient {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.remoteAddress(host, port)
.initializer(new ChannelInitializer<SocketChannel>() {
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(

View File

@ -42,7 +42,7 @@ public class ObjectEchoServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(

View File

@ -42,7 +42,7 @@ public class PortUnificationServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new PortUnificationServerHandler());

View File

@ -42,7 +42,7 @@ public class HexDumpProxy {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(localPort)
.childInitializer(new HexDumpProxyInitializer(remoteHost, remotePort));
.childHandler(new HexDumpProxyInitializer(remoteHost, remotePort));
b.bind().sync().channel().closeFuture().sync();
} finally {

View File

@ -22,8 +22,6 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInboundHandlerContext;
import io.netty.channel.ChannelInboundStreamHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class HexDumpProxyFrontendHandler extends ChannelInboundStreamHandlerAdapter {
@ -49,12 +47,7 @@ public class HexDumpProxyFrontendHandler extends ChannelInboundStreamHandlerAdap
b.eventLoop(inboundChannel.eventLoop())
.channel(new NioSocketChannel())
.remoteAddress(remoteHost, remotePort)
.initializer(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HexDumpProxyBackendHandler(inboundChannel));
}
});
.handler(new HexDumpProxyBackendHandler(inboundChannel));
ChannelFuture f = b.connect();
outboundChannel = f.channel();

View File

@ -18,9 +18,7 @@ package io.netty.example.qotm;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ChannelBuffers;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioEventLoop;
@ -49,12 +47,7 @@ public class QuoteOfTheMomentClient {
.channel(new NioDatagramChannel())
.localAddress(new InetSocketAddress(0))
.option(ChannelOption.SO_BROADCAST, true)
.initializer(new ChannelInitializer<DatagramChannel>() {
@Override
public void initChannel(DatagramChannel ch) throws Exception {
ch.pipeline().addLast(new QuoteOfTheMomentClientHandler());
}
});
.handler(new QuoteOfTheMomentClientHandler());
Channel ch = b.bind().sync().channel();

View File

@ -16,9 +16,7 @@
package io.netty.example.qotm;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.channel.socket.nio.NioEventLoop;
@ -45,12 +43,7 @@ public class QuoteOfTheMomentServer {
.channel(new NioDatagramChannel())
.localAddress(new InetSocketAddress(port))
.option(ChannelOption.SO_BROADCAST, true)
.initializer(new ChannelInitializer<DatagramChannel>() {
@Override
public void initChannel(DatagramChannel ch) throws Exception {
ch.pipeline().addLast(new QuoteOfTheMomentServerHandler());
}
});
.handler(new QuoteOfTheMomentServerHandler());
b.bind().sync().channel().closeFuture().await();
} finally {

View File

@ -44,7 +44,7 @@ public class SecureChatClient {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.remoteAddress(host, port)
.initializer(new SecureChatClientInitializer());
.handler(new SecureChatClientInitializer());
// Start the connection attempt.
Channel ch = b.connect().sync().channel();

View File

@ -37,7 +37,7 @@ public class SecureChatServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new SecureChatServerInitializer());
.childHandler(new SecureChatServerInitializer());
b.bind().sync().channel().closeFuture().sync();
} finally {

View File

@ -43,7 +43,7 @@ public class TelnetClient {
b.eventLoop(new NioEventLoop())
.channel(new NioSocketChannel())
.remoteAddress(host, port)
.initializer(new TelnetClientInitializer());
.handler(new TelnetClientInitializer());
// Start the connection attempt.
Channel ch = b.connect().sync().channel();

View File

@ -36,7 +36,7 @@ public class TelnetServer {
b.eventLoop(new NioEventLoop(), new NioEventLoop())
.channel(new NioServerSocketChannel())
.localAddress(port)
.childInitializer(new TelnetServerPipelineFactory());
.childHandler(new TelnetServerPipelineFactory());
b.bind().sync().channel().closeFuture().sync();
} finally {

View File

@ -61,7 +61,7 @@ public class UptimeClient {
b.eventLoop(l)
.channel(new NioSocketChannel())
.remoteAddress(host, port)
.initializer(new ChannelInitializer<SocketChannel>() {
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new IdleStateHandler(READ_TIMEOUT, 0, 0), handler);

View File

@ -26,7 +26,7 @@ public class Bootstrap {
private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<ChannelOption<?>, Object>();
private EventLoop eventLoop;
private Channel channel;
private ChannelHandler initializer;
private ChannelHandler handler;
private SocketAddress localAddress;
private SocketAddress remoteAddress;
@ -64,11 +64,11 @@ public class Bootstrap {
return this;
}
public Bootstrap initializer(ChannelHandler initializer) {
if (initializer == null) {
throw new NullPointerException("initializer");
public Bootstrap handler(ChannelHandler handler) {
if (handler == null) {
throw new NullPointerException("handler");
}
this.initializer = initializer;
this.handler = handler;
return this;
}
@ -174,7 +174,7 @@ public class Bootstrap {
}
ChannelPipeline p = channel.pipeline();
p.addLast(initializer);
p.addLast(handler);
for (Entry<ChannelOption<?>, Object> e: options.entrySet()) {
try {
@ -192,7 +192,7 @@ public class Bootstrap {
private static boolean ensureOpen(ChannelFuture future) {
if (!future.channel().isOpen()) {
// Registration was successful but the channel was closed due to some failure in
// initializer.
// handler.
future.setFailure(new ChannelException("initialization failure"));
return false;
}
@ -212,8 +212,8 @@ public class Bootstrap {
if (channel == null) {
throw new IllegalStateException("channel not set");
}
if (initializer == null) {
throw new IllegalStateException("initializer not set");
if (handler == null) {
throw new IllegalStateException("handler not set");
}
}

View File

@ -44,8 +44,8 @@ public class ServerBootstrap {
private EventLoop parentEventLoop;
private EventLoop childEventLoop;
private ServerChannel channel;
private ChannelHandler initializer;
private ChannelHandler childInitializer;
private ChannelHandler handler;
private ChannelHandler childHandler;
private SocketAddress localAddress;
public ServerBootstrap eventLoop(EventLoop parentEventLoop, EventLoop childEventLoop) {
@ -95,16 +95,16 @@ public class ServerBootstrap {
return this;
}
public ServerBootstrap initializer(ChannelHandler initializer) {
this.initializer = initializer;
public ServerBootstrap handler(ChannelHandler handler) {
this.handler = handler;
return this;
}
public ServerBootstrap childInitializer(ChannelHandler childInitializer) {
if (childInitializer == null) {
throw new NullPointerException("childInitializer");
public ServerBootstrap childHandler(ChannelHandler childHandler) {
if (childHandler == null) {
throw new NullPointerException("childHandler");
}
this.childInitializer = childInitializer;
this.childHandler = childHandler;
return this;
}
@ -152,8 +152,8 @@ public class ServerBootstrap {
}
ChannelPipeline p = channel.pipeline();
if (initializer != null) {
p.addLast(initializer);
if (handler != null) {
p.addLast(handler);
}
p.addLast(acceptor);
@ -165,7 +165,7 @@ public class ServerBootstrap {
if (!channel.isOpen()) {
// Registration was successful but the channel was closed due to some failure in
// initializer.
// handler.
future.setFailure(new ChannelException("initialization failure"));
return future;
}
@ -191,8 +191,8 @@ public class ServerBootstrap {
if (channel == null) {
throw new IllegalStateException("channel not set");
}
if (childInitializer == null) {
throw new IllegalStateException("childInitializer not set");
if (childHandler == null) {
throw new IllegalStateException("childHandler not set");
}
if (childEventLoop == null) {
logger.warn("childEventLoop is not set. Using eventLoop instead.");
@ -230,7 +230,7 @@ public class ServerBootstrap {
break;
}
child.pipeline().addLast(childInitializer);
child.pipeline().addLast(childHandler);
for (Entry<ChannelOption<?>, Object> e: childOptions.entrySet()) {
try {

View File

@ -45,17 +45,12 @@ public class LocalChannelRegistryTest {
cb.eventLoop(new LocalEventLoop())
.channel(new LocalChannel())
.remoteAddress(addr)
.initializer(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new TestHandler());
}
});
.handler(new TestHandler());
sb.eventLoop(new LocalEventLoop(), new LocalEventLoop())
.channel(new LocalServerChannel())
.localAddress(addr)
.childInitializer(new ChannelInitializer<LocalChannel>() {
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new TestHandler());

View File

@ -38,7 +38,7 @@ public class LocalTransportThreadModelTest {
sb.eventLoop(new LocalEventLoop(), new LocalEventLoop())
.channel(new LocalServerChannel())
.localAddress(LocalAddress.ANY)
.childInitializer(new ChannelInitializer<LocalChannel>() {
.childHandler(new ChannelInitializer<LocalChannel>() {
@Override
public void initChannel(LocalChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundMessageHandlerAdapter<Object>() {