From 9aa66699aaa610be66f5bd0c22e90cae114311f9 Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: 星期五, 07 七月 2023 17:15:16 +0800
Subject: [PATCH] 优化外联设备报警解析并上传mqtt
---
src/main/java/com/ard/utils/tcp/NettyTcpClient.java | 63 +++++++++++++++++++++----------
1 files changed, 42 insertions(+), 21 deletions(-)
diff --git a/src/main/java/com/ard/utils/tcp/NettyTcpClient.java b/src/main/java/com/ard/utils/tcp/NettyTcpClient.java
index 2d01793..464bba5 100644
--- a/src/main/java/com/ard/utils/tcp/NettyTcpClient.java
+++ b/src/main/java/com/ard/utils/tcp/NettyTcpClient.java
@@ -1,7 +1,9 @@
package com.ard.utils.tcp;
+import com.ard.alarm.radar.domain.ArdEquipRadar;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
@@ -11,8 +13,10 @@
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
+import java.util.concurrent.TimeUnit;
+
/**
- * @Description:
+ * @Description: 鍙︿竴绉峵cp瀹㈡埛绔� 鐩墠寮冪敤
* @ClassName: NettyTcpClient
* @Author: 鍒樿嫃涔�
* @Date: 2023骞�06鏈�25鏃�17:00
@@ -20,28 +24,45 @@
**/
@EnableAsync
@Component
-@Slf4j(topic = "radar")
+@Slf4j(topic = "netty")
public class NettyTcpClient {
- @Async
- public void init(String host, Integer port) {
- NioEventLoopGroup group = new NioEventLoopGroup();
- try {
- Bootstrap bootstrap = new Bootstrap();
- bootstrap.group(group)
- .channel(NioSocketChannel.class)
- .handler(new ChannelInitializer<SocketChannel>() {
- @Override
- protected void initChannel(SocketChannel ch) throws Exception {
- ch.pipeline().addLast(new NettyTcpClientHandler());
- }
- });
- ChannelFuture future = bootstrap.connect(host, port).sync();
- future.channel().closeFuture().sync();
+ @Async("alarm")
+ public void init(ArdEquipRadar ardEquipRadar) {
+ while (true) {
+ NioEventLoopGroup group = new NioEventLoopGroup();
+ try {
+ Bootstrap bootstrap = new Bootstrap();
+ bootstrap.group(group)
+ .channel(NioSocketChannel.class)
+ .handler(new ChannelInitializer<SocketChannel>() {
+ @Override
+ protected void initChannel(SocketChannel ch) throws Exception {
+ ch.pipeline().addLast(new NettyTcpClientHandler(ardEquipRadar));
+ }
+ });
+ String host = ardEquipRadar.getIp();
+ Integer port = ardEquipRadar.getPort();
+ ChannelFuture future = bootstrap.connect(host, port).sync();
+ // 娣诲姞杩炴帴鎴愬姛鐨勭洃鍚櫒
+ future.addListener((ChannelFutureListener) future1 -> {
+ if (future1.isSuccess()) {
+ log.info("tcp杩炴帴鎴愬姛" + host + ":" + port);
+ } else {
+ log.info("tcp杩炴帴澶辫触" + host + ":" + port);
+ }
+ });
+ future.channel().closeFuture().sync();
- } catch (Exception ex) {
- log.error("nettyTcp瀹㈡埛绔垵濮嬪寲寮傚父:" + ex.getMessage());
- } finally {
- group.shutdownGracefully();
+ } catch (Exception e) {
+ log.error("nettyTcp瀹㈡埛绔垵濮嬪寲寮傚父:" + e.getMessage());
+ try {
+ TimeUnit.SECONDS.sleep(5); // 绛夊緟5绉掑悗閲嶆柊杩炴帴
+ } catch (InterruptedException ex) {
+ Thread.currentThread().interrupt();
+ }
+ } finally {
+ group.shutdownGracefully();
+ }
}
}
}
--
Gitblit v1.9.3