Search code examples
httpnettyevent-driven

Synchronous HTTP call in Netty


My app receives an HTTP request, and in the middle of the pipeline, a call is made to another server for supporting information. The initial HTTP request can't continue through the pipeline until that response comes back. I can't use the awaitUninterruptability() from an I/O thread, so what's the best approach to make these calls so I don't block Netty's event loop, but put the client's pipeline on hold until my call out returns and I tell the pipeline to continue on?


Solution

  • Ryan this does not sound like a good idea..

    I think you should better use something like that:

    public class HttpHandler extends SimpleChannelUpstreamHandler{
    
        @Override
        public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
            otherChannel.write(yourRequet).addListener(new ChannelFutureListener() {
    
                public void operationComplete(ChannelFuture future) throws Exception {
    
                    // once the write is done we can continue in the pipeline
                    ctx.sendUpstream(e);
                }
            });
    
            // the event stops here to get processed
    
        }
    
    }
    

    If you need to wait for the response then you will need to handle it in another SimpleChannelUpstreamHandler. But I think you get the idea..