0
votes

Our scenario is as follow:

  1. There's a TCP server written using Netty.
  2. There are multiple clients establishing a connection to the server.
  3. After establishing the connection, messages can be sent in the two direction.

To be able to deliver messages to correct recipients I need to maintain channel outside of handlers. I'm aware of ChannelGroup which let me easily implement a broadcast server (one message to all clients).

Now the requirement is how to write to specific channel.
I'm thinking to have a ChannelGroup and enrich it with client-specific id map (with a Map<ClientId, ChannelId> mapping client-id to channel-id). This way having a client-id, I can easily get the channel and write to it.

Now my question is:

  1. Generally what is the different between writing via ChannelHandlerContext and via Channel? (since ChannelGroup gives me the Channel not ChannelHandlerContext).
  2. Is it a bad idea to maintain the group of ChannelHandlerContext? (in other words, why don't we have ChannelHandlerContextGroup)?
  3. Any other suggestion how to do it?
1

1 Answers

0
votes

1) The difference between operation on a Channel vs ChannelHandler context is that when using for example Channel.write(...) it will start to traverse the while ChannelPipeline (from the tail to the head), while using ChannelHandlerContext.write(...) it will start the traversal of the ChannelPipeline from the given ChannelHandlerContext which belongs to a specific position in the ChannelPipeline as it is "related" to the ChannelHandler that was added before.

2) It is not a bad idea but most of the times users that use ChannelGroup want to to use Channel.write(...) (see above)

3) Either using ChannelGroup or Map is fine. When using a Map just ensure you will remove the Channel / ChannelHandlerContext from the Map once the Channel is closed.