开发者社区> 问答> 正文

netty客户端重连的问题?报错

public class ReconnectTimer extends TimerTask {
    @Override
    public void run() {
        System.out.println("正在尝试与服务器连接......");
        try {
            new TelnetClient("192.168.0.107", 9090).run();
            System.out.println("与服务器断开连接,尝试10秒后与服务器重新连接......");
        } catch (Exception e) {
            System.out.println("无法连接到服务器,尝试10秒后与服务器重新连接......");
            return;
        }
    }
public static void main(String[] args) throws Exception {
    Timer clientTimer = new Timer("Client Reconnect Timer");// 创建定时器
    clientTimer.schedule(new ReconnectTimer(), 0, 10 * 1000);// 开启断线重连定时器
}

}

这是netty客户端重连服务端代码,下面是netty服务端代码

     try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup,workerGroup)
             .channel(NioServerSocketChannel.class)
             .childHandler(new TelnetServerPipelineFactory());
            b.bind(port).sync().channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

标红的地方应该是客户端连接断开的时候,代码就会往下走,反之,就阻塞在这里。

我设想这样的场景,如果服务端压根没有启动的话,直接运行客户端,其运行结果如下:

无法连接到服务器,尝试10秒后与服务器重新连接......
正在尝试与服务器连接......
无法连接到服务器,尝试10秒后与服务器重新连接......
正在尝试与服务器连接......
无法连接到服务器,尝试10秒后与服务器重新连接......
正在尝试与服务器连接......
无法连接到服务器,尝试10秒后与服务器重新连接......
正在尝试与服务器连接......

但是,如果,我一开始就把netty服务端启动后,再运行客户端后,客户端自然会连上服务端,这个时候,我再把netty服务端给关闭掉,却发现客户端报错

警告: Unexpected exception from downstream.
java.io.IOException: 远程主机强迫关闭了一个现有的连接。

此后,客户端不再执行定时任务了,按照我的设想,这个时候,应该会继续执行定时器任务的,为什么不执行定时器任务呢?

 

展开
收起
爱吃鱼的程序员 2020-06-20 16:48:13 798 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    IOException是uncheckedexception

    如果TimerTask抛出未检查的异常,Timer将会产生无法预料的行为。Timer线程并不捕获异常,所以TimerTask抛出的未检查的异常会终止timer线程
    定时任务现在推荐用ScheduledExecutorService

    TelnetClient这边没有处理exception,你可以overrideexceptionCaught方法      //客户端重启
          bootstrap.connect().addListener(newChannelFutureListener(){
            @Override
            publicvoidoperationComplete(ChannelFuturefuture){
              if(!future.isSuccess()){
                logger.info("服务端无法正常连接,客户端正在尝试重连");
                try{
                  Thread.sleep(retryInterval);
                }catch(Exceptione){
                  logger.error(e.getMessage(),e);
                }


                future.channel().close();
                bootstrap.connect().addListener(this);
              }
            }
          });
    2020-06-20 16:48:29
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载