1:netty 客户端设置连接超时时间,在规定的时间内如果没有连接到服务端视为网络异常,进行跳过处理。
2:我曾试过bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
可是这段代码没有起作用,代码如下:
工作线程池组
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)// //设置线程池
.channel(NioSocketChannel.class)// 设置socket工厂
// .option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 9000)// 设置超时时间
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
// 处理类
pipeline.addLast(new EchoClientHandler());
}
});
try {
// 发起连接操作
ChannelFuture future = bootstrap.connect(host, port);
// 连接若异常发起3次重新连接
i++;
if (i <= 3) {
// 启动时连接超时重试
future.addListener(new ConnectionListener(i));
}
// 等待客户端链路关闭
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
} finally {
// 优雅退出,释放线程池资源
eventLoopGroup.shutdownGracefully();
}
对于这个问题该如何解决呢?