Class BlockingReadHandler<E>

java.lang.Object
org.jboss.netty.channel.SimpleChannelUpstreamHandler
org.jboss.netty.handler.queue.BlockingReadHandler<E>
Type Parameters:
E - the type of the received messages
All Implemented Interfaces:
ChannelHandler, ChannelUpstreamHandler

public class BlockingReadHandler<E> extends SimpleChannelUpstreamHandler
Emulates blocking read operation. This handler stores all received messages into a BlockingQueue and returns the received messages when read(), read(long, TimeUnit), readEvent(), or readEvent(long, TimeUnit) method is called.

Please note that this handler is only useful for the cases where there are very small number of connections, such as testing and simple client-side application development.

Also, any handler placed after this handler will never receive messageReceived, exceptionCaught, and channelClosed events, hence it should be placed in the last place in a pipeline.

Here is an example that demonstrates the usage:

 BlockingReadHandler<ChannelBuffer> reader =
         new BlockingReadHandler<ChannelBuffer>();
 ChannelPipeline p = ...;
 p.addLast("reader", reader);

 ...

 // Read a message from a channel in a blocking manner.
 try {
     ChannelBuffer buf = reader.read(60, TimeUnit.SECONDS);
     if (buf == null) {
         // Connection closed.
     } else {
         // Handle the received message here.
     }
 } catch (BlockingReadTimeoutException e) {
     // Read timed out.
 } catch (IOException e) {
     // Other read errors
 }