‘liusuyi’
2023-12-28 1c490ff27a0eeb4327923a9367f3a7c2aaa929b2
src/main/java/com/ard/utils/netty/tcp/BootNettyClient.java
@@ -23,6 +23,8 @@
    IArdEquipRadarService ardEquipRadarService;
    @Resource
    NettyTcpConfiguration nettyTcpConfig;
    static Integer waitTimes = 1;
    static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    /**
@@ -33,12 +35,16 @@
            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<SocketChannel>());
        bootstrap.group(group).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new BootNettyChannelInitializer());
        return bootstrap;
    }
 
    public void connect( String host,int port) throws Exception {
    public void connect(ArdEquipRadar radar) throws Exception {
        String host = radar.getIp();
        int port=radar.getPort();
        log.debug("正在进行连接:【" + host+":"+port+"】");
        eventLoopGroup.shutdownGracefully();
        eventLoopGroup = new NioEventLoopGroup();
@@ -57,13 +63,14 @@
                    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(host,port);
                        connect(radar);
                    }
                }
            });
@@ -73,7 +80,7 @@
            try {
                Thread.sleep(waitTimes * 1000);
            } finally {
                connect(host,port);
                connect(radar);
            }
            e.printStackTrace();
        } finally {
@@ -97,7 +104,7 @@
            String host = ardEquipRadar.getIp();
            Integer port = Integer.valueOf(ardEquipRadar.getPort());
            log.debug("TCP client try to connect radar【:" + host + ":" + port+"】");
            BootNettyClientThread thread = new BootNettyClientThread(host,port);
            BootNettyClientThread thread = new BootNettyClientThread(ardEquipRadar);
            thread.start();
        }
    }