Reconnecting a client automatically *without losing state*

Bruno de Carvalho kindernade at gmail.com
Mon Aug 2 18:19:08 EDT 2010


Benoit,


This is one of those cases where I'd ditch the PipelineFactory and
create the pipeline manually.

Considering that every time you call connect on a client bootstrap you
have to know a host and port, if you build your pipeline "manually", you
can pass this information to the YourHandler instance via constructor
param.

Alternatively, you can have your pipeline factory assembling the
pipeline except for the last ChannelHandler, which would be YourHandler.
You'd then append this one as the last handler to the pipeline.

Do note that you'd actually be calling setPipeline() and not
setPipelineFactory() on the bootstrap. You'd just use the pipeline
factory for the sake of separation of concerns.


Bruno

On Mon, 2010-08-02 at 14:39 -0700, tsuna wrote:
> On Mon, Aug 2, 2010 at 7:05 AM, brunodecarvalho <kindernade at gmail.com> wrote:
> > Now your listener can attempt to reconnect the exact same handler by either
> > creating a new pipeline with the same handler in place or access the
> > pipeline of the handler that just disconnected and create a new bootstrap
> > with it, thus reusing the same handler (which will be using a different
> > Channel in each connection).
> 
> Ah, I didn't think about creating a new ClientBootstrap and calling
> setPipeline on it, instead of setPipelineFactory, in order to re-use
> the same pipeline (and thus re-use the existing stateful handlers).
> 
> The only problem remaining is that unfortunately if I get a
> ConnectException during the initial connection, then this.channel will
> never be set, so I don't know where to reconnect to.  In your example
> you store the host and port in the handler as they're given to you
> through the constructor, but it's not clear how this information would
> reach the constructor in the first place.  When you create a
> ClientBootstrap for the very first time, you give it a shared
> ChannelPipelineFactory that is re-used to create any number of new
> clients, and because this ChannelPipelineFactory is shared, it doesn't
> have access to the host / port for this particular client, so it
> cannot give it to the constructor of your handler.
> 




More information about the netty-users mailing list