0
votes

If i cache the Channel [ctx.channel()] instead of caching the ChannelHandlerContext does it make any difference?

Here is the step of implementation and questions:

  1. Netty server handler receives message from client
  2. Because there are other running threads to process the user data, we need to put this request to a queue and let the worker process
  3. We need to have the same connection used for multiple message exchange between the server and client
  4. whenever a message is ready to send to client from the worker thread is it good practice to use the channel cached in the HashMap

    final ChannelFuture status = channel.writeAndFlush(Unpooled.copiedBuffer(payload.getBytes()));

  5. And whenever worker thread wants to close the connection with the client, if we close the cached channel, Is that is sufficient?

see this Netty - can I cache those ChannelHandlerContext in Hash map and response it later?.

Above one is about using the ChannelHandlerContext, but mine is about the channel. Also is there any advantage in using ChannelHandlerContext vs Channel

Thank you all

1

1 Answers

0
votes

Yes, you can cache a Channel and use it later. If all you need is to writeAndFlush() a message from another thread later, just caching Channel is perfectly fine.

ChannelHandlerContext is useful when you want to modify the pipeline dynamically, fire events up and down the pipeline etc. If you don't need these, you can just use Channel.