Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用netty 4.0在连接变为非活动状态后重新连接_Java_Netty - Fatal编程技术网

Java 如何使用netty 4.0在连接变为非活动状态后重新连接

Java 如何使用netty 4.0在连接变为非活动状态后重新连接,java,netty,Java,Netty,我有scanerio,在其中,一旦由于任何原因使用netty 4.0导致连接变为非活动状态,我必须重新连接到服务器。代码应尝试重新连接,直到成功连接。以下是连接到服务器一次的代码 Bootstrap b; b.group(group); b.channel(NioSocketChannel.class); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000); // b.option(Channe

我有scanerio,在其中,一旦由于任何原因使用netty 4.0导致连接变为非活动状态,我必须重新连接到服务器。代码应尝试重新连接,直到成功连接。以下是连接到服务器一次的代码

Bootstrap b;
b.group(group);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
//        b.option(ChannelOption., 10000);
        b.handler(new ChannelInitializer<SocketChannel>() {
             @Override
             public void initChannel(SocketChannel ch) throws Exception {
               ch.pipeline().addLast("frameDecoder", new DelimiterBasedFrameDecoder(bufferSize,bt));
               ch.pipeline().addLast("ByteDecoder", new ByteArrayDecoder());                    
               ch.pipeline().addLast("frameEncoder", new ByteArrayEncoder());
               ch.pipeline().addLast (new TimeClientHandler (c));
             }
         });
        System.out.println("Connecting Server");
        this.host = host;
        this.port = port;
        try {
            f = b.connect(host, port).sync();

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Bootstrap b;
b、 组(组);
b、 通道(NioSocketChannel.class);
b、 选项(ChannelOption.CONNECT\u TIMEOUT\u MILLIS,10000);
//b.选择权(渠道选择权,10000);
b、 处理程序(新的ChannelInitializer(){
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ch.pipeline().addLast(“frameDecoder”,新定界符rbasedframedecoder(bufferSize,bt));
ch.pipeline().addLast(“ByteDecoder”,新的ByteArrayDecoder());
ch.pipeline().addLast(“帧编码器”,新的ByteArrayEncoder());
ch.pipeline().addLast(新的TimeClientHandler(c));
}
});
System.out.println(“连接服务器”);
this.host=host;
this.port=端口;
试一试{
f=b.connect(主机、端口).sync();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}

您应该将ChannelFutureListener添加到connect(…)返回的ChannelFuture,并检查future是否失败。如果失败,您可以在需要时尝试重新连接。

此外,您还需要处理断开的连接。这是你的电话号码