1.public interface EventLoopGroup extends EventExecutorGroupSpecial2.EventExecutorGroup
which allows registeringChannel
s that get processed for later selection during the event loop.
public class NioEventLoopGroup extends MultithreadEventLoopGroup
3.
public class Bootstrap extends AbstractBootstrap<Bootstrap,Channel>
A
strap的意识是捆绑
Bootstrap
that makes it easy to bootstrap a
Channel
to use for clients.
The AbstractBootstrap.bind()
methods are useful in combination with connectionless transports such as datagram (UDP). For regular TCP connections, please use the provided connect()
methods.
4.
public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap,ServerChannel>
Bootstrap
sub-class which allows easy bootstrap of
ServerChannel
ServerBootstrap是Bootstrap的子类
5.了解下服务器启动的时候涉及的ServerBootstrap的相关方法
<T> ServerBootstrap | childAttr(AttributeKey<T> childKey, T value)
Set the specific
AttributeKey with the given value on every child
Channel .
|
EventLoopGroup | childGroup()
Return the configured
EventLoopGroup which will be used for the child channels or
null if non is configured yet.
|
ServerBootstrap | childHandler(ChannelHandler childHandler)
Set the
ChannelHandler which is used to serve the request for the
Channel 's.
|
<T> ServerBootstrap | childOption(ChannelOption<T> childOption, T value)
Allow to specify a
ChannelOption which is used for the
Channel instances once they get created (after the acceptor accepted the
Channel ).
|
ServerBootstrap | clone()
Returns a deep clone of this bootstrap which has the identical configuration.
|
ServerBootstrap | group(EventLoopGroup group)
Specify the
EventLoopGroup which is used for the parent (acceptor) and the child (client).
|
ServerBootstrap | group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
Set the
EventLoopGroup for the parent (acceptor) and the child (client).
|
String | toString() |
ServerBootstrap | validate()
Validate all the parameters.
|
public class HttpServer {
public void start(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
// server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
ch.pipeline().addLast(new HttpResponseEncoder());
// server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
ch.pipeline().addLast(new HttpRequestDecoder());
ch.pipeline().addLast(
new HttpServerInboundHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
HttpServer server = new HttpServer();
server.start(8080);
}
(1)创建了一个ServerBootstrap对象b,这个对象的作用是用来捆绑启动Netty服务
(2)