liusuyi
2025-03-04 16fd4235f692777cfd9cceff5090e5450e2173b3
ard-work/src/main/java/com/ard/tianyu/netty/TyNettyTcpClient.java
@@ -1,6 +1,7 @@
package com.ard.tianyu.netty;
import com.ard.tianyu.config.TyConfiguration;
import com.ard.tianyu.domain.cache.TyTcpServerCache;
import com.ard.tianyu.netty.handler.ClientHandler;
import com.ard.tianyu.netty.handler.MessageProtocolDecoder;
import com.ard.tianyu.netty.handler.MessageProtocolEncoder;
@@ -12,7 +13,6 @@
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
@@ -72,16 +72,18 @@
        ChannelFuture future = bootstrap.connect(host, port).addListener((ChannelFuture futureListener) -> {
            if (futureListener.isSuccess()) {
                log.info("连接天宇TCP服务器成功【{}:{}】", host, port);
                TyTcpServerCache.putTcpServerCache(host + ":" + port, true);
            } else {
                final EventLoop eventLoop = futureListener.channel().eventLoop();
                eventLoop.schedule(() -> {
                    log.info("连接天宇TCP服务器失败【{}:{}】,正尝试重连...", host, port);
                    TyTcpServerCache.putTcpServerCache(host + ":" + port, false);
                    try {
                        initConnection(host, port);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }, 5, TimeUnit.SECONDS);
                }, 10, TimeUnit.SECONDS);
            }
        });
        //将连接存入集合