public void serve(int port) throws IOException, InterruptedException {
final ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8")));
//1. Create ServerBootstrap to allow bootstrap to server instance
ServerBootstrap bootstrap = new ServerBootstrap();
//2. Use OioEventLoopGroup Ito allow blocking mode (Old-IO)
EventLoopGroup group = new OioEventLoopGroup();
try {
bootstrap.group(group)
.channel(OioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() { //3. Specify ChannelInitializer that will be called for each accepted connection
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//4. Add ChannelHandler to intercept events and allow to react on them
ch.pipeline().addLast(new ChannelHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
//5. Write message to client and add ChannelFutureListener to close connection once message written
ctx.write(buf.duplicate()).addListener(ChannelFutureListener.CLOSE);
}
});
}
});
//6. Bind server to accept connections
ChannelFuture future = bootstrap.bind().sync();
future.channel().closeFuture().sync();
} finally {
//7. Release all resources
group.shutdownGracefully().sync();
}
}
NettyOioServer.java 文件源码
java
阅读 26
收藏 0
点赞 0
评论 0
项目:javase-study
作者:
评论列表
文章目录