What's the best way to reconnect after connection closed in Netty

后端 未结 3 1604
青春惊慌失措
青春惊慌失措 2021-02-01 10:11

Simple scenario:

  1. A lower level class A that extends SimpleChannelUpstreamHandler. This class is the workhorse to send the message and received the response.
3条回答
  •  梦如初夏
    2021-02-01 11:08

    I don't know if this is the right solution but to fix the thread leak of trustin's solution I found I could shutdown the event loop after the scheduler had triggered:

    final EventLoop eventloop = f.channel().eventLoop();
    b.connect().addListener((ChannelFuture f) -> {
        if (!f.isSuccess()) {
            long nextRetryDelay = nextRetryDelay(...);
            eventloop.schedule(() -> {
                doConnect();
                eventloop.shutdownGracefully();
            }, nextRetryDelay, ...);
        }
    });
    

提交回复
热议问题