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