Package org.jboss.netty.handler.queue
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 aBlockingQueueand returns the received messages whenread(),read(long, TimeUnit),readEvent(), orreadEvent(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, andchannelClosedevents, hence it should be placed in the last place in a pipeline.Here is an example that demonstrates the usage:
BlockingReadHandler<ChannelBuffer> reader = newBlockingReadHandler<ChannelBuffer>();ChannelPipelinep = ...; p.addLast("reader", reader); ... // Read a message from a channel in a blocking manner. try {ChannelBufferbuf = reader.read(60, TimeUnit.SECONDS); if (buf == null) { // Connection closed. } else { // Handle the received message here. } } catch (BlockingReadTimeoutExceptione) { // Read timed out. } catch (IOException e) { // Other read errors }
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedprivate java.util.concurrent.BlockingQueue<ChannelEvent>queue
-
Constructor Summary
Constructors Constructor Description BlockingReadHandler()Creates a new instance withLinkedBlockingQueueBlockingReadHandler(java.util.concurrent.BlockingQueue<ChannelEvent> queue)Creates a new instance with the specifiedBlockingQueue.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidchannelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelwas closed and all its related resources were released.private static voiddetectDeadLock()voidexceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)Invoked when an exception was raised by an I/O thread or aChannelHandler.private EgetMessage(MessageEvent e)protected java.util.concurrent.BlockingQueue<ChannelEvent>getQueue()Returns the queue which stores the received messages.booleanisClosed()Returnstrueif and only if theChannelassociated with this handler has been closed.voidmessageReceived(ChannelHandlerContext ctx, MessageEvent e)Invoked when a message object (e.g:ChannelBuffer) was received from a remote peer.Eread()Waits until a new message is received or the associatedChannelis closed.Eread(long timeout, java.util.concurrent.TimeUnit unit)Waits until a new message is received or the associatedChannelis closed.ChannelEventreadEvent()Waits until a newChannelEventis received or the associatedChannelis closed.ChannelEventreadEvent(long timeout, java.util.concurrent.TimeUnit unit)Waits until a newChannelEventis received or the associatedChannelis closed.-
Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, handleUpstream, writeComplete
-
-
-
-
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 withLinkedBlockingQueue
-
BlockingReadHandler
public BlockingReadHandler(java.util.concurrent.BlockingQueue<ChannelEvent> queue)
Creates a new instance with the specifiedBlockingQueue.
-
-
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()
Returnstrueif and only if theChannelassociated with this handler has been closed.- Throws:
java.lang.IllegalStateException- if this handler was not added to aChannelPipelineyet
-
read
public E read() throws java.io.IOException, java.lang.InterruptedException
Waits until a new message is received or the associatedChannelis closed.- Returns:
- the received message or
nullif the associatedChannelhas been closed - Throws:
java.io.IOException- if failed to receive a new messagejava.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 associatedChannelis closed.- Parameters:
timeout- the amount time to wait until a new message is received. If no message is received within the timeout,BlockingReadTimeoutExceptionis thrown.unit- the unit oftimeout- Returns:
- the received message or
nullif the associatedChannelhas been closed - Throws:
BlockingReadTimeoutException- if no message was received within the specified timeoutjava.io.IOException- if failed to receive a new messagejava.lang.InterruptedException- if the operation has been interrupted
-
readEvent
public ChannelEvent readEvent() throws java.lang.InterruptedException
Waits until a newChannelEventis received or the associatedChannelis closed.- Returns:
- a
MessageEventor anExceptionEvent.nullif the associatedChannelhas been closed - Throws:
java.lang.InterruptedException- if the operation has been interrupted
-
readEvent
public ChannelEvent readEvent(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, BlockingReadTimeoutException
Waits until a newChannelEventis received or the associatedChannelis closed.- Parameters:
timeout- the amount time to wait until a newChannelEventis received. If no message is received within the timeout,BlockingReadTimeoutExceptionis thrown.unit- the unit oftimeout- Returns:
- a
MessageEventor anExceptionEvent.nullif the associatedChannelhas been closed - Throws:
BlockingReadTimeoutException- if no event was received within the specified timeoutjava.lang.InterruptedException- if the operation has been interrupted
-
detectDeadLock
private static void detectDeadLock()
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when a message object (e.g:ChannelBuffer) was received from a remote peer.- Overrides:
messageReceivedin classSimpleChannelUpstreamHandler- Throws:
java.lang.Exception
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when an exception was raised by an I/O thread or aChannelHandler.- Overrides:
exceptionCaughtin classSimpleChannelUpstreamHandler- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when aChannelwas closed and all its related resources were released.- Overrides:
channelClosedin classSimpleChannelUpstreamHandler- Throws:
java.lang.Exception
-
getMessage
private E getMessage(MessageEvent e)
-
-