'How to postpone response on netty

In my Netty application I need to postpone the response until some data (which is going to be provided by other clients) be available. It's more like Producer-Consumer Problem implementing in a stack.

If I implement my handler like this, I'll loose the channel and can't send the client stackContent when other client's push on stack.

public class ProducerConsumerHandler extends SimpleChannelInboundHandler<TheCommand> {
  private static Stack<StackContent> theStack = new Stack<>();

  @Override
  protected void channelRead0(ChannelHandlerContext context, TheCommand msg) {
      if (!theStack.isEmpty())
          if (TheCommand.CONSUME == msg){ 
              context.writeAndFlush(theStack.pop());
          }
          if (TheCommand.PRODUCE == msg){ 
              context.writeAndFlush("OK");
          }
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source