newbie - howto properly handle connect failures
lists at joerg-buchberger.de
lists at joerg-buchberger.de
Fri Apr 23 07:45:58 EDT 2010
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
More information about the netty-users
mailing list