feiyu02
2024-08-13 b8cc591541b88dd2bb93f111f8e8075842dce7ca
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
96
97
98
99
100
101
102
103
104
105
package cn.flightfeather.supervision.infrastructure.service;
 
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
 
import java.net.InetSocketAddress;
import java.util.Map;
 
// 用于配置server
public class NettyConfig {
 
    private Map<ChannelOption<?>, Object> channelOptions;
 
    // reactor多线程模型中的acceptor
    private NioEventLoopGroup bossGroup;
 
    // reactor多线程模型中的threadPool
    private NioEventLoopGroup workerGroup;
 
    //bossGroup的线程数
    private int bossThreadCount;
 
    //workerGroup的线程数
    private int workerThreadCount;
    private InetSocketAddress socketAddress;
    private int portNumber = 18090;
 
    protected ChannelInitializer<? extends Channel> channelInitializer;
 
    public Map<ChannelOption<?>, Object> getChannelOptions() {
        return channelOptions;
    }
 
    public void setChannelOptions(
            Map<ChannelOption<?>, Object> channelOptions) {
        this.channelOptions = channelOptions;
    }
 
    public synchronized NioEventLoopGroup getBossGroup() {
        if (null == bossGroup) {
            if (0 >= bossThreadCount) {
                bossGroup = new NioEventLoopGroup();
            } else {
                bossGroup = new NioEventLoopGroup(bossThreadCount);
            }
        }
        return bossGroup;
    }
 
    public void setBossGroup(NioEventLoopGroup bossGroup) {
        this.bossGroup = bossGroup;
    }
 
    public synchronized NioEventLoopGroup getWorkerGroup() {
        if (null == workerGroup) {
            if (0 >= workerThreadCount) {
                workerGroup = new NioEventLoopGroup();
            } else {
                workerGroup = new NioEventLoopGroup(workerThreadCount);
            }
        }
        return workerGroup;
    }
 
    public void setWorkerGroup(NioEventLoopGroup workerGroup) {
        this.workerGroup = workerGroup;
    }
 
    public int getBossThreadCount() {
        return bossThreadCount;
    }
 
    public void setBossThreadCount(int bossThreadCount) {
        this.bossThreadCount = bossThreadCount;
    }
 
    public int getWorkerThreadCount() {
        return workerThreadCount;
    }
 
    public void setWorkerThreadCount(int workerThreadCount) {
        this.workerThreadCount = workerThreadCount;
    }
 
    public synchronized InetSocketAddress getSocketAddress() {
        if (null == socketAddress) {
            socketAddress = new InetSocketAddress(portNumber);
        }
        return socketAddress;
    }
 
    public void setSocketAddress(InetSocketAddress socketAddress) {
        this.socketAddress = socketAddress;
    }
 
    public int getPortNumber() {
        return portNumber;
    }
 
    public void setPortNumber(int portNumber) {
        this.portNumber = portNumber;
    }
}