Client side disconnect/reconnect?

brunodecarvalho kindernade at gmail.com
Sat Jul 24 15:14:56 EDT 2010


Bota,


I believe the following will do what you want:

// I'm assuming your handler extends SCUH and your connection
// establishment code lies somewhere else outside of your handler...
public class YourHandler implements SimpleChannelUpstreamHandler {
  ...
  public void disconnect() {
    if ((this.channel == null) || !this.channel.isConnected()) {
      return;
    }

    // flush all pending writes before requesting close
    // beware, it's still possible to get hit with ChannelClosedException
here
    this.channel.write(ChannelBuffers.EMPTY_BUFFER).awaitUninterruptibly()
            .getChannel().close().awaitUninterruptibly();
  }
}

And in the code that calls the disconnect() method on the handler instance,
the following:

...
handler.disconnect(); // blocking call, returns as soon as channel
disconnects
openConnectionToServer(...);
...

However, I suspect that the right (read "elegant") way of doing this, would
be using custom events.

Something like:

// ctx is some sort of connection context, stores info like host, port, etc
// so you can later on reconnect
channel.write(ChannelBuffers.EMPTY_BUFFER).addListener(new
CustomCloseListener(ctx));

and your CustomCloseListener (which received ctx via constructor) would have
in its operationComplete() method the following:

public void operationComplete(ChannelFuture future) {
  future.getChannel().close().addListener(new ReconnectListener(ctx));
}

The ReconnectListener would then implement the reconnection logic in its
operationComplete() method.

It pretty much does the same as the previous version but works
asynchronously (so your timer wouldn't block on disconnect()).
Depends on what suits your needs better, I guess...


Cheers,
  Bruno
-- 
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Client-side-disconnect-reconnect-tp5331263p5333458.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list