Class 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
     }
     
    • Field Detail

      • queue

        private final java.util.concurrent.BlockingQueue<ChannelEvent> queue
      • closed

        private volatile boolean closed
    • Constructor Detail

      • BlockingReadHandler

        public BlockingReadHandler()
        Creates a new instance with LinkedBlockingQueue
      • BlockingReadHandler

        public BlockingReadHandler​(java.util.concurrent.BlockingQueue<ChannelEvent> queue)
        Creates a new instance with the specified BlockingQueue.
    • Method Detail

      • getQueue

        protected java.util.concurrent.BlockingQueue<ChannelEvent> getQueue()
        Returns the queue which stores the received messages. The default implementation returns the queue which was specified in the constructor.
      • isClosed

        public boolean isClosed()
        Returns true if and only if the Channel associated with this handler has been closed.
        Throws:
        java.lang.IllegalStateException - if this handler was not added to a ChannelPipeline yet
      • read

        public E read()
               throws java.io.IOException,
                      java.lang.InterruptedException
        Waits until a new message is received or the associated Channel is closed.
        Returns:
        the received message or null if the associated Channel has been closed
        Throws:
        java.io.IOException - if failed to receive a new message
        java.lang.InterruptedException - if the operation has been interrupted
      • read

        public E read​(long timeout,
                      java.util.concurrent.TimeUnit unit)
               throws java.io.IOException,
                      java.lang.InterruptedException
        Waits until a new message is received or the associated Channel is closed.
        Parameters:
        timeout - the amount time to wait until a new message is received. If no message is received within the timeout, BlockingReadTimeoutException is thrown.
        unit - the unit of timeout
        Returns:
        the received message or null if the associated Channel has been closed
        Throws:
        BlockingReadTimeoutException - if no message was received within the specified timeout
        java.io.IOException - if failed to receive a new message
        java.lang.InterruptedException - if the operation has been interrupted
      • readEvent

        public ChannelEvent readEvent()
                               throws java.lang.InterruptedException
        Waits until a new ChannelEvent is received or the associated Channel is closed.
        Returns:
        a MessageEvent or an ExceptionEvent. null if the associated Channel has been closed
        Throws:
        java.lang.InterruptedException - if the operation has been interrupted
      • detectDeadLock

        private static void detectDeadLock()