3

Is it possible to use a single clientsocketbootstrap to connect to multiple hosts? Also, does each connection get its own pipeline, so one connection can have a certain set of handlers and another its own set of handlers?

bootstrap.connect(serverA, portA);

then, later, after serverA is connected,

bootstrap.connect(serverB, portB);

R. Andrews
  • 105
  • 1
  • 5

2 Answers2

7

Yes you can reuse the client bootstrap without a problem. If you specify a ChannelPipelineFactory each new channel will get its own ChannelPipeline. If you want to have different settings for different clients you may just create one client bootstrap per connection. This works out very well as the bootstrap is really light-weight.

Norman Maurer
  • 211
  • 1
  • 1
1

Take a look at this answer. It shows the code to use same client bootstrap with different handlers, creating multiple connections.

Community
  • 1
  • 1
Abe
  • 8,623
  • 10
  • 50
  • 74
  • Thanks for the tips. I was able to get the client sockets to share a worker thread. Ideally, I would like all sockets to be handled by a single selector once connected including server sockets. It looks like each server socket that is bound to a different port requires a different set of worker threads and that client sockets cannot share a worker with server sockets. is that correct? – R. Andrews Jan 06 '12 at 21:12
  • I ended up creating my own socket channel factories and supplied them with a worker factory interface. The worker factory creates a shared pool of workers (1 in my case). I couldn't just extend NioServerSocketPipelineSink and NioClientSocketPipelineSink because their constructors automatically create the workers, but copying the class code and replacing the worker creation in the constructor with a worker factory that shares pre-created workers did the trick. With this approach, you can have a single worker for multiple server and client channels. – R. Andrews Jan 09 '12 at 16:04
  • Looks like a good solution. I also have servers running on different ports, this looks like a viable solution. Could you share the code as some github gist? – Abe Jan 09 '12 at 18:38