vincent
2020-06-04 fe78ade0acf1c83986fe0b5ffa92fd2ead09555c
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
package com.zy.gateway.pipeline;
 
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.ResourceLeakDetector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
 
/**
 * 输送设备网关引导类
 * Created by vincent on 2020-06-04
 */
@Component
public class PipelineBootstrap implements Bootstrap {
 
    private static Logger log = LoggerFactory.getLogger(PipelineBootstrap.class);
 
    private Channel channel;
    private ServerBootstrap bootstrap;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
    @Autowired
    private PipelineProperties pipelineProperties;
    @Autowired
    private HandlerInitializer handlerInitializer;
 
    {
        bootstrap = new ServerBootstrap();
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }
 
    /**
     * 网关启动初始化
     */
    @Override
    @PostConstruct
    public void serverStart() throws Exception {
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(handlerInitializer)
                .option(ChannelOption.SO_BACKLOG, pipelineProperties.getBacklog())
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.SO_KEEPALIVE, pipelineProperties.isKeepAlive())
                .childOption(ChannelOption.SO_SNDBUF, pipelineProperties.getSndbuf())
                .childOption(ChannelOption.SO_RCVBUF, pipelineProperties.getRcvbuf());
 
        ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
 
        log.info("TCP server started successfully, port:{}", pipelineProperties.getTcpPort());
 
        channel = bootstrap.bind(pipelineProperties.getTcpPort()).sync().channel();
    }
 
    @Override
    @PreDestroy
    public void destroy() {
        if (channel != null && channel.isActive()) {
            channel.close();
        }
        if (bossGroup != null) {
            bossGroup.shutdownGracefully();
        }
        if (workerGroup != null) {
            workerGroup.shutdownGracefully();
        }
        log.info("TCP server stopped successfully, port: {}", pipelineProperties.getTcpPort());
    }
}