riku
2022-06-17 3a5c011d9509d3bc0367921f463676c81ff2e374
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
package cn.flightfeather.supervision.infrastructure.service;
 
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelGroupFuture;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.net.InetSocketAddress;
 
public abstract class AbstractNettyServer implements NettyServer {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractNettyServer.class);
    //用于管理所有的channel
    public static final ChannelGroup ALL_CHANNELS = new DefaultChannelGroup("NADRON-CHANNELS", GlobalEventExecutor.INSTANCE);
    protected final NettyConfig nettyConfig;
    protected ChannelInitializer<? extends Channel> channelInitializer;
 
    public AbstractNettyServer(NettyConfig nettyConfig,
                               ChannelInitializer<? extends Channel> channelInitializer) {
        this.nettyConfig = nettyConfig;
        this.channelInitializer = channelInitializer;
    }
 
    @Override
    public void startServer(int port) throws Exception {
        nettyConfig.setPortNumber(port);
        nettyConfig.setSocketAddress(new InetSocketAddress(port));
        startServer();
    }
 
    @Override
    public void startServer(InetSocketAddress socketAddress) throws Exception {
        nettyConfig.setSocketAddress(socketAddress);
        startServer();
    }
 
    @Override
    public void stopServer() throws Exception {
        LOG.debug("In stopServer method of class: {}", this.getClass()
                .getName());
        ChannelGroupFuture future = ALL_CHANNELS.close();
        try {
            future.await();
        } catch (InterruptedException e) {
            LOG.error(
                    "Execption occurred while waiting for channels to close: {}",
                    e);
        } finally {
            if (null != nettyConfig.getBossGroup()) {
                nettyConfig.getBossGroup().shutdownGracefully();
            }
            if (null != nettyConfig.getWorkerGroup()) {
                nettyConfig.getWorkerGroup().shutdownGracefully();
            }
        }
    }
 
    @Override
    public ChannelInitializer<? extends Channel> getChannelInitializer() {
        return channelInitializer;
    }
 
    // 获取configuration @link(NettyConfig.class)
    @Override
    public NettyConfig getNettyConfig() {
        return nettyConfig;
    }
 
    // 获取bossGroup,在spring中配置
    protected EventLoopGroup getBossGroup() {
        return nettyConfig.getBossGroup();
    }
 
    // 获取workerGroup, 在spring中配置
    protected EventLoopGroup getWorkerGroup() {
        return nettyConfig.getWorkerGroup();
    }
 
    @Override
    public InetSocketAddress getSocketAddress() {
        return nettyConfig.getSocketAddress();
    }
 
    @Override
    public String toString() {
        return "NettyServer [socketAddress=" + nettyConfig.getSocketAddress()
                + ", portNumber=" + nettyConfig.getPortNumber() + "]";
    }
}