leutu
2024-06-03 3ef35e6cd16bbfa206b26bb3271eac40ad020bcb
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
package com.fastbee.server;
 
 
import com.fastbee.server.config.NettyConfig;
import io.netty.bootstrap.AbstractBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
 
import java.util.concurrent.ExecutorService;
 
/**
 * 基础服务器启动类
 *
 * @Author guanshubiao
 * @Date 2022/9/12 20:22
 */
@Slf4j
@NoArgsConstructor
public abstract class Server {
 
    protected EventLoopGroup bossGroup;
    protected EventLoopGroup workerGroup;
    protected ExecutorService businessService;
    protected boolean isRunning;
    public NettyConfig config;
 
 
 
    protected Server(NettyConfig config){
        this.config = config;
    }
 
    /*初始化方法*/
    protected abstract AbstractBootstrap initialize();
 
 
    public synchronized boolean start() {
        if (isRunning) {
            log.warn("=>服务:{},在端口:{},已经运行", config.name, config.port);
            return isRunning;
        }
        AbstractBootstrap bootstrap = initialize();
        ChannelFuture future = bootstrap.bind(config.port).awaitUninterruptibly();
        future.channel().closeFuture().addListener(event -> {
            if (isRunning) {
                stop();
            }
        });
        if (isRunning = future.isSuccess()) {
            log.info("=>服务:{},在端口:{},启动成功!", config.name, config.port);
            return isRunning;
        }
 
        if (future.cause() != null) {
            log.error("服务启动失败", future.cause());
        }
        return isRunning;
    }
 
 
    public synchronized void stop() {
 
        isRunning = false;
        bossGroup.shutdownGracefully();
        if (workerGroup != null) {
            workerGroup.shutdownGracefully();
        }
        if (businessService != null) {
            businessService.shutdown();
        }
        log.warn("=>服务:{},在端口:{},已经停止!", config.name, config.port);
    }
 
 
}