自动化立体仓库 - WCS系统
zhangc
2025-03-11 d5449236ef0b3adafb3e4cc872f50479efa0ce7b
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
package com.zy.core.netty;
 
 
import com.zy.core.netty.properties.TcpProperties;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
 
/**
 * TCP/IP协议端口
 * Netty Server 引导类
 * @author Vincent
 */
@Slf4j
@Component
public class OnlineServer {
 
    private final HandlerInitializer handlerInitializer;
    private final TcpProperties tcpProperties;
    private Channel channel;
    private ServerBootstrap bootstrap;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;
 
    {
        bootstrap = new ServerBootstrap();
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }
 
    @Autowired
    public OnlineServer(TcpProperties tcpProperties, HandlerInitializer handlerInitializer) {
        ;
        this.tcpProperties = tcpProperties;
        this.handlerInitializer = handlerInitializer;
    }
 
    /**
     *  tcp server init
     */
    @PostConstruct
    public void serverStart() throws Exception {
        // 开启大端模式
//        CStruct.reverse = false;
 
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(handlerInitializer)
                .option(ChannelOption.SO_BACKLOG, tcpProperties.getBacklog())
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.SO_KEEPALIVE, tcpProperties.isKeepAlive())
                .childOption(ChannelOption.SO_SNDBUF, tcpProperties.getSndbuf())
                .childOption(ChannelOption.SO_RCVBUF, tcpProperties.getRcvbuf());
 
        ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
 
        log.info("TCP server started successfully, port:{}", tcpProperties.getPort());
 
        channel = bootstrap.bind(tcpProperties.getPort()).sync().channel();
    }
 
 
    /**
     * tcp server stop
     */
    @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: {}", tcpProperties.getPort());
    }
 
}