puzhibing
2023-08-01 98bc380ae322eaac2ee7e21d0c565e30eacce2b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.sinata.zuul.util.echo;
 
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.timeout.IdleStateHandler;
 
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
 
/**
 * 即时通讯服务启动类
 *
 * @date 2016年6月25日
 * @version 1.0
 */
public class NettyServer {
 
 
    /**
     * NettyServer启动方法.
     */
    public void bind() {
        final Thread thread = new Thread(new NettyRunnable());
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                thread.start();
            }
        }, 10000);
    }
 
    /**
     * 即时通讯服务启动
     *
     * @date 2016年6月24日
     * @version 1.0
     */
    public class NettyRunnable implements Runnable {
 
        /**
         * 获取即时通讯启动端口 
         */
        private Integer nettyPort = 9999;
        @Override
        public void run() {
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap bootstrap = new ServerBootstrap();
                bootstrap.group(bossGroup, workerGroup);
                bootstrap.channel(NioServerSocketChannel.class);
                bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
                // 通过TCP_NODELAY禁用NAGLE,使消息立即发出去,不用等待到一定的数据量才发出去
                bootstrap.option(ChannelOption.TCP_NODELAY, true);
                // 保持长连接状态
                bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
                bootstrap.childHandler(new ServerInit() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast("ping", new IdleStateHandler(120, 60, 5, TimeUnit.SECONDS));
                        pipeline.addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
                        pipeline.addLast("encoder", new LengthFieldPrepender(4));
                        //pipeline.addLast(new LineBasedFrameDecoder(1048576 * 10));
                        //pipeline.addLast(new StringDecoder(Charset.forName("UTF-8")));
                        //pipeline.addLast(new StringEncoder(Charset.forName("UTF-8")));
                        pipeline.addLast(new DiscardServerHandler());
                    }
                });
                // 服务器绑定端口监听
                ChannelFuture f = bootstrap.bind(nettyPort).sync();
                if(f.isSuccess()) {
                    System.out.println("******************************Netty启动成功******************************");
                }
                // 监听服务器关闭监听
                f.channel().closeFuture().sync();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    }
}