DanTheMan
DanTheMan

Reputation: 291

Netty 4.0.x Correctly catch a ConnectException

I'm developing an application with Netty and I need to handle the ConnectException on the client side thrown in case, for example, of a connect timeout.

Here's the code

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();
}
}

And this is the result:

        Test Connection failed
        Exception in thread "main" 
    java.net.ConnectException: Connection refused: no further information: /0.0.0.0:8080
        at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
        at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
        at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:208)
        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:287)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:524)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:464)
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:378)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
        at java.lang.Thread.run(Unknown Source)

As you can see the listener works fine, but i still get an ugly stacktrace printed out and I can't figure out where to intercept it.

Any hints?

Upvotes: 2

Views: 4670

Answers (1)

jknair
jknair

Reputation: 4764

the culprit is

f.sync() 

You could handle ConnectExceptions in the listener :

    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();

Upvotes: 5

Related Questions