|
|
@@ -0,0 +1,44 @@
|
|
|
+package com.xiaobao.gateway.protocol.echo;
|
|
|
+
|
|
|
+import io.netty.bootstrap.ServerBootstrap;
|
|
|
+import io.netty.channel.*;
|
|
|
+import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
+import io.netty.channel.socket.SocketChannel;
|
|
|
+import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
+import io.netty.handler.logging.LogLevel;
|
|
|
+import io.netty.handler.logging.LoggingHandler;
|
|
|
+
|
|
|
+public class EchoServer {
|
|
|
+ static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ // Configure the server.
|
|
|
+ EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
|
|
+ EventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
|
+ final EchoServerHandler serverHandler = new EchoServerHandler();
|
|
|
+ try {
|
|
|
+ ServerBootstrap b = new ServerBootstrap();
|
|
|
+ b.group(bossGroup, workerGroup)
|
|
|
+ .channel(NioServerSocketChannel.class)
|
|
|
+ .option(ChannelOption.SO_BACKLOG, 100)
|
|
|
+ .handler(new LoggingHandler(LogLevel.INFO))
|
|
|
+ .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
+ @Override
|
|
|
+ public void initChannel(SocketChannel ch) throws Exception {
|
|
|
+ ChannelPipeline p = ch.pipeline();
|
|
|
+ p.addLast(serverHandler);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // Start the server.
|
|
|
+ ChannelFuture f = b.bind(PORT).sync();
|
|
|
+
|
|
|
+ // Wait until the server socket is closed.
|
|
|
+ f.channel().closeFuture().sync();
|
|
|
+ } finally {
|
|
|
+ // Shut down all event loops to terminate all threads.
|
|
|
+ bossGroup.shutdownGracefully();
|
|
|
+ workerGroup.shutdownGracefully();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|