- java.lang.Object
-
- org.xnio.conduits.ConduitWritableMessageChannel
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Cloneable
,java.nio.channels.Channel
,java.nio.channels.InterruptibleChannel
,CloseableChannel
,CloseListenerSettable<ConduitWritableMessageChannel>
,Configurable
,SuspendableWriteChannel
,WritableMessageChannel
,WriteListenerSettable<ConduitWritableMessageChannel>
public final class ConduitWritableMessageChannel extends java.lang.Object implements WritableMessageChannel, WriteListenerSettable<ConduitWritableMessageChannel>, CloseListenerSettable<ConduitWritableMessageChannel>, java.lang.Cloneable
A writable message channel which is backed by a message sink conduit.- Author:
- David M. Lloyd
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.xnio.channels.CloseListenerSettable
CloseListenerSettable.Setter<C extends java.nio.channels.Channel>
-
Nested classes/interfaces inherited from interface org.xnio.channels.WriteListenerSettable
WriteListenerSettable.Setter<C extends java.nio.channels.Channel>
-
-
Field Summary
-
Fields inherited from interface org.xnio.channels.Configurable
EMPTY
-
-
Constructor Summary
Constructors Constructor Description ConduitWritableMessageChannel(Configurable configurable, MessageSinkConduit conduit)
Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
awaitWritable()
Block until this channel becomes writable again.void
awaitWritable(long time, java.util.concurrent.TimeUnit timeUnit)
Block until this channel becomes writable again, or until the timeout expires.ConduitWritableMessageChannel
clone()
Duplicate this channel.void
close()
Close this channel.boolean
flush()
Flush any waiting partial send or write.ChannelListener<? super ConduitWritableMessageChannel>
getCloseListener()
Get the close listener.ChannelListener.Setter<ConduitWritableMessageChannel>
getCloseSetter()
Get the setter which can be used to change the close listener for this channel.MessageSinkConduit
getConduit()
Get the underlying conduit for this channel.XnioIoThread
getIoThread()
Get the I/O thread associated with this channel.<T> T
getOption(Option<T> option)
Get the value of a channel option.XnioWorker
getWorker()
Get the worker for this channel.ChannelListener<? super ConduitWritableMessageChannel>
getWriteListener()
Get the write listener.ChannelListener.Setter<ConduitWritableMessageChannel>
getWriteSetter()
Get the setter which can be used to change the write listener for this channel.XnioExecutor
getWriteThread()
Deprecated.boolean
isOpen()
Determine whether this channel is open.boolean
isWriteResumed()
Determine whether writes are resumed.void
resumeWrites()
Resume writes on this channel.boolean
send(java.nio.ByteBuffer dst)
Send a complete message.boolean
send(java.nio.ByteBuffer[] srcs)
Send a complete message.boolean
send(java.nio.ByteBuffer[] dsts, int offs, int len)
Send a complete message.boolean
sendFinal(java.nio.ByteBuffer buffer)
Send a complete message.boolean
sendFinal(java.nio.ByteBuffer[] buffers)
Send a complete message.boolean
sendFinal(java.nio.ByteBuffer[] buffers, int offs, int len)
Send a complete message.void
setCloseListener(ChannelListener<? super ConduitWritableMessageChannel> closeListener)
Set the close listener.void
setConduit(MessageSinkConduit conduit)
Set the underlying conduit for this channel.<T> T
setOption(Option<T> option, T value)
Set an option for this channel.void
setWriteListener(ChannelListener<? super ConduitWritableMessageChannel> writeListener)
Set the write listener.void
shutdownWrites()
Indicate that writing is complete for this channel.boolean
supportsOption(Option<?> option)
Determine whether an option is supported on this channel.void
suspendWrites()
Suspend further write notifications on this channel.void
wakeupWrites()
Resume writes
on this channel, and force the write listener to be triggered even if the channel isn't actually writable.
-
-
-
Constructor Detail
-
ConduitWritableMessageChannel
public ConduitWritableMessageChannel(Configurable configurable, MessageSinkConduit conduit)
Construct a new instance.- Parameters:
configurable
- the configurable to delegate configuration requests toconduit
- the initial conduit to use for data transport
-
-
Method Detail
-
getConduit
public MessageSinkConduit getConduit()
Get the underlying conduit for this channel.- Returns:
- the underlying conduit for this channel
-
setConduit
public void setConduit(MessageSinkConduit conduit)
Set the underlying conduit for this channel.- Parameters:
conduit
- the underlying conduit for this channel
-
getWriteListener
public ChannelListener<? super ConduitWritableMessageChannel> getWriteListener()
Description copied from interface:WriteListenerSettable
Get the write listener.- Specified by:
getWriteListener
in interfaceWriteListenerSettable<ConduitWritableMessageChannel>
- Returns:
- the write listener
-
setWriteListener
public void setWriteListener(ChannelListener<? super ConduitWritableMessageChannel> writeListener)
Description copied from interface:WriteListenerSettable
Set the write listener.- Specified by:
setWriteListener
in interfaceWriteListenerSettable<ConduitWritableMessageChannel>
- Parameters:
writeListener
- the write listener
-
getCloseListener
public ChannelListener<? super ConduitWritableMessageChannel> getCloseListener()
Description copied from interface:CloseListenerSettable
Get the close listener.- Specified by:
getCloseListener
in interfaceCloseListenerSettable<ConduitWritableMessageChannel>
- Returns:
- the close listener
-
setCloseListener
public void setCloseListener(ChannelListener<? super ConduitWritableMessageChannel> closeListener)
Description copied from interface:CloseListenerSettable
Set the close listener.- Specified by:
setCloseListener
in interfaceCloseListenerSettable<ConduitWritableMessageChannel>
- Parameters:
closeListener
- the close listener
-
getWriteSetter
public ChannelListener.Setter<ConduitWritableMessageChannel> getWriteSetter()
Description copied from interface:WritableMessageChannel
Get the setter which can be used to change the write listener for this channel.- Specified by:
getWriteSetter
in interfaceSuspendableWriteChannel
- Specified by:
getWriteSetter
in interfaceWritableMessageChannel
- Returns:
- the setter
-
getCloseSetter
public ChannelListener.Setter<ConduitWritableMessageChannel> getCloseSetter()
Description copied from interface:WritableMessageChannel
Get the setter which can be used to change the close listener for this channel. If the channel is already closed, then the listener will not be called.- Specified by:
getCloseSetter
in interfaceCloseableChannel
- Specified by:
getCloseSetter
in interfaceSuspendableWriteChannel
- Specified by:
getCloseSetter
in interfaceWritableMessageChannel
- Returns:
- the setter
-
suspendWrites
public void suspendWrites()
Description copied from interface:SuspendableWriteChannel
Suspend further write notifications on this channel.- Specified by:
suspendWrites
in interfaceSuspendableWriteChannel
-
resumeWrites
public void resumeWrites()
Description copied from interface:SuspendableWriteChannel
Resume writes on this channel. The write listener will be called as soon as the channel becomes writable.- Specified by:
resumeWrites
in interfaceSuspendableWriteChannel
-
wakeupWrites
public void wakeupWrites()
Description copied from interface:SuspendableWriteChannel
Resume writes
on this channel, and force the write listener to be triggered even if the channel isn't actually writable.- Specified by:
wakeupWrites
in interfaceSuspendableWriteChannel
-
isWriteResumed
public boolean isWriteResumed()
Description copied from interface:SuspendableWriteChannel
Determine whether writes are resumed.- Specified by:
isWriteResumed
in interfaceSuspendableWriteChannel
- Returns:
true
if writes are resumed,false
if writes are suspended
-
awaitWritable
public void awaitWritable() throws java.io.IOException
Description copied from interface:SuspendableWriteChannel
Block until this channel becomes writable again. This method may return spuriously before the channel becomes writable.- Specified by:
awaitWritable
in interfaceSuspendableWriteChannel
- Throws:
java.io.InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as welljava.io.IOException
- if an I/O error occurs
-
awaitWritable
public void awaitWritable(long time, java.util.concurrent.TimeUnit timeUnit) throws java.io.IOException
Description copied from interface:SuspendableWriteChannel
Block until this channel becomes writable again, or until the timeout expires. This method may return spuriously before the channel becomes writable or the timeout expires.- Specified by:
awaitWritable
in interfaceSuspendableWriteChannel
- Parameters:
time
- the time to waittimeUnit
- the time unit- Throws:
java.io.InterruptedIOException
- if the operation is interrupted; the thread's interrupt flag will be set as welljava.io.IOException
- if an I/O error occurs
-
send
public boolean send(java.nio.ByteBuffer dst) throws java.io.IOException
Description copied from interface:WritableMessageChannel
Send a complete message.- Specified by:
send
in interfaceWritableMessageChannel
- Parameters:
dst
- the message to send- Returns:
- the result of the send operation;
true
if the message was sent, orfalse
if it would block - Throws:
java.io.IOException
- if an I/O error occurs
-
send
public boolean send(java.nio.ByteBuffer[] srcs) throws java.io.IOException
Description copied from interface:WritableMessageChannel
Send a complete message.- Specified by:
send
in interfaceWritableMessageChannel
- Parameters:
srcs
- the buffers holding the message to send- Returns:
- the result of the send operation;
true
if the message was sent, orfalse
if it would block - Throws:
java.io.IOException
- if an I/O error occurs
-
send
public boolean send(java.nio.ByteBuffer[] dsts, int offs, int len) throws java.io.IOException
Description copied from interface:WritableMessageChannel
Send a complete message.- Specified by:
send
in interfaceWritableMessageChannel
- Parameters:
dsts
- the buffers holding the message to sendoffs
- the offset into the buffer array of the first bufferlen
- the number of buffers that contain data to send- Returns:
- the result of the send operation;
true
if the message was sent, orfalse
if it would block - Throws:
java.io.IOException
- if an I/O error occurs
-
sendFinal
public boolean sendFinal(java.nio.ByteBuffer buffer) throws java.io.IOException
Description copied from interface:WritableMessageChannel
Send a complete message. If the message was successfully sent the channel with have its writes shutdown.- Specified by:
sendFinal
in interfaceWritableMessageChannel
- Parameters:
buffer
- the message to send- Returns:
- the result of the send operation;
true
if the message was sent, orfalse
if it would block - Throws:
java.io.IOException
- if an I/O error occurs
-
sendFinal
public boolean sendFinal(java.nio.ByteBuffer[] buffers) throws java.io.IOException
Description copied from interface:WritableMessageChannel
Send a complete message. If the message was successfully sent the channel with have its writes shutdown.- Specified by:
sendFinal
in interfaceWritableMessageChannel
- Parameters:
buffers
- the buffers holding the message to send- Returns:
- the result of the send operation;
true
if the message was sent, orfalse
if it would block - Throws:
java.io.IOException
- if an I/O error occurs
-
sendFinal
public boolean sendFinal(java.nio.ByteBuffer[] buffers, int offs, int len) throws java.io.IOException
Description copied from interface:WritableMessageChannel
Send a complete message. If the message was successfully sent the channel with have its writes shutdown.- Specified by:
sendFinal
in interfaceWritableMessageChannel
- Parameters:
buffers
- the buffers holding the message to sendoffs
- the offset into the buffer array of the first bufferlen
- the number of buffers that contain data to send- Returns:
- the result of the send operation;
true
if the message was sent, orfalse
if it would block - Throws:
java.io.IOException
- if an I/O error occurs
-
flush
public boolean flush() throws java.io.IOException
Description copied from interface:SuspendableWriteChannel
Flush any waiting partial send or write. If there is no data to flush, or if the flush completed successfully, this method will returntrue
. If there is data to flush which cannot be immediately written, this method will returnfalse
. If this method returnstrue
afterSuspendableWriteChannel.shutdownWrites()
was called on this channel, the write listener will no longer be invoked on this channel. If this is case and additionally this is a write-only channel or the read side was previously shut down, then the channel will automatically be closed.- Specified by:
flush
in interfaceSuspendableWriteChannel
- Returns:
true
if the message was flushed, orfalse
if the result would block- Throws:
java.io.IOException
- if an I/O error occurs
-
supportsOption
public boolean supportsOption(Option<?> option)
Description copied from interface:Configurable
Determine whether an option is supported on this channel.- Specified by:
supportsOption
in interfaceConfigurable
- Parameters:
option
- the option- Returns:
true
if it is supported
-
getOption
public <T> T getOption(Option<T> option) throws java.io.IOException
Description copied from interface:Configurable
Get the value of a channel option.- Specified by:
getOption
in interfaceConfigurable
- Type Parameters:
T
- the type of the option value- Parameters:
option
- the option to get- Returns:
- the value of the option, or
null
if it is not set - Throws:
java.io.IOException
- if an I/O error occurred when reading the option
-
setOption
public <T> T setOption(Option<T> option, T value) throws java.lang.IllegalArgumentException, java.io.IOException
Description copied from interface:Configurable
Set an option for this channel. Unsupported options are ignored.- Specified by:
setOption
in interfaceConfigurable
- Type Parameters:
T
- the type of the option value- Parameters:
option
- the option to setvalue
- the value of the option to set- Returns:
- the previous option value, if any
- Throws:
java.lang.IllegalArgumentException
- if the value is not acceptable for this optionjava.io.IOException
- if an I/O error occurred when modifying the option
-
shutdownWrites
public void shutdownWrites() throws java.io.IOException
Description copied from interface:SuspendableWriteChannel
Indicate that writing is complete for this channel. Further attempts to write data to this channel after this method is invoked will result in an exception. If this method was already called, calling this method again will have no additional effect. After this method is called, any remaining data still must be flushed out via theSuspendableWriteChannel.flush()
method; once this is done, if the read side of the channel was shut down, the channel will automatically close.- Specified by:
shutdownWrites
in interfaceSuspendableWriteChannel
- Throws:
java.io.IOException
- if an I/O error occurs
-
isOpen
public boolean isOpen()
Description copied from interface:SuspendableWriteChannel
Determine whether this channel is open. This method will returnfalse
if all directions are shut down, even if there is unflushed write data pending.- Specified by:
isOpen
in interfacejava.nio.channels.Channel
- Specified by:
isOpen
in interfaceSuspendableWriteChannel
- Returns:
true
if the channel is open,false
otherwise
-
close
public void close() throws java.io.IOException
Description copied from interface:SuspendableWriteChannel
Close this channel. If data has been written but not flushed, that data may be discarded, depending on the channel implementation. When a channel is closed, its close listener is invoked. Invoking this method more than once has no additional effect.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.nio.channels.Channel
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceCloseableChannel
- Specified by:
close
in interfacejava.nio.channels.InterruptibleChannel
- Specified by:
close
in interfaceSuspendableWriteChannel
- Throws:
java.io.IOException
- if the close failed
-
getWriteThread
@Deprecated public XnioExecutor getWriteThread()
Deprecated.Description copied from interface:SuspendableWriteChannel
Get the write thread for this channel.- Specified by:
getWriteThread
in interfaceSuspendableWriteChannel
- Returns:
- the thread, or
null
if none is configured or available
-
getIoThread
public XnioIoThread getIoThread()
Description copied from interface:CloseableChannel
Get the I/O thread associated with this channel.- Specified by:
getIoThread
in interfaceCloseableChannel
- Returns:
- the I/O thread associated with this channel
-
getWorker
public XnioWorker getWorker()
Description copied from interface:CloseableChannel
Get the worker for this channel.- Specified by:
getWorker
in interfaceCloseableChannel
- Returns:
- the worker
-
clone
public ConduitWritableMessageChannel clone()
Duplicate this channel. Changing the delegate conduit in one channel will not affect the other.- Overrides:
clone
in classjava.lang.Object
- Returns:
- the cloned channel
-
-