[测评系统]--测评系统核心代码库
linzhijie
2021-03-11 84fea994d2db7dc313ad1774f34eb12a45f8d6e7
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
106
107
108
109
110
111
package com.ots.project.tool.report.reportCalculation.socket.netty;
import com.alibaba.fastjson.JSON;
import com.ots.project.tool.report.reportCalculation.request.HolderInfo;
import com.ots.project.tool.report.reportCalculation.request.MaqRequest;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import static com.ots.project.tool.report.reportCalculation.request.CalculationTypeEnum.MAQ;
import static com.ots.project.tool.report.reportCalculation.request.ReportTypeEnum.firstPath;
 
@Slf4j
public class NettyClient {
    private String host;
    private int port;
    
    private Bootstrap bootstrap = null;
    
    private Channel channel;
    private EventLoopGroup workerGroup;
    public NettyClient(String host, int port) {
        this.host = host;
        this.port = port;
        init();
    }
    public static void main(String[] args) throws Exception {
        final NettyClient nettyClient = new NettyClient("120.24.39.179", 9123);
        new Thread(() -> {
            try {
                nettyClient.start();
            } catch (InterruptedException e) {
                log.error("NettyClient连接已经断开:{}", e.getMessage(), e);
                nettyClient.close();
            }
        }).start();
        Scanner scanner = new Scanner(System.in);
        String msg;
        while (!(msg = scanner.nextLine()).equals("exit")) {
            nettyClient.sendMessage(NettyClient.getString());
        }
        nettyClient.close();
    }
    public static String getString() {
        
        Map data = new HashMap();
        String topics = "MAQ0001,MAQ0002,MAQ0003,MAQ0006,MAQ0009,MAQ0013,MAQ0020,MAQ0041,MAQ0048,MAQ0088,MAQ0094,MAQ0098,MAQ0112,MAQ0117,MAQ0133,MAQ0145,MAQ0163,MAQ0172,MAQ0174,MAQ0175,MAQ0183,MAQ0194,MAQ0203,MAQ0209,MAQ0212,MAQ0224,MAQ0232,MAQ0233,MAQ0235,MAQ0236,MAQ0241,MAQ0244,MAQ0249,MAQ0250,MAQ0251,MAQ0252";
        String[] split = topics.split(",");
        Arrays.asList(split).stream().forEach(item -> {
            data.put(item, "5");
        });
        HolderInfo info = new HolderInfo();
        info.setAccessCode("abdke12315234");
        info.setFullName("Bill Li");
        info.setCalculationType(MAQ);
        MaqRequest request = new MaqRequest();
        request.setInfo(info);
        request.setType(firstPath);
        request.setData(data);
        return JSON.toJSONString(request).trim();
    }
    private void init() {
        bootstrap = new Bootstrap();
        workerGroup = new NioEventLoopGroup();
        bootstrap.group(workerGroup).option(ChannelOption.SO_KEEPALIVE, true)
                .channel(NioSocketChannel.class)
                .handler(new NettyClientInitializer(NettyClient.this));
    }
    public void start() throws InterruptedException {
        ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
        
        channelFuture.addListener((ChannelFutureListener) future -> {
            
            if (future.isSuccess()) {
                log.info("客户端[" + channelFuture.channel().localAddress().toString() + "]已连接...");
                channel = channelFuture.channel();
            }
            
            else {
                log.info("客户端[" + channelFuture.channel().localAddress().toString() + "]连接失败,重新连接中...");
                future.channel().close();
                bootstrap.connect(host, port);
            }
        });
        
        channelFuture.channel().closeFuture().addListener(cfl -> {
            close();
            log.info("客户端[" + channelFuture.channel().localAddress().toString() + "]已断开...");
        });
        
        channelFuture.channel().closeFuture().sync();
    }
    
    private void close() {
        
        if (channel != null) {
            channel.close();
        }
        
        if (workerGroup != null) {
            workerGroup.shutdownGracefully();
        }
    }
    public void sendMessage(String json) throws Exception {
        if (Objects.isNull(channel)) {
            throw new Exception("无法和泰国计算组件连接");
        }
        channel.writeAndFlush(json + System.getProperty("line.separator"));
    }
}