newbie - howto properly handle connect failures

Vijay K Pandey VPandey at mdes.ms.gov
Fri Apr 23 10:22:27 EDT 2010


May be you should not wait Indefinitely on the client connect - have some time parameter - for eg 60 secs or so

future.awaitUninterruptibly(60, TimeUnit.SECONDS);

if (!future.isSuccess()) {
      bootstrap.releaseExternalResources();
      throw future.getCause();
}

I myself starting looking into Netty from yesterday - so I might be totally wrong :)

Cheers
Vijay
-----Original Message-----
From: netty-users-bounces at lists.jboss.org [mailto:netty-users-bounces at lists.jboss.org] On Behalf Of lists at joerg-buchberger.de
Sent: Friday, April 23, 2010 6:46 AM
To: netty-users at lists.jboss.org
Subject: newbie - howto properly handle connect failures

Hi.

I've setup a simple TCP client.
At present, I'm not sure how to properly detect and deal with connect failures.
So, I setup a test case, where the client tries to connect to a non-existing
host ...
in the meantime I got the impression, that I have some basic misconceptions
about proper netty usage in general.

When doing myClientBootstrap.connect(), I somewhat naively assumed, that for my
testcase either the connect()-method would throw an exception or the returned
ChannelFuture would return false on isSuccess() ... however, neither was the
case.

That part of my code looks like this:

    ChannelFuture future = bootstrap.connect(new InetSocketAddress(address,
port));
    channel = future.awaitUninterruptibly().getChannel();
    if (!future.isSuccess()) {
        bootstrap.releaseExternalResources();
        throw future.getCause();
    }

The pipeline of the ClientBootstrap (of the simple tcp client used in my
testcase) contains one simple handler (which is an inner class to the simple
client) ...

    @ChannelPipelineCoverage("one")
    private class FirstClientHandler extends SimpleChannelUpstreamHandler {
        private final ChannelBuffer firstMessage;         public
FirstClientHandler(final int CONNECT_CAPACITY) {
            firstMessage = ChannelBuffers.buffer(CONNECT_CAPACITY);
            for (int capacityCount = 0; capacityCount < CONNECT_CAPACITY;
capacityCount++) {
                // refactor here or offer new constructor, if support for a
server demanding a connect magic is needed
                firstMessage.writeByte((byte) 0x00);
            }
        }

        @Override
        public void channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e) {
            e.getChannel().write(firstMessage);
        }

        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
            boolean offered = messageQueue.offer((ChannelBuffer)
e.getMessage());
            assert offered;
            if (SimpleTcpClient.this.countdownLatch !=
null) SimpleTcpClient.this.countdownLatch.countDown();
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent
e) throws Exception {
            e.getFuture().setFailure(e.getCause());
            e.getChannel().close();
            throw (Exception) e.getCause();
        }
    } // END inner class FirstClientHandler

Now, if I connect to an existing host, everything is fine, in the sense that
messages are exchanged.
But for my testcase with a non-existing host ... I only sometimes get the
exceptionCaught()-method of my simple handler called - though, only if I'm
stepping through with the debugger. If I just execute it, however - it is not
invoked. So, I get no indication that the connection did not happen. And it
surely cannot be established, since the host does not exist. I'm somewhat lost.

Thanks for reading up to here. ;-)
Cheers
Joerg




_______________________________________________
netty-users mailing list
netty-users at lists.jboss.org
https://lists.jboss.org/mailman/listinfo/netty-users


More information about the netty-users mailing list