| | |
| | | package com.ard.tianyu.netty; |
| | | |
| | | import com.ard.tianyu.config.TyConfiguration; |
| | | import com.ard.tianyu.model.cache.TyTcpServerCache; |
| | | import com.ard.tianyu.netty.handler.ClientHandler; |
| | | import com.ard.tianyu.netty.handler.MessageProtocolDecoder; |
| | | import com.ard.tianyu.netty.handler.MessageProtocolEncoder; |
| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | | }); |
| | | //将连接存入集合 |