package cn.flightfeather.supervision.infrastructure.test;
|
|
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.channel.Channel;
|
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.EventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
import java.net.InetSocketAddress;
|
|
public class EchoServer {
|
|
private final int port;
|
|
public EchoServer(int port) {
|
this.port = port;
|
}
|
|
public static void main(String[] args) throws Exception {
|
int port = Integer.parseInt("8081"); //⇽--- 设置端口值(如果端口参数的格式不正确,则抛出一个NumberFormatException)
|
new EchoServer(port).start(); //⇽--- 调用服务器的start()方法
|
}
|
|
public void start() throws Exception {
|
final EchoServerHandler serverHandler = new EchoServerHandler();
|
EventLoopGroup group = new NioEventLoopGroup(); //⇽--- 创建Event-LoopGroup
|
try {
|
ServerBootstrap b = new ServerBootstrap(); //⇽--- 创建Server-Bootstrap
|
b.group(group)
|
.channel(NioServerSocketChannel.class) //⇽--- 指定所使用的NIO传输Channel
|
.localAddress(new InetSocketAddress(port)) //⇽--- 使用指定的端口设置套接字地址
|
.childHandler(new ChannelInitializer(){
|
//⇽--- 添加一个EchoServer-
|
//Handler到子Channel的ChannelPipeline
|
@Override
|
protected void initChannel(Channel channel) throws Exception {
|
channel.pipeline().addLast(serverHandler); //⇽--- EchoServerHandler被标注为@Shareable,所以我们可以总是使用同样的实例
|
}
|
});
|
ChannelFuture f = b.bind().sync(); //⇽--- 异步地绑定服务器;调用sync()方法阻塞等待直到绑定完成
|
f.channel().closeFuture().sync(); //⇽--- 获取Channel的CloseFuture,并且阻塞当前线程直到它完成
|
} finally {
|
group.shutdownGracefully().sync(); //⇽--- 关闭EventLoopGroup,释放所有的资源
|
}
|
}
|
}
|