| package com.ard.utils.netty.tcp; | 
|   | 
| import com.ard.alarm.radar.domain.ArdEquipRadar; | 
| import com.ard.alarm.radar.service.IArdEquipRadarService; | 
| import com.ard.utils.netty.config.NettyTcpConfiguration; | 
| import io.netty.bootstrap.Bootstrap; | 
| import io.netty.channel.*; | 
| import io.netty.channel.nio.NioEventLoopGroup; | 
| import io.netty.channel.socket.nio.NioSocketChannel; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.boot.ApplicationArguments; | 
| import org.springframework.boot.ApplicationRunner; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import javax.annotation.Resource; | 
| import java.nio.channels.SocketChannel; | 
| import java.util.List; | 
|   | 
| @Slf4j(topic = "netty") | 
| @Component | 
| public class BootNettyClient { | 
|     @Resource | 
|     IArdEquipRadarService ardEquipRadarService; | 
|     @Resource | 
|     NettyTcpConfiguration nettyTcpConfig; | 
|   | 
|   | 
|     static Integer waitTimes = 1; | 
|     static EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); | 
|     /** | 
|      * 初始化Bootstrap | 
|      */ | 
|     public static final Bootstrap getBootstrap(EventLoopGroup group) { | 
|         if (null == group) { | 
|             group = eventLoopGroup; | 
|         } | 
|         Bootstrap bootstrap = new Bootstrap(); | 
|         bootstrap.group(group).channel(NioSocketChannel.class) | 
|                 .option(ChannelOption.TCP_NODELAY, true) | 
|                 .option(ChannelOption.SO_KEEPALIVE, true) | 
|                 .handler(new BootNettyChannelInitializer()); | 
|         return bootstrap; | 
|     } | 
|   | 
|     public void connect(ArdEquipRadar radar) throws Exception { | 
|         String host = radar.getIp(); | 
|         int port=radar.getPort(); | 
|         log.debug("正在进行连接:【" + host+":"+port+"】"); | 
|         eventLoopGroup.shutdownGracefully(); | 
|         eventLoopGroup = new NioEventLoopGroup(); | 
|         Bootstrap bootstrap = getBootstrap(null); | 
|   | 
|         try { | 
|             bootstrap.remoteAddress(host, port); | 
|             // 异步连接tcp服务端 | 
|             ChannelFuture future = bootstrap.connect().addListener((ChannelFuture futureListener) -> { | 
|                 final EventLoop eventLoop = futureListener.channel().eventLoop(); | 
|                 if (futureListener.isSuccess()) { | 
|                     BootNettyClientChannel bootNettyClientChannel = new BootNettyClientChannel(); | 
|                     Channel channel = futureListener.channel(); | 
|                     String id = futureListener.channel().id().toString(); | 
| //                    String id = host; | 
|                     bootNettyClientChannel.setChannel(channel); | 
|                     bootNettyClientChannel.setCode("clientId:" + id); | 
|                     BootNettyClientChannelCache.save("clientId:" + id, bootNettyClientChannel); | 
|                     BootNettyClientChannelCache.save(host+":"+port,radar); | 
|                     log.debug("netty client start success=" + id); | 
|                 } else { | 
| //                    System.err.println("连接失败," + waitTimes.toString() + "秒后重新连接:" + host); | 
|                     try { | 
|                         Thread.sleep(waitTimes * 1000); | 
|                     } finally { | 
|                         connect(radar); | 
|                     } | 
|                 } | 
|             }); | 
|             future.channel().closeFuture().sync(); | 
|         } catch (Exception e) { | 
|             System.err.println("连接异常," + waitTimes.toString() + "秒后重新连接:" + host); | 
|             try { | 
|                 Thread.sleep(waitTimes * 1000); | 
|             } finally { | 
|                 connect(radar); | 
|             } | 
|             e.printStackTrace(); | 
|         } finally { | 
|             /** | 
|              * 退出,释放资源 | 
|              */ | 
|             eventLoopGroup.shutdownGracefully().sync(); | 
|         } | 
|   | 
|     } | 
|     /** | 
|      * 初始化方法 | 
|      */ | 
|     public void run(ApplicationArguments args) { | 
|         if (!nettyTcpConfig.getEnabled()) { | 
|             return; | 
|         } | 
|         List<ArdEquipRadar> ardEquipRadars = ardEquipRadarService.selectArdEquipRadarList(new ArdEquipRadar()); | 
|         for (ArdEquipRadar ardEquipRadar : ardEquipRadars) { | 
|             String host = ardEquipRadar.getIp(); | 
|             Integer port = Integer.valueOf(ardEquipRadar.getPort()); | 
|             log.debug("TCP client try to connect radar【:" + host + ":" + port+"】"); | 
|             BootNettyClientThread thread = new BootNettyClientThread(ardEquipRadar); | 
|             thread.start(); | 
|         } | 
|     } | 
| } |