|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.jboss.netty.channel.SimpleChannelUpstreamHandler
org.jboss.netty.handler.queue.BlockingReadHandler<E>
E - the type of the received messagespublic class BlockingReadHandler<E>
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 org.jboss.netty.channel.ChannelHandler |
|---|
ChannelHandler.Sharable |
| Constructor Summary | |
|---|---|
BlockingReadHandler()
Creates a new instance with the default unbounded BlockingQueue
implementation. |
|
BlockingReadHandler(java.util.concurrent.BlockingQueue<ChannelEvent> queue)
Creates a new instance with the specified BlockingQueue. |
|
| Method Summary | |
|---|---|
void |
channelClosed(ChannelHandlerContext ctx,
ChannelStateEvent e)
Invoked when a Channel was closed and all its related resources
were released. |
void |
exceptionCaught(ChannelHandlerContext ctx,
ExceptionEvent e)
Invoked when an exception was raised by an I/O thread or a ChannelHandler. |
protected java.util.concurrent.BlockingQueue<ChannelEvent> |
getQueue()
Returns the queue which stores the received messages. |
boolean |
isClosed()
Returns true if and only if the Channel associated with
this handler has been closed. |
void |
messageReceived(ChannelHandlerContext ctx,
MessageEvent e)
Invoked when a message object (e.g: ChannelBuffer) was received
from a remote peer. |
E |
read()
Waits until a new message is received or the associated Channel
is closed. |
E |
read(long timeout,
java.util.concurrent.TimeUnit unit)
Waits until a new message is received or the associated Channel
is closed. |
ChannelEvent |
readEvent()
Waits until a new ChannelEvent is received or the associated
Channel is closed. |
ChannelEvent |
readEvent(long timeout,
java.util.concurrent.TimeUnit unit)
Waits until a new ChannelEvent is received or the associated
Channel is closed. |
| Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler |
|---|
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, handleUpstream, writeComplete |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public BlockingReadHandler()
BlockingQueue
implementation.
public BlockingReadHandler(java.util.concurrent.BlockingQueue<ChannelEvent> queue)
BlockingQueue.
| Method Detail |
|---|
protected java.util.concurrent.BlockingQueue<ChannelEvent> getQueue()
public boolean isClosed()
true if and only if the Channel associated with
this handler has been closed.
java.lang.IllegalStateException - if this handler was not added to a ChannelPipeline yet
public E read()
throws java.io.IOException,
java.lang.InterruptedException
Channel
is closed.
null if the associated
Channel has been closed
java.io.IOException - if failed to receive a new message
java.lang.InterruptedException - if the operation has been interrupted
public E read(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.io.IOException,
java.lang.InterruptedException
Channel
is closed.
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
null if the associated
Channel has been closed
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
public ChannelEvent readEvent()
throws java.lang.InterruptedException
ChannelEvent is received or the associated
Channel is closed.
MessageEvent or an ExceptionEvent.
null if the associated Channel has been closed
java.lang.InterruptedException - if the operation has been interrupted
public ChannelEvent readEvent(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.lang.InterruptedException,
BlockingReadTimeoutException
ChannelEvent is received or the associated
Channel is closed.
timeout - the amount time to wait until a new ChannelEvent is
received. If no message is received within the timeout,
BlockingReadTimeoutException is thrown.unit - the unit of timeout
MessageEvent or an ExceptionEvent.
null if the associated Channel has been closed
BlockingReadTimeoutException - if no event was received within the specified timeout
java.lang.InterruptedException - if the operation has been interrupted
public void messageReceived(ChannelHandlerContext ctx,
MessageEvent e)
throws java.lang.Exception
SimpleChannelUpstreamHandlerChannelBuffer) was received
from a remote peer.
messageReceived in class SimpleChannelUpstreamHandlerjava.lang.Exception
public void exceptionCaught(ChannelHandlerContext ctx,
ExceptionEvent e)
throws java.lang.Exception
SimpleChannelUpstreamHandlerChannelHandler.
exceptionCaught in class SimpleChannelUpstreamHandlerjava.lang.Exception
public void channelClosed(ChannelHandlerContext ctx,
ChannelStateEvent e)
throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel was closed and all its related resources
were released.
channelClosed in class SimpleChannelUpstreamHandlerjava.lang.Exception
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||