我在Netty中写了一些简单的代码
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
我想处理java.net.ConnectException
,
ERROR [main] (SDKUpHandler.java:37)| Unexpected exception from downstream. java.net.ConnectException: Connection refused: connect: localhost/127.0.0.1:12345
但我没有找到方法,因为 Netty 版本的 bind
和 connect
不会将它们扔掉。
根据javadoc,connect() 抛出两个异常,但没有抛出java.net.ConnectException
内容。
Throws:
ClassCastException - if "localAddress" option's value is neither a SocketAddress nor null
ChannelPipelineException - if this bootstrap's pipelineFactory failed to create a new ChannelPipeline
任何机构都可以提供一些解决方案吗?
最佳答案
Netty 是异步的。您需要将 ChannelFutureListener 添加到 ChannelFuture,操作完成后它将收到通知。这样您将收到“成功”或异常的通知。检查包含所有这些内容的用户文档。
关于java - 使用Netty时如何处理连接或绑定(bind)异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508621/