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
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 = 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 ChannelHandler
ChannelHandler.Sharable -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance withLinkedBlockingQueueCreates a new instance with the specifiedBlockingQueue. -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked when aChannelwas closed and all its related resources were released.private static voidvoidInvoked when an exception was raised by an I/O thread or aChannelHandler.private Eprotected BlockingQueue<ChannelEvent> getQueue()Returns the queue which stores the received messages.booleanisClosed()Returnstrueif and only if theChannelassociated with this handler has been closed.voidInvoked when a message object (e.g:ChannelBuffer) was received from a remote peer.read()Waits until a new message is received or the associatedChannelis closed.Waits until a new message is received or the associatedChannelis closed.Waits until a newChannelEventis received or the associatedChannelis closed.Waits until a newChannelEventis received or the associatedChannelis closed.Methods inherited from class SimpleChannelUpstreamHandler
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, handleUpstream, writeComplete
-
Field Details
-
queue
-
closed
private volatile boolean closed
-
-
Constructor Details
-
BlockingReadHandler
public BlockingReadHandler()Creates a new instance withLinkedBlockingQueue -
BlockingReadHandler
Creates a new instance with the specifiedBlockingQueue.
-
-
Method Details
-
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:
IllegalStateException- if this handler was not added to aChannelPipelineyet
-
read
Waits until a new message is received or the associatedChannelis closed.- Returns:
- the received message or
nullif the associatedChannelhas been closed - Throws:
IOException- if failed to receive a new messageInterruptedException- if the operation has been interrupted
-
read
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 timeoutIOException- if failed to receive a new messageInterruptedException- if the operation has been interrupted
-
readEvent
Waits until a newChannelEventis received or the associatedChannelis closed.- Returns:
- a
MessageEventor anExceptionEvent.nullif the associatedChannelhas been closed - Throws:
InterruptedException- if the operation has been interrupted
-
readEvent
public ChannelEvent readEvent(long timeout, TimeUnit unit) throws 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 timeoutInterruptedException- if the operation has been interrupted
-
detectDeadLock
private static void detectDeadLock() -
messageReceived
Description copied from class:SimpleChannelUpstreamHandlerInvoked when a message object (e.g:ChannelBuffer) was received from a remote peer.- Overrides:
messageReceivedin classSimpleChannelUpstreamHandler- Throws:
Exception
-
exceptionCaught
Description copied from class:SimpleChannelUpstreamHandlerInvoked when an exception was raised by an I/O thread or aChannelHandler.- Overrides:
exceptionCaughtin classSimpleChannelUpstreamHandler- Throws:
Exception
-
channelClosed
Description copied from class:SimpleChannelUpstreamHandlerInvoked when aChannelwas closed and all its related resources were released.- Overrides:
channelClosedin classSimpleChannelUpstreamHandler- Throws:
Exception
-
getMessage
-