我在使用Netty(3.6.6)时遇到了一些奇怪的行为:
我构建了一个简单的客户端 - 服务器架构,客户端通过Netty通过TCP连接到服务器。客户端包括重新建立连接的逻辑,如果它断开连接,后退并无限期重试。
问题:如果我强行断开客户端多次,它会到达重新连接尝试开始失败的程度。他们继续失败了许多连接尝试,持续几分钟然后恢复工作(!)。
其他一些观点:
connect()
尝试失败时Channel
已打开但未连接。有没有人遇到过这个问题?
初始化代码(执行一次)
Executor bossPool = Executors.newSingleThreadExecutor(new SimpleThreadFactory("I/O Boss Thread"));
Executor workerPool = Executors.newSingleThreadExecutor();
ChannelFactory channelFactory = new NioClientSocketChannelFactory(bossPool, workerPool);
bootstrap = new ClientBootstrap(channelFactory);
连接代码(在重新连接尝试时执行)
// SimpleChannelHandler responsible for establishing which protocol we're speaking.
SalutationHandler salutationHandler = new SalutationHandler(messageCodec);
// Reconfigure pipeline to initial state.
// Will be reconfigured again after initial client-server handshaking.
bootstrap.setPipeline(configureNewPipeline(Channels.pipeline(salutationHandler)));
// Wait for up to 5 seconds before timing out.
wrapChannelFuture(bootstrap.connect(address)).sync(timeOutMillis, TimeUnit.MILLISECONDS);