我的netty之旅
程序员文章站
2022-04-23 11:52:33
...
package com.almond;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class MyAlmondServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup work = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss, work).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO)).childHandler(new MyAlmondInitlizer());
Channel channel = serverBootstrap.bind(8899).sync().channel();
channel.closeFuture().sync();
} finally {
boss.shutdownGracefully();
work.shutdownGracefully();
}
}
}
以上代码,EventLoopGroup是循环组,boss负责接受任务,work负责处理任务,handler是针对boss的循环组,childHandler是针对work循环组,closeFuture是长连接
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
以上容器是管道组,服务器端的ChannelHandlerContext得到的channel,就是每个访问者各自的channel,传递给服务器的channel管道中
代码地址 https://gitee.com/chijingsanxun/nettying.git
上一篇: Netty服务端启动流程分析
下一篇: netty开发遇到的问题