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"));
|
}
|
}
|