Class EmbeddedChannel
- java.lang.Object
-
- io.netty.util.DefaultAttributeMap
-
- io.netty.channel.AbstractChannel
-
- io.netty.channel.embedded.EmbeddedChannel
-
- All Implemented Interfaces:
Channel,ChannelOutboundInvoker,AttributeMap,Comparable<Channel>
public class EmbeddedChannel extends AbstractChannel
Base class forChannelimplementations that are used in an embedded fashion.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.netty.channel.AbstractChannel
AbstractChannel.AbstractUnsafe
-
Nested classes/interfaces inherited from interface io.netty.channel.Channel
Channel.Unsafe
-
-
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 Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidcheckException()Check if there was anyThrowablereceived and if so rethrow it.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(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.booleanfinishAndReleaseAll()Mark thisChannelas finished and release all pending message in the inbound and outbound buffer.EmbeddedChannelflushInbound()Flushes the inbound of thisChannel.EmbeddedChannelflushOutbound()Flushes the outbound of thisChannel.protected voidhandleInboundMessage(Object msg)Called for each inbound message.protected voidhandleOutboundMessage(Object msg)Called for each outbound message.Queue<Object>inboundMessages()booleanisActive()Returntrueif theChannelis active and so connected.protected booleanisCompatible(EventLoop loop)Returntrueif the givenEventLoopis compatible with this instance.booleanisOpen()Returnstrueif theChannelis open and may get active laterQueue<Object>lastInboundBuffer()Deprecated.Queue<Object>lastOutboundBuffer()Deprecated.protected SocketAddresslocalAddress0()Returns theSocketAddresswhich is bound locally.ChannelMetadatametadata()protected DefaultChannelPipelinenewChannelPipeline()Returns a newDefaultChannelPipelineinstance.protected AbstractChannel.AbstractUnsafenewUnsafe()Create a newAbstractChannel.AbstractUnsafeinstance which will be used for the life-time of theChannelQueue<Object>outboundMessages()<T> TreadInbound()Return received data from thisChannel<T> TreadOutbound()Read data from the outbound.voidregister()Register thisChannelon itsEventLoop.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 SocketAddressremoteAddress0()Return theSocketAddresswhich theChannelis connected to.voidrunPendingTasks()longrunScheduledPendingTasks()Channel.Unsafeunsafe()Returns an internal-use-only object that provides unsafe operations.booleanwriteInbound(Object... msgs)Write messages to the inbound of thisChannel.ChannelFuturewriteOneInbound(Object msg)Writes one message to the inbound of thisChanneland does not flush it.ChannelFuturewriteOneInbound(Object msg, ChannelPromise promise)Writes one message to the inbound of thisChanneland does not flush it.ChannelFuturewriteOneOutbound(Object msg)Writes one message to the outbound of thisChanneland does not flush it.ChannelFuturewriteOneOutbound(Object msg, ChannelPromise promise)Writes one message to the outbound of thisChanneland does not flush it.booleanwriteOutbound(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, maxMessagesPerWrite, 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
-
-
-
-
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
-
register
public void register() throws ExceptionRegister thisChannelon itsEventLoop.- Throws:
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.
-
lastInboundBuffer
@Deprecated public Queue<Object> lastInboundBuffer()
Deprecated.
-
lastOutboundBuffer
@Deprecated public Queue<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(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(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(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()
-
writeOutbound
public boolean writeOutbound(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(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(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()
-
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
-
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.
-
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
-
runPendingTasks
public void runPendingTasks()
-
runScheduledPendingTasks
public long runScheduledPendingTasks()
-
checkException
public void checkException()
Check if there was anyThrowablereceived and if so rethrow it.
-
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 SocketAddress localAddress0()
Description copied from class:AbstractChannelReturns theSocketAddresswhich is bound locally.- Specified by:
localAddress0in classAbstractChannel
-
remoteAddress0
protected SocketAddress remoteAddress0()
Description copied from class:AbstractChannelReturn theSocketAddresswhich theChannelis connected to.- Specified by:
remoteAddress0in classAbstractChannel
-
doRegister
protected void doRegister() throws 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:
Exception
-
doBind
protected void doBind(SocketAddress localAddress) throws Exception
Description copied from class:AbstractChannelBind theChannelto theSocketAddress- Specified by:
doBindin classAbstractChannel- Throws:
Exception
-
doDisconnect
protected void doDisconnect() throws ExceptionDescription copied from class:AbstractChannelDisconnect thisChannelfrom its remote peer- Specified by:
doDisconnectin classAbstractChannel- Throws:
Exception
-
doClose
protected void doClose() throws ExceptionDescription copied from class:AbstractChannelClose theChannel- Specified by:
doClosein classAbstractChannel- Throws:
Exception
-
doBeginRead
protected void doBeginRead() throws ExceptionDescription copied from class:AbstractChannelSchedule a read operation.- Specified by:
doBeginReadin classAbstractChannel- Throws:
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 Exception
Description copied from class:AbstractChannelFlush the content of the given buffer to the remote peer.- Specified by:
doWritein classAbstractChannel- Throws:
Exception
-
handleOutboundMessage
protected void handleOutboundMessage(Object msg)
Called for each outbound message.- See Also:
doWrite(ChannelOutboundBuffer)
-
handleInboundMessage
protected void handleInboundMessage(Object msg)
Called for each inbound message.
-
-