From b91fdc439ca1ba0b98409de0f1c0edf6fd626ee9 Mon Sep 17 00:00:00 2001 From: ‘liusuyi’ <1951119284@qq.com> Date: 星期五, 21 七月 2023 14:16:35 +0800 Subject: [PATCH] 增加app一键报警 --- src/main/java/com/ard/utils/tcp/NettyTcpClient.java | 70 ++++++++++++++++++++-------------- 1 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/ard/utils/tcp/NettyTcpClient.java b/src/main/java/com/ard/utils/tcp/NettyTcpClient.java index 2d6c324..464bba5 100644 --- a/src/main/java/com/ard/utils/tcp/NettyTcpClient.java +++ b/src/main/java/com/ard/utils/tcp/NettyTcpClient.java @@ -1,5 +1,6 @@ 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; @@ -12,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 @@ -21,36 +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.addListener((ChannelFutureListener) future1 -> { - if (future1.isSuccess()) { - log.info("tcp杩炴帴鎴愬姛"+host+":"+port); - } else { - log.info("tcp杩炴帴澶辫触"+host+":"+port); - } - }); - 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