I am writing a messaging system using Netty. I cannot send a subsequent message before the first message has been sent successfully (and at times wait for a response of the send from the peer endpoint). I see recommendations not to wait for future to be completed in ChannelHandlerAdapter, as it chews up cpu cycles in the EventLoop.
The question then is - How do I achieve this sequential logic without waiting for the first send to complete in the ChannelHandlerAdapter EventLoop or avoid a thread context switch between an application thread and eventloop thread?
a) If I wait for the ChannelFuture to complete in ChannelHandlerAdapter, it chews up cpu cycles. If I send the subsequent messages in the listener registered with the channelFuture of a write, by having application logic in the listener registered with ChanelFuture of this channel, this will chew up cpu cycles too in the EventLoop.
or,
b) If I use the channel in an application thread and write to this channel, there is a thread context switch from Application thread to the Channel thread. Is there a way to avoid this thread context switch in this use-case?
Is there a better way?