From ce309155d4a0d750f786ca242b36bba8fc0bbf14 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Tue, 22 Oct 2024 17:26:43 +0800
Subject: [PATCH] 优化
---
ard-work/src/main/java/com/ard/tianyu/netty/TyNettyTcpClient.java | 47 ++++++++++++++++++++++-------------------------
1 files changed, 22 insertions(+), 25 deletions(-)
diff --git a/ard-work/src/main/java/com/ard/tianyu/netty/TyNettyTcpClient.java b/ard-work/src/main/java/com/ard/tianyu/netty/TyNettyTcpClient.java
index 9c8d52d..02f2fa7 100644
--- a/ard-work/src/main/java/com/ard/tianyu/netty/TyNettyTcpClient.java
+++ b/ard-work/src/main/java/com/ard/tianyu/netty/TyNettyTcpClient.java
@@ -11,6 +11,7 @@
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
+
import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.HashMap;
@@ -45,59 +46,55 @@
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
- pipeline.addLast("heart",new IdleStateHandler(0, 3, 0, TimeUnit.SECONDS));
- pipeline.addLast("encoder",new MessageProtocolEncoder());
- pipeline.addLast("decoder",new MessageProtocolDecoder());
- pipeline.addLast("handler",clientHandler);
+ pipeline.addLast("heart", new IdleStateHandler(0, 3, 0, TimeUnit.SECONDS));
+ pipeline.addLast("encoder", new MessageProtocolEncoder());
+ pipeline.addLast("decoder", new MessageProtocolDecoder());
+ pipeline.addLast("handler", clientHandler);
}
});
return bootstrap;
}
// 获取所有连接
- public void initAllChannel(List<Map<String,Object>> portHosts) {
+ public void initAllChannel(List<Map<String, Object>> portHosts) {
for (Map<String, Object> map : portHosts) {
String host = (String) map.get("IP");
Integer port = (Integer) map.get("PORT");
- initOneConnection(host,port);
+ initOneConnection(host, port);
}
}
- public void initOneConnection(String host,Integer port){
+ public void initOneConnection(String host, Integer port) {
- log.info("调用一次{}:{}",host,port);
+ log.info("初始化连接天宇tcp服务器【{}:{}】", host, port);
//判断该账号下,是否有盒子被控制
//当前正在被控制的盒子
String currentBoxSn = "";
- if(channels.containsKey(host+"/"+port))
- {
- currentBoxSn = channels.get(host+"/"+port).getSubBoxSn();
- channels.get(host+"/"+port).getFuture().channel().close();
+ if (channels.containsKey(host + ":" + port)) {
+ currentBoxSn = channels.get(host + ":" + port).getSubBoxSn();
+ channels.get(host + ":" + port).getFuture().channel().close();
}
//异步连接tcp服务端
Bootstrap bootstrap = getBootstrap(null);
- ChannelFuture future = bootstrap.connect(host,port).addListener((ChannelFuture futureListener) -> {
+ ChannelFuture future = bootstrap.connect(host, port).addListener((ChannelFuture futureListener) -> {
if (futureListener.isSuccess()) {
- log.info("连接天宇TCP服务端成功:"+host+":"+port);
+ log.info("连接天宇TCP服务器成功【{}:{}】", host, port);
} else {
final EventLoop eventLoop = futureListener.channel().eventLoop();
- eventLoop.schedule(new Runnable() {
- @Override
- public void run() {
- log.info("连接天宇TCP服务端失败ip:{},port:{},正尝试重连...",host,port);
- try {
- initOneConnection(host,port);
- } catch (Exception e) {
- e.printStackTrace();
- }
+ eventLoop.schedule(() -> {
+ log.info("连接天宇TCP服务器失败【{}:{}】,正尝试重连...", host, port);
+ try {
+ initOneConnection(host, port);
+ } catch (Exception e) {
+ e.printStackTrace();
}
}, 5, TimeUnit.SECONDS);
}
});
//判断是否有盒子被订阅
- channels.put(host+"/"+port, TyBoxSession.builder().subBoxSn(currentBoxSn).future(future)
+ channels.put(host + ":" + port, TyBoxSession.builder().subBoxSn(currentBoxSn).future(future)
.isReConnect(!currentBoxSn.equals("")).build());
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.3