标签:
AbstractBootstrap是一个工具类,用来配置和启动Channel的,下面看下AbstractBootstrap的类继承,ServerBootstrap用于启动ServerChannel的,是服务端的工具类,Bootstrap是用于启动Channel,是客户端的工具类,bind用于udp等无连接的传输,bind用于有连接的传输。
AbstractBootstrap
该类用来配置启动一个Channel,那么配置启动一个Channel需要那些条件?首先需要一个Channel,AbstractBootstrap提供了一个ChannelFactory对象用来创建Channel,一个Channel会对应一个EventLoop用于IO的事件处理,在Channel的整个生命周期中会绑定一个EventLoop,这里可理解给Channel分配一个线程进行IO事件处理,结束后回收该线程,但是AbstractBootstrap没有提供EventLoop而是提供了一个EventLoopGroup,EventLoop继承EventLoopGroup,在某些情况下可以把EventLoopGroup当EventLoop来用,EventLoopGroup中包含多个Eventloop,当一个连接到达,Netty会注册一个Channel,然后EventLoopGroup会分配一个EventLoop绑定到这个channel。不管是服务器还是客户端的Channel都需要绑定一个本地端口这就有了SocketAddress类的对象localAddress,Channel有很多选项所有有了options对象LinkedHashMap
private volatile EventLoopGroup group;
private volatile ChannelFactory channelFactory;
private volatile SocketAddress localAddress;
private final Map, Object> options = new LinkedHashMap, Object>();
private final Map, Object> attrs = new LinkedHashMap, Object>();
private volatile ChannelHandler handler;
AbstractBootstrap还有一个重要方法doBind(),为什么这个方法会在父类实现而不是在ServerBootstrap中实现,注解里面写了bind()方法不仅可以在ServerBootstrap中绑定一个端口用于监听,也可以用在BootStrap中用于无连接的数据传输。这里主要调用Channel的bind方法。因为netty中多有的操作都是异步的。所以所有IOC操作都返回future的子类。然后判断是否OK,有时间写一篇该文章。
Bootstrap
客户端的帮助类,用来配置启动一个channel,该类有2个主要方法,一个init用于初始化通道,一个是doConnect(),这里面会生成一个客户端的Channel。
private ChannelFuture doConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
final ChannelFuture regFuture = initAndRegister();
final Channel channel = regFuture.channel();
if (regFuture.cause() != null) {
return regFuture;
}
final ChannelPromise promise = channel.newPromise();
if (regFuture.isDone()) {
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
} else {
regFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
doConnect0(regFuture, channel, remoteAddress, localAddress, promise);
}
});
}
return promise;
}
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
}
//p.addLast(new LoggingHandler(LogLevel.INFO));
p.addLast(new EchoClientHandler());
}
});
// Start the client.
ChannelFuture f = b.connect(HOST, PORT).sync(); //
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down the event loop to terminate all threads.
group.shutdownGracefully();
}
private final Map<ChannelOption<?>, Object> childOptions = new LinkedHashMap<ChannelOption<?>, Object>();
private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();
private volatile EventLoopGroup childGroup;
private volatile ChannelHandler childHandler;
p.addLast(new ChannelInitializer<Channel>() { //当有Channel连接该监听端口是调用该
@Override
public void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new ServerBootstrapAcceptor(
currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
/**
* 设置服务端的Channel,Netty通过Channel工厂类创建不同的Channel,对于服务端,
* Netty需要创建NioServerSocketChannel,
*/
.channel(NioServerSocketChannel.class)
/**
* 设置TCP的参数,这里设置了套接字的最大连接个数。
*/
.option(ChannelOption.SO_BACKLOG, 100)
/**
* 设置父类的Handler,父类的handler是客户端新接入的接连SocketChannel对应的ChannelPipeline
* 的handler
*/
.handler(new LoggingHandler(LogLevel.INFO))
/**
* 子类的Hanlder是NioServerSockerChannel对应的ChannelPipeline的Handler
*/
/**
* 区别:ServerBootstrap中的handler是NioServerSocketChannel使用
* 所有连接该监听端口的客户端都会执行它,父类的AbstractBootstrap中的Handler
* 是个工厂类,他为没有新接入的客户端都创建一个新的Handler
*/
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
//p.addLast(new LoggingHandler(LogLevel.INFO));
p.addLast(new EchoServerHandler());
}
});
/**
*每个channel绑定一个ChannelPipeline,一个ChannelPipeline里面添加了很多handler
*/
// Start the server.
ChannelFuture f = b.bind(PORT).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
标签:
原文地址:http://www.cnblogs.com/gaoxing/p/4384784.html