【问题标题】:Netty 4.0.x Correctly catch a ConnectExceptionNetty 4.0.x 正确捕获 ConnectException
【发布时间】:2014-07-15 14:34:44
【问题描述】:

我正在使用 Netty 开发一个应用程序,我需要处理客户端抛出的 ConnectException,以防例如连接超时。

这是代码

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;

public class ConnectTest {

public static void main(String[] args) throws Exception {
    Bootstrap b = new Bootstrap();
    b.group(new NioEventLoopGroup()).channel(NioSocketChannel.class)
    .handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {

        }
    });
    final ChannelFuture f = b.connect("0.0.0.0", 8080);
    f.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!f.isSuccess())
                System.out.println("Test Connection failed");
        }
    });
    f.sync();
}
}

结果如下:

测试连接失败 线程“主”中的异常 java.net.ConnectException:连接被拒绝:没有更多信息:/0.0.0.0:8080 在 sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) 在 sun.nio.ch.SocketChannelImpl.finishConnect(未知来源) 在 io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:208) 在 io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:287) 在 io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:524) 在 io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized (NioEventLoop.java:464) 在 io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:378) 在 io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350) 在 io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) 在 java.lang.Thread.run(未知来源)

正如您所见,监听器工作正常,但我仍然打印出丑陋的堆栈跟踪,我不知道在哪里拦截它。

有什么提示吗?

【问题讨论】:

  • 您能否包含 (SSCCE)[sscce.org/] 和堆栈跟踪?

标签: java netty connectexception


【解决方案1】:

罪魁祸首是

f.sync() 

您可以在侦听器中处理 ConnectExceptions:

    final ChannelFuture f = b.connect("0.0.0.0", 8080);
    f.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!f.isSuccess()) {
                System.out.println("Test Connection failed");
                handleException(future.cause());

            }
        }
    });
    //f.sync();

【讨论】:

  • 好的,这解决了问题,但我不明白,如果我希望调用线程(示例中为 Main)等待引导程序连接怎么办?您是在告诉我,永远不应该在从连接获得的 ChannelFuture 上调用 sync() 吗?
  • 通常你希望一切都是异步的,尽量不要阻塞,但如果你必须...用 f.sync() 周围的 try-catch 块处理异常
  • 好的,我之前尝试过在 f.sync 周围使用 try catch,但在 ConnectException 上而不是在 Exception 上进行了 catch。所以 f.sync 并不是真正的罪魁祸首,你解决了我的问题,但我建议你更正答案只是为了让事情更清楚
猜你喜欢
  • 2016-07-04
  • 2017-08-02
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
相关资源
最近更新 更多