E
- the type of the received messagespublic class BlockingReadHandler<E> extends SimpleChannelUpstreamHandler
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
>();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 }
ChannelHandler.Sharable
Constructor and Description |
---|
BlockingReadHandler()
Creates a new instance with
LinkedBlockingQueue |
BlockingReadHandler(BlockingQueue<ChannelEvent> queue)
Creates a new instance with the specified
BlockingQueue . |
Modifier and Type | Method and Description |
---|---|
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 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,
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,
TimeUnit unit)
Waits until a new
ChannelEvent is received or the associated
Channel is closed. |
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, handleUpstream, writeComplete
public BlockingReadHandler()
LinkedBlockingQueue
public BlockingReadHandler(BlockingQueue<ChannelEvent> queue)
BlockingQueue
.protected BlockingQueue<ChannelEvent> getQueue()
public boolean isClosed()
true
if and only if the Channel
associated with
this handler has been closed.IllegalStateException
- if this handler was not added to a ChannelPipeline
yetpublic E read() throws IOException, InterruptedException
Channel
is closed.null
if the associated
Channel
has been closedIOException
- if failed to receive a new messageInterruptedException
- if the operation has been interruptedpublic E read(long timeout, TimeUnit unit) throws IOException, 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 closedBlockingReadTimeoutException
- if no message was received within the specified timeoutIOException
- if failed to receive a new messageInterruptedException
- if the operation has been interruptedpublic ChannelEvent readEvent() throws InterruptedException
ChannelEvent
is received or the associated
Channel
is closed.MessageEvent
or an ExceptionEvent
.
null
if the associated Channel
has been closedInterruptedException
- if the operation has been interruptedpublic ChannelEvent readEvent(long timeout, TimeUnit unit) throws 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 closedBlockingReadTimeoutException
- if no event was received within the specified timeoutInterruptedException
- if the operation has been interruptedpublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
SimpleChannelUpstreamHandler
ChannelBuffer
) was received
from a remote peer.messageReceived
in class SimpleChannelUpstreamHandler
Exception
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception
SimpleChannelUpstreamHandler
ChannelHandler
.exceptionCaught
in class SimpleChannelUpstreamHandler
Exception
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
SimpleChannelUpstreamHandler
Channel
was closed and all its related resources
were released.channelClosed
in class SimpleChannelUpstreamHandler
Exception
Copyright © 2008-2016 The Netty Project. All Rights Reserved.