Class EmbeddedChannel
- java.lang.Object
-
- io.netty.util.DefaultAttributeMap
-
- io.netty.channel.AbstractChannel
-
- io.netty.channel.embedded.EmbeddedChannel
-
- All Implemented Interfaces:
Channel,ChannelOutboundInvoker,AttributeMap,java.lang.Comparable<Channel>
public class EmbeddedChannel extends AbstractChannel
Base class forChannelimplementations that are used in an embedded fashion.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classEmbeddedChannel.EmbeddedChannelPipelineprivate classEmbeddedChannel.EmbeddedUnsafeprivate static classEmbeddedChannel.State-
Nested classes/interfaces inherited from class io.netty.channel.AbstractChannel
AbstractChannel.AbstractUnsafe
-
Nested classes/interfaces inherited from interface io.netty.channel.Channel
Channel.Unsafe
-
-
Field Summary
Fields Modifier and Type Field Description private ChannelConfigconfigprivate static ChannelHandler[]EMPTY_HANDLERSprivate java.util.Queue<java.lang.Object>inboundMessagesprivate java.lang.ThrowablelastExceptionprivate static java.net.SocketAddressLOCAL_ADDRESSprivate static InternalLoggerloggerprivate EmbeddedEventLooploopprivate ChannelMetadatametadataprivate static ChannelMetadataMETADATA_DISCONNECTprivate static ChannelMetadataMETADATA_NO_DISCONNECTprivate java.util.Queue<java.lang.Object>outboundMessagesprivate ChannelFutureListenerrecordExceptionListenerprivate static java.net.SocketAddressREMOTE_ADDRESSprivate EmbeddedChannel.Statestate
-
Constructor Summary
Constructors Constructor Description EmbeddedChannel()Create a new instance with anEmbeddedChannelIdand an empty pipeline.EmbeddedChannel(boolean register, boolean hasDisconnect, ChannelHandler... handlers)Create a new instance with the pipeline initialized with the specified handlers.EmbeddedChannel(boolean hasDisconnect, ChannelHandler... handlers)Create a new instance with the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelHandler... handlers)Create a new instance with the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId)Create a new instance with the specified ID and an empty pipeline.EmbeddedChannel(ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelConfig config, ChannelHandler... handlers)Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelHandler... handlers)Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId, ChannelHandler... handlers)Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(Channel parent, ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidcheckException()Check if there was anyThrowablereceived and if so rethrow it.private ChannelFuturecheckException(ChannelPromise promise)Checks for the presence of anException.private booleancheckOpen(boolean recordException)ChannelFutureclose()Request to close theChanneland notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error.ChannelFutureclose(ChannelPromise promise)Request to close theChanneland notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error.ChannelConfigconfig()Returns the configuration of this channel.ChannelFuturedisconnect()Request to disconnect from the remote peer and notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error.ChannelFuturedisconnect(ChannelPromise promise)Request to disconnect from the remote peer and notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error.protected voiddoBeginRead()Schedule a read operation.protected voiddoBind(java.net.SocketAddress localAddress)Bind theChannelto theSocketAddressprotected voiddoClose()Close theChannelprotected voiddoDisconnect()Disconnect thisChannelfrom its remote peerprotected voiddoRegister()protected voiddoWrite(ChannelOutboundBuffer in)Flush the content of the given buffer to the remote peer.protected voidensureOpen()Ensure theChannelis open and if not throw an exception.booleanfinish()Mark thisChannelas finished.private booleanfinish(boolean releaseAll)Mark thisChannelas finished.booleanfinishAndReleaseAll()Mark thisChannelas finished and release all pending message in the inbound and outbound buffer.private voidfinishPendingTasks(boolean cancel)EmbeddedChannelflushInbound()Flushes the inbound of thisChannel.private ChannelFutureflushInbound(boolean recordException, ChannelPromise promise)EmbeddedChannelflushOutbound()Flushes the outbound of thisChannel.private voidflushOutbound0()protected voidhandleInboundMessage(java.lang.Object msg)Called for each inbound message.protected voidhandleOutboundMessage(java.lang.Object msg)Called for each outbound message.java.util.Queue<java.lang.Object>inboundMessages()booleanisActive()Returntrueif theChannelis active and so connected.protected booleanisCompatible(EventLoop loop)Returntrueif the givenEventLoopis compatible with this instance.private static booleanisNotEmpty(java.util.Queue<java.lang.Object> queue)booleanisOpen()Returnstrueif theChannelis open and may get active laterjava.util.Queue<java.lang.Object>lastInboundBuffer()Deprecated.java.util.Queue<java.lang.Object>lastOutboundBuffer()Deprecated.protected java.net.SocketAddresslocalAddress0()Returns theSocketAddresswhich is bound locally.ChannelMetadatametadata()private static ChannelMetadatametadata(boolean hasDisconnect)protected DefaultChannelPipelinenewChannelPipeline()Returns a newDefaultChannelPipelineinstance.protected AbstractChannel.AbstractUnsafenewUnsafe()Create a newAbstractChannel.AbstractUnsafeinstance which will be used for the life-time of theChanneljava.util.Queue<java.lang.Object>outboundMessages()private static java.lang.Objectpoll(java.util.Queue<java.lang.Object> queue)<T> TreadInbound()Return received data from thisChannel<T> TreadOutbound()Read data from the outbound.private voidrecordException(ChannelFuture future)private voidrecordException(java.lang.Throwable cause)voidregister()Register thisChannelon itsEventLoop.private static booleanreleaseAll(java.util.Queue<java.lang.Object> queue)booleanreleaseInbound()Release all buffered inbound messages and returntrueif any were in the inbound buffer,falseotherwise.booleanreleaseOutbound()Release all buffered outbound messages and returntrueif any were in the outbound buffer,falseotherwise.protected java.net.SocketAddressremoteAddress0()Return theSocketAddresswhich theChannelis connected to.voidrunPendingTasks()longrunScheduledPendingTasks()private voidsetup(boolean register, ChannelHandler... handlers)Channel.Unsafeunsafe()Returns an internal-use-only object that provides unsafe operations.booleanwriteInbound(java.lang.Object... msgs)Write messages to the inbound of thisChannel.ChannelFuturewriteOneInbound(java.lang.Object msg)Writes one message to the inbound of thisChanneland does not flush it.ChannelFuturewriteOneInbound(java.lang.Object msg, ChannelPromise promise)Writes one message to the inbound of thisChanneland does not flush it.ChannelFuturewriteOneOutbound(java.lang.Object msg)Writes one message to the outbound of thisChanneland does not flush it.ChannelFuturewriteOneOutbound(java.lang.Object msg, ChannelPromise promise)Writes one message to the outbound of thisChanneland does not flush it.booleanwriteOutbound(java.lang.Object... msgs)Write messages to the outbound of thisChannel.-
Methods inherited from class io.netty.channel.AbstractChannel
alloc, bind, bind, bytesBeforeUnwritable, bytesBeforeWritable, closeFuture, compareTo, connect, connect, connect, connect, deregister, deregister, doDeregister, doShutdownOutput, equals, eventLoop, filterOutboundMessage, flush, hashCode, id, invalidateLocalAddress, invalidateRemoteAddress, isRegistered, isWritable, localAddress, newFailedFuture, newId, newProgressivePromise, newPromise, newSucceededFuture, parent, pipeline, read, remoteAddress, toString, validateFileRegion, voidPromise, write, write, writeAndFlush, writeAndFlush
-
Methods inherited from class io.netty.util.DefaultAttributeMap
attr, hasAttr
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.netty.util.AttributeMap
attr, hasAttr
-
-
-
-
Field Detail
-
LOCAL_ADDRESS
private static final java.net.SocketAddress LOCAL_ADDRESS
-
REMOTE_ADDRESS
private static final java.net.SocketAddress REMOTE_ADDRESS
-
EMPTY_HANDLERS
private static final ChannelHandler[] EMPTY_HANDLERS
-
logger
private static final InternalLogger logger
-
METADATA_NO_DISCONNECT
private static final ChannelMetadata METADATA_NO_DISCONNECT
-
METADATA_DISCONNECT
private static final ChannelMetadata METADATA_DISCONNECT
-
loop
private final EmbeddedEventLoop loop
-
recordExceptionListener
private final ChannelFutureListener recordExceptionListener
-
metadata
private final ChannelMetadata metadata
-
config
private final ChannelConfig config
-
inboundMessages
private java.util.Queue<java.lang.Object> inboundMessages
-
outboundMessages
private java.util.Queue<java.lang.Object> outboundMessages
-
lastException
private java.lang.Throwable lastException
-
state
private EmbeddedChannel.State state
-
-
Constructor Detail
-
EmbeddedChannel
public EmbeddedChannel()
Create a new instance with anEmbeddedChannelIdand an empty pipeline.
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId)
Create a new instance with the specified ID and an empty pipeline.- Parameters:
channelId- theChannelIdthat will be used to identify this channel
-
EmbeddedChannel
public EmbeddedChannel(ChannelHandler... handlers)
Create a new instance with the pipeline initialized with the specified handlers.- Parameters:
handlers- theChannelHandlers which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(boolean hasDisconnect, ChannelHandler... handlers)Create a new instance with the pipeline initialized with the specified handlers.- Parameters:
hasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(boolean register, boolean hasDisconnect, ChannelHandler... handlers)Create a new instance with the pipeline initialized with the specified handlers.- Parameters:
register-trueif thisChannelis registered to theEventLoopin the constructor. Iffalsethe user will need to callregister().hasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId- theChannelIdthat will be used to identify this channelhandlers- theChannelHandlers which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId- theChannelIdthat will be used to identify this channelhasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId- theChannelIdthat will be used to identify this channelregister-trueif thisChannelis registered to theEventLoopin the constructor. Iffalsethe user will need to callregister().hasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(Channel parent, ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
parent- the parentChannelof thisEmbeddedChannel.channelId- theChannelIdthat will be used to identify this channelregister-trueif thisChannelis registered to theEventLoopin the constructor. Iffalsethe user will need to callregister().hasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelConfig config, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId- theChannelIdthat will be used to identify this channelhasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.config- theChannelConfigwhich will be returned byconfig().handlers- theChannelHandlers which will be add in theChannelPipeline
-
-
Method Detail
-
metadata
private static ChannelMetadata metadata(boolean hasDisconnect)
-
setup
private void setup(boolean register, ChannelHandler... handlers)
-
register
public void register() throws java.lang.ExceptionRegister thisChannelon itsEventLoop.- Throws:
java.lang.Exception
-
newChannelPipeline
protected final DefaultChannelPipeline newChannelPipeline()
Description copied from class:AbstractChannelReturns a newDefaultChannelPipelineinstance.- Overrides:
newChannelPipelinein classAbstractChannel
-
metadata
public ChannelMetadata metadata()
Description copied from interface:Channel
-
config
public ChannelConfig config()
Description copied from interface:ChannelReturns the configuration of this channel.
-
isOpen
public boolean isOpen()
Description copied from interface:ChannelReturnstrueif theChannelis open and may get active later
-
isActive
public boolean isActive()
Description copied from interface:ChannelReturntrueif theChannelis active and so connected.
-
inboundMessages
public java.util.Queue<java.lang.Object> inboundMessages()
-
lastInboundBuffer
@Deprecated public java.util.Queue<java.lang.Object> lastInboundBuffer()
Deprecated.
-
outboundMessages
public java.util.Queue<java.lang.Object> outboundMessages()
-
lastOutboundBuffer
@Deprecated public java.util.Queue<java.lang.Object> lastOutboundBuffer()
Deprecated.
-
readInbound
public <T> T readInbound()
Return received data from thisChannel
-
readOutbound
public <T> T readOutbound()
Read data from the outbound. This may returnnullif nothing is readable.
-
writeInbound
public boolean writeInbound(java.lang.Object... msgs)
Write messages to the inbound of thisChannel.- Parameters:
msgs- the messages to be written- Returns:
trueif the write operation did add something to the inbound buffer
-
writeOneInbound
public ChannelFuture writeOneInbound(java.lang.Object msg)
Writes one message to the inbound of thisChanneland does not flush it. This method is conceptually equivalent toAbstractChannel.write(Object).- See Also:
writeOneOutbound(Object)
-
writeOneInbound
public ChannelFuture writeOneInbound(java.lang.Object msg, ChannelPromise promise)
Writes one message to the inbound of thisChanneland does not flush it. This method is conceptually equivalent toAbstractChannel.write(Object, ChannelPromise).- See Also:
writeOneOutbound(Object, ChannelPromise)
-
flushInbound
public EmbeddedChannel flushInbound()
Flushes the inbound of thisChannel. This method is conceptually equivalent toAbstractChannel.flush().- See Also:
flushOutbound()
-
flushInbound
private ChannelFuture flushInbound(boolean recordException, ChannelPromise promise)
-
writeOutbound
public boolean writeOutbound(java.lang.Object... msgs)
Write messages to the outbound of thisChannel.- Parameters:
msgs- the messages to be written- Returns:
- bufferReadable returns
trueif the write operation did add something to the outbound buffer
-
writeOneOutbound
public ChannelFuture writeOneOutbound(java.lang.Object msg)
Writes one message to the outbound of thisChanneland does not flush it. This method is conceptually equivalent toAbstractChannel.write(Object).- See Also:
writeOneInbound(Object)
-
writeOneOutbound
public ChannelFuture writeOneOutbound(java.lang.Object msg, ChannelPromise promise)
Writes one message to the outbound of thisChanneland does not flush it. This method is conceptually equivalent toAbstractChannel.write(Object, ChannelPromise).- See Also:
writeOneInbound(Object, ChannelPromise)
-
flushOutbound
public EmbeddedChannel flushOutbound()
Flushes the outbound of thisChannel. This method is conceptually equivalent toAbstractChannel.flush().- See Also:
flushInbound()
-
flushOutbound0
private void flushOutbound0()
-
finish
public boolean finish()
Mark thisChannelas finished. Any further try to write data to it will fail.- Returns:
- bufferReadable returns
trueif any of the used buffers has something left to read
-
finishAndReleaseAll
public boolean finishAndReleaseAll()
Mark thisChannelas finished and release all pending message in the inbound and outbound buffer. Any further try to write data to it will fail.- Returns:
- bufferReadable returns
trueif any of the used buffers has something left to read
-
finish
private boolean finish(boolean releaseAll)
Mark thisChannelas finished. Any further try to write data to it will fail.- Parameters:
releaseAll- iftrueall pending message in the inbound and outbound buffer are released.- Returns:
- bufferReadable returns
trueif any of the used buffers has something left to read
-
releaseInbound
public boolean releaseInbound()
Release all buffered inbound messages and returntrueif any were in the inbound buffer,falseotherwise.
-
releaseOutbound
public boolean releaseOutbound()
Release all buffered outbound messages and returntrueif any were in the outbound buffer,falseotherwise.
-
releaseAll
private static boolean releaseAll(java.util.Queue<java.lang.Object> queue)
-
finishPendingTasks
private void finishPendingTasks(boolean cancel)
-
close
public final ChannelFuture close()
Description copied from interface:ChannelOutboundInvokerRequest to close theChanneland notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error. After it is closed it is not possible to reuse it again.This will result in having the
ChannelOutboundHandler.close(ChannelHandlerContext, ChannelPromise)method called of the nextChannelOutboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
closein interfaceChannelOutboundInvoker- Overrides:
closein classAbstractChannel
-
disconnect
public final ChannelFuture disconnect()
Description copied from interface:ChannelOutboundInvokerRequest to disconnect from the remote peer and notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelOutboundHandler.disconnect(ChannelHandlerContext, ChannelPromise)method called of the nextChannelOutboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
disconnectin interfaceChannelOutboundInvoker- Overrides:
disconnectin classAbstractChannel
-
close
public final ChannelFuture close(ChannelPromise promise)
Description copied from interface:ChannelOutboundInvokerRequest to close theChanneland notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error. After it is closed it is not possible to reuse it again. The givenChannelPromisewill be notified.This will result in having the
ChannelOutboundHandler.close(ChannelHandlerContext, ChannelPromise)method called of the nextChannelOutboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
closein interfaceChannelOutboundInvoker- Overrides:
closein classAbstractChannel
-
disconnect
public final ChannelFuture disconnect(ChannelPromise promise)
Description copied from interface:ChannelOutboundInvokerRequest to disconnect from the remote peer and notify theChannelFutureonce the operation completes, either because the operation was successful or because of an error. The givenChannelPromisewill be notified.This will result in having the
ChannelOutboundHandler.disconnect(ChannelHandlerContext, ChannelPromise)method called of the nextChannelOutboundHandlercontained in theChannelPipelineof theChannel.- Specified by:
disconnectin interfaceChannelOutboundInvoker- Overrides:
disconnectin classAbstractChannel
-
isNotEmpty
private static boolean isNotEmpty(java.util.Queue<java.lang.Object> queue)
-
poll
private static java.lang.Object poll(java.util.Queue<java.lang.Object> queue)
-
runPendingTasks
public void runPendingTasks()
-
runScheduledPendingTasks
public long runScheduledPendingTasks()
-
recordException
private void recordException(ChannelFuture future)
-
recordException
private void recordException(java.lang.Throwable cause)
-
checkException
private ChannelFuture checkException(ChannelPromise promise)
Checks for the presence of anException.
-
checkException
public void checkException()
Check if there was anyThrowablereceived and if so rethrow it.
-
checkOpen
private boolean checkOpen(boolean recordException)
-
ensureOpen
protected final void ensureOpen()
Ensure theChannelis open and if not throw an exception.
-
isCompatible
protected boolean isCompatible(EventLoop loop)
Description copied from class:AbstractChannelReturntrueif the givenEventLoopis compatible with this instance.- Specified by:
isCompatiblein classAbstractChannel
-
localAddress0
protected java.net.SocketAddress localAddress0()
Description copied from class:AbstractChannelReturns theSocketAddresswhich is bound locally.- Specified by:
localAddress0in classAbstractChannel
-
remoteAddress0
protected java.net.SocketAddress remoteAddress0()
Description copied from class:AbstractChannelReturn theSocketAddresswhich theChannelis connected to.- Specified by:
remoteAddress0in classAbstractChannel
-
doRegister
protected void doRegister() throws java.lang.ExceptionDescription copied from class:AbstractChannelIs called after theChannelis registered with itsEventLoopas part of the register process. Sub-classes may override this method- Overrides:
doRegisterin classAbstractChannel- Throws:
java.lang.Exception
-
doBind
protected void doBind(java.net.SocketAddress localAddress) throws java.lang.ExceptionDescription copied from class:AbstractChannelBind theChannelto theSocketAddress- Specified by:
doBindin classAbstractChannel- Throws:
java.lang.Exception
-
doDisconnect
protected void doDisconnect() throws java.lang.ExceptionDescription copied from class:AbstractChannelDisconnect thisChannelfrom its remote peer- Specified by:
doDisconnectin classAbstractChannel- Throws:
java.lang.Exception
-
doClose
protected void doClose() throws java.lang.ExceptionDescription copied from class:AbstractChannelClose theChannel- Specified by:
doClosein classAbstractChannel- Throws:
java.lang.Exception
-
doBeginRead
protected void doBeginRead() throws java.lang.ExceptionDescription copied from class:AbstractChannelSchedule a read operation.- Specified by:
doBeginReadin classAbstractChannel- Throws:
java.lang.Exception
-
newUnsafe
protected AbstractChannel.AbstractUnsafe newUnsafe()
Description copied from class:AbstractChannelCreate a newAbstractChannel.AbstractUnsafeinstance which will be used for the life-time of theChannel- Specified by:
newUnsafein classAbstractChannel
-
unsafe
public Channel.Unsafe unsafe()
Description copied from interface:ChannelReturns an internal-use-only object that provides unsafe operations.- Specified by:
unsafein interfaceChannel- Overrides:
unsafein classAbstractChannel
-
doWrite
protected void doWrite(ChannelOutboundBuffer in) throws java.lang.Exception
Description copied from class:AbstractChannelFlush the content of the given buffer to the remote peer.- Specified by:
doWritein classAbstractChannel- Throws:
java.lang.Exception
-
handleOutboundMessage
protected void handleOutboundMessage(java.lang.Object msg)
Called for each outbound message.- See Also:
doWrite(ChannelOutboundBuffer)
-
handleInboundMessage
protected void handleInboundMessage(java.lang.Object msg)
Called for each inbound message.
-
-