Channel Handlers, Threads and Stack

48 views
Skip to first unread message

L_DisplayName

unread,
Feb 2, 2017, 5:22:36 PM2/2/17
to Netty discussions
Greetings All,

Do the instances of channel handlers such as (SimpleChannelInboundHandler,  ChannelInboundHandlerAdapter, etc.) share the same thread and stack or do each have it’s own thread and stack? I ask because I instantiate numerous channel handlers and I need them to communicate with each other and I must decide between using thread communication or non- threaded communication. 


Thank you for your answer

Tim Boudreau

unread,
Feb 13, 2017, 11:51:42 PM2/13/17
to Netty discussions
If your handler is sharable (you're not creating a new one for every input), then multiple threads may all be inside a single handler at once.  In that case, you want to write handlers to be stateless - no internal variables that one input can update.  Channel.attr() can be useful if you want to pass some data between handlers.  Or you can chain up a bunch of MessageToMessageDecoders so each one generates the object that is the input to the next - in that case, you don't worry about whether the handlers are all called on the same thread (which is most likely given the way the thread pools Netty uses work) or not.

So, basically, Netty's handler pipeline *is* your channel for communication between handlers - use it, rather than inventing your own additional one.

-Tim
Reply all
Reply to author
Forward
0 new messages