public abstract class NIOConnection extends Object implements Connection<SocketAddress>
Connection implementation for Java NIO Connections.Connection.CloseListener, Connection.CloseType| Modifier and Type | Field and Description |
|---|---|
protected AttributeHolder |
attributes |
protected SelectableChannel |
channel |
protected AtomicReferenceFieldUpdater<NIOConnection,Object> |
connectCloseSemaphorUpdater |
protected boolean |
isBlocking |
protected boolean |
isStandalone |
protected int |
maxAsyncWriteQueueSize |
protected DefaultMonitoringConfig<ConnectionProbe> |
monitoringConfig
Connection probes
|
protected static Object |
NOTIFICATION_CLOSED_COMPLETE |
protected static Object |
NOTIFICATION_INITIALIZED |
protected Processor |
processor |
protected ProcessorSelector |
processorSelector |
protected long |
readTimeoutMillis |
protected SelectionKey |
selectionKey |
protected SelectorRunner |
selectorRunner |
protected NIOTransport |
transport |
protected long |
writeTimeoutMillis |
protected short |
zeroByteReadCount |
| Constructor and Description |
|---|
NIOConnection(NIOTransport transport) |
| Modifier and Type | Method and Description |
|---|---|
void |
addCloseListener(CloseListener closeListener)
Add the
Connection.CloseListener, which will be notified once Connection
will be closed. |
void |
addCloseListener(Connection.CloseListener closeListener)
Add the
Connection.CloseListener, which will be notified once Connection
will be closed. |
void |
assertOpen()
Checks if this Connection is open and ready to be used.
|
void |
attachToSelectorRunner(SelectorRunner selectorRunner) |
protected void |
checkEmptyRead(int size) |
GrizzlyFuture<Closeable> |
close()
Gracefully close the
Connection |
void |
close(CompletionHandler<Closeable> completionHandler)
Deprecated.
please use
close() with the following GrizzlyFuture.addCompletionHandler(org.glassfish.grizzly.CompletionHandler) call |
GrizzlyFuture<CloseReason> |
closeFuture() |
protected void |
closeGracefully0(CompletionHandler<Closeable> completionHandler,
CloseReason closeReason) |
void |
closeSilently()
Gracefully close the
Connection silently, no notification required on
completion or failure. |
void |
closeWithReason(IOException reason)
Gracefully closes the Connection and provides the reason description.
|
void |
configureBlocking(boolean isBlocking)
Sets the
Connection mode. |
void |
configureStandalone(boolean isStandalone) |
void |
detachSelectorRunner() |
void |
disableIOEvent(IOEvent ioEvent) |
protected void |
doClose()
Do the actual connection close.
|
protected void |
enableInitialOpRead()
Enables OP_READ if it has never been enabled before.
|
void |
enableIOEvent(IOEvent ioEvent) |
void |
executeInEventThread(IOEvent event,
Runnable runnable)
Executes the
Runnable in the thread, responsible for running
the given type of event on this Connection. |
TaskQueue<AsyncReadQueueRecord> |
getAsyncReadQueue() |
TaskQueue<AsyncWriteQueueRecord> |
getAsyncWriteQueue() |
AttributeHolder |
getAttributes()
Get associated
AttributeHolder. |
SelectableChannel |
getChannel() |
CloseReason |
getCloseReason()
Returns
CloseReason if this Connection has been closed,
or null otherwise. |
int |
getMaxAsyncWriteQueueSize()
Get the max size (in bytes) of asynchronous write queue associated
with connection.
|
MemoryManager<?> |
getMemoryManager() |
MonitoringConfig<ConnectionProbe> |
getMonitoringConfig()
Return the object associated
MonitoringConfig. |
Processor |
getProcessor()
Gets the default
Processor, which will process Connection
I/O events. |
ProcessorSelector |
getProcessorSelector()
Gets the default
ProcessorSelector, which will be used to get
Processor to process Connection I/O events, in case if
this Connection's Processor is null. |
long |
getReadTimeout(TimeUnit timeUnit)
Returns the current value for the blocking read timeout converted to the
provided
TimeUnit specification. |
SelectionKey |
getSelectionKey() |
SelectorRunner |
getSelectorRunner() |
Transport |
getTransport()
Get the
Transport, to which this Connection belongs to. |
long |
getWriteTimeout(TimeUnit timeUnit)
Returns the current value for the blocking write timeout converted to the
provided
TimeUnit specification. |
boolean |
isBlocking() |
boolean |
isClosed() |
boolean |
isOpen()
Is
Connection open and ready. |
boolean |
isStandalone() |
void |
notifyConnectionError(Throwable error)
Method gets invoked, when error occur during the Connection lifecycle.
|
protected static void |
notifyIOEventDisabled(NIOConnection connection,
IOEvent ioEvent)
Notify registered
ConnectionProbes about the IO Event disabled event. |
protected static void |
notifyIOEventEnabled(NIOConnection connection,
IOEvent ioEvent)
Notify registered
ConnectionProbes about the IO Event enabled event. |
protected static void |
notifyIOEventReady(NIOConnection connection,
IOEvent ioEvent)
Notify registered
ConnectionProbes about the IO Event ready event. |
protected static void |
notifyProbesAccept(NIOConnection serverConnection,
NIOConnection clientConnection)
Notify registered
ConnectionProbes about the accept event. |
protected static void |
notifyProbesBind(NIOConnection connection)
Notify registered
ConnectionProbes about the bind event. |
protected static void |
notifyProbesClose(NIOConnection connection)
Notify registered
ConnectionProbes about the close event. |
protected static void |
notifyProbesConnect(NIOConnection connection)
Notify registered
ConnectionProbes about the connect event. |
protected static void |
notifyProbesError(NIOConnection connection,
Throwable error)
Notify registered
ConnectionProbes about the error. |
protected static void |
notifyProbesRead(NIOConnection connection,
Buffer data,
int size)
Notify registered
ConnectionProbes about the read event. |
protected static void |
notifyProbesWrite(NIOConnection connection,
Buffer data,
long size)
Notify registered
ConnectionProbes about the write event. |
Processor |
obtainProcessor(IOEvent ioEvent)
Gets the
Processor, which will process Connection
I/O event. |
<E> E |
obtainProcessorState(Processor processor,
NullaryFunction<E> factory)
Returns the
Processor state associated with this Connection. |
protected void |
preClose() |
<M> GrizzlyFuture<ReadResult<M,SocketAddress>> |
read()
Method reads data.
|
<M> void |
read(CompletionHandler<ReadResult<M,SocketAddress>> completionHandler) |
boolean |
removeCloseListener(CloseListener closeListener)
Remove the
Connection.CloseListener. |
boolean |
removeCloseListener(Connection.CloseListener closeListener)
Remove the
Connection.CloseListener. |
protected void |
setChannel(SelectableChannel channel) |
void |
setMaxAsyncWriteQueueSize(int maxAsyncWriteQueueSize)
Set the max size (in bytes) of asynchronous write queue associated
with connection.
|
void |
setProcessor(Processor preferableProcessor)
Sets the default
Processor, which will process Connection
I/O events. |
void |
setProcessorSelector(ProcessorSelector preferableProcessorSelector)
Sets the default
ProcessorSelector, which will be used to get
Processor to process Connection I/O events, in case if
this Connection's Processor is null. |
void |
setReadTimeout(long timeout,
TimeUnit timeUnit)
Specifies the timeout for the blocking reads.
|
protected void |
setSelectionKey(SelectionKey selectionKey) |
protected void |
setSelectorRunner(SelectorRunner selectorRunner) |
void |
setWriteTimeout(long timeout,
TimeUnit timeUnit)
Specifies the timeout for the blocking writes.
|
void |
simulateIOEvent(IOEvent ioEvent) |
GrizzlyFuture<Closeable> |
terminate()
Close the
Connection |
protected void |
terminate0(CompletionHandler<Closeable> completionHandler,
CloseReason reason) |
void |
terminateSilently()
Close the
Connection silently, no notification required on
completion or failure. |
void |
terminateWithReason(IOException reason)
Closes the Connection and provides the reason description.
|
<M> GrizzlyFuture<WriteResult<M,SocketAddress>> |
write(M message)
Method writes the buffer.
|
<M> void |
write(M message,
CompletionHandler<WriteResult<M,SocketAddress>> completionHandler)
Method writes the buffer.
|
<M> void |
write(M message,
CompletionHandler<WriteResult<M,SocketAddress>> completionHandler,
PushBackHandler pushbackHandler)
Deprecated.
|
<M> void |
write(SocketAddress dstAddress,
M message,
CompletionHandler<WriteResult<M,SocketAddress>> completionHandler)
Method writes the buffer to the specific address.
|
<M> void |
write(SocketAddress dstAddress,
M message,
CompletionHandler<WriteResult<M,SocketAddress>> completionHandler,
PushBackHandler pushbackHandler)
Deprecated.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetLocalAddress, getPeerAddress, getReadBufferSize, getWriteBufferSize, setReadBufferSize, setWriteBufferSizecanWrite, canWrite, notifyCanWrite, notifyCanWriteprotected static final Object NOTIFICATION_INITIALIZED
protected static final Object NOTIFICATION_CLOSED_COMPLETE
protected final NIOTransport transport
protected volatile int maxAsyncWriteQueueSize
protected volatile long readTimeoutMillis
protected volatile long writeTimeoutMillis
protected volatile SelectableChannel channel
protected volatile SelectionKey selectionKey
protected volatile SelectorRunner selectorRunner
protected volatile Processor processor
protected volatile ProcessorSelector processorSelector
protected final AttributeHolder attributes
protected final AtomicReferenceFieldUpdater<NIOConnection,Object> connectCloseSemaphorUpdater
protected volatile boolean isBlocking
protected volatile boolean isStandalone
protected short zeroByteReadCount
protected final DefaultMonitoringConfig<ConnectionProbe> monitoringConfig
public NIOConnection(NIOTransport transport)
public void configureBlocking(boolean isBlocking)
ConnectionConnection mode.configureBlocking in interface Connection<SocketAddress>isBlocking - the Connection mode. true,
if Connection should operate in blocking mode, or
false otherwise.public boolean isBlocking()
isBlocking in interface Connection<SocketAddress>Connection mode.
true, if Connection is operating in blocking mode, or
false otherwise.public MemoryManager<?> getMemoryManager()
getMemoryManager in interface Connection<SocketAddress>MemoryManager. It's a shortcut for #getTransport()#getMemoryManager()public void configureStandalone(boolean isStandalone)
configureStandalone in interface Connection<SocketAddress>public boolean isStandalone()
isStandalone in interface Connection<SocketAddress>public Transport getTransport()
ConnectionTransport, to which this Connection belongs to.getTransport in interface Connection<SocketAddress>Transport, to which this Connection belongs to.public int getMaxAsyncWriteQueueSize()
getMaxAsyncWriteQueueSize in interface Connection<SocketAddress>public void setMaxAsyncWriteQueueSize(int maxAsyncWriteQueueSize)
setMaxAsyncWriteQueueSize in interface Connection<SocketAddress>maxAsyncWriteQueueSize - the max size (in bytes) of asynchronous
write queue associated with connection.public long getReadTimeout(TimeUnit timeUnit)
ConnectionTimeUnit specification. If this value hasn't been
explicitly set, it will default to seconds.getReadTimeout in interface Connection<SocketAddress>timeUnit - the TimeUnit to convert the returned result to.public void setReadTimeout(long timeout,
TimeUnit timeUnit)
ConnectionsetReadTimeout in interface Connection<SocketAddress>timeout - the new timeout valuetimeUnit - the specification of the provided value.Connection.setReadTimeout(long, java.util.concurrent.TimeUnit)public long getWriteTimeout(TimeUnit timeUnit)
ConnectionTimeUnit specification. If this value hasn't been
explicitly set, it will default to seconds.getWriteTimeout in interface Connection<SocketAddress>timeUnit - the TimeUnit to convert the returned result to.public void setWriteTimeout(long timeout,
TimeUnit timeUnit)
ConnectionsetWriteTimeout in interface Connection<SocketAddress>timeout - the new timeout valuetimeUnit - the specification of the provided value.Connection.setWriteTimeout(long, java.util.concurrent.TimeUnit)public SelectorRunner getSelectorRunner()
protected void setSelectorRunner(SelectorRunner selectorRunner)
public void attachToSelectorRunner(SelectorRunner selectorRunner) throws IOException
IOExceptionpublic void detachSelectorRunner()
throws IOException
IOExceptionpublic SelectableChannel getChannel()
protected void setChannel(SelectableChannel channel)
public SelectionKey getSelectionKey()
protected void setSelectionKey(SelectionKey selectionKey)
public Processor obtainProcessor(IOEvent ioEvent)
ConnectionProcessor, which will process Connection
I/O event.
If Processor is null, - then Transport will try
to get Processor using Connection's
ProcessorSelector.select(IOEvent, Connection). If
ProcessorSelector, associated withthe Connection is also
null - will ask Transport for a Processor.obtainProcessor in interface Connection<SocketAddress>Processor, which will process
Connection I/O events.public Processor getProcessor()
ConnectionProcessor, which will process Connection
I/O events.
If Processor is null, - then Transport will try
to get Processor using Connection's
ProcessorSelector.select(IOEvent, Connection). If
ProcessorSelector, associated withthe Connection is also
null - Transport will try to get Processor
using own settings.getProcessor in interface Connection<SocketAddress>Processor, which will process
Connection I/O events.public void setProcessor(Processor preferableProcessor)
ConnectionProcessor, which will process Connection
I/O events.
If Processor is null, - then Transport will try
to get Processor using Connection's
ProcessorSelector.select(IOEvent, Connection). If
ProcessorSelector, associated withthe Connection is also
null - Transport will try to get Processor
using own settings.setProcessor in interface Connection<SocketAddress>preferableProcessor - the default Processor, which will
process Connection I/O events.public ProcessorSelector getProcessorSelector()
ConnectionProcessorSelector, which will be used to get
Processor to process Connection I/O events, in case if
this Connection's Processor is null.getProcessorSelector in interface Connection<SocketAddress>ProcessorSelector, which will be used to get
Processor to process Connection I/O events, in case if
this Connection's Processor is null.public void setProcessorSelector(ProcessorSelector preferableProcessorSelector)
ConnectionProcessorSelector, which will be used to get
Processor to process Connection I/O events, in case if
this Connection's Processor is null.setProcessorSelector in interface Connection<SocketAddress>preferableProcessorSelector - the default ProcessorSelector,
which will be used to get Processor to process Connection
I/O events, in case if this Connection's Processor
is null.public <E> E obtainProcessorState(Processor processor, NullaryFunction<E> factory)
ConnectionProcessor state associated with this Connection.obtainProcessorState in interface Connection<SocketAddress>processor - ProcessorProcessor state associated with this Connection.public void executeInEventThread(IOEvent event, Runnable runnable)
ConnectionRunnable in the thread, responsible for running
the given type of event on this Connection.
The thread will be chosen based on Transport
settings, especially current I/O strategy.executeInEventThread in interface Connection<SocketAddress>public TaskQueue<AsyncReadQueueRecord> getAsyncReadQueue()
public TaskQueue<AsyncWriteQueueRecord> getAsyncWriteQueue()
public AttributeHolder getAttributes()
AttributeStorageAttributeHolder.
Implementation may return null if AttributeHolder wasn't
initialized yet.getAttributes in interface AttributeStorageAttributeHolder.
Implementation may return null if AttributeHolder wasn't
initialized yet.public <M> GrizzlyFuture<ReadResult<M,SocketAddress>> read()
Readableread in interface Readable<SocketAddress>Future, using which it's possible to check the resultpublic <M> void read(CompletionHandler<ReadResult<M,SocketAddress>> completionHandler)
read in interface Readable<SocketAddress>public <M> GrizzlyFuture<WriteResult<M,SocketAddress>> write(M message)
Writeablewrite in interface Writeable<SocketAddress>message - the buffer, from which the data will be writtenFuture, using which it's possible to check the
resultpublic <M> void write(M message,
CompletionHandler<WriteResult<M,SocketAddress>> completionHandler)
Writeablewrite in interface Writeable<SocketAddress>message - the buffer, from which the data will be writtencompletionHandler - CompletionHandler,
which will get notified, when write will be completed@Deprecated public <M> void write(M message, CompletionHandler<WriteResult<M,SocketAddress>> completionHandler, PushBackHandler pushbackHandler)
Writeablewrite in interface Writeable<SocketAddress>message - the buffer, from which the data will be writtencompletionHandler - CompletionHandler,
which will get notified, when write will be completedpushbackHandler - PushBackHandler, which will be notified
if message was accepted by transport write queue or refusedpublic <M> void write(SocketAddress dstAddress, M message, CompletionHandler<WriteResult<M,SocketAddress>> completionHandler)
Writeablewrite in interface Writeable<SocketAddress>dstAddress - the destination address the buffer will be
sent tomessage - the buffer, from which the data will be writtencompletionHandler - CompletionHandler,
which will get notified, when write will be completed@Deprecated public <M> void write(SocketAddress dstAddress, M message, CompletionHandler<WriteResult<M,SocketAddress>> completionHandler, PushBackHandler pushbackHandler)
Writeablewrite in interface Writeable<SocketAddress>dstAddress - the destination address the buffer will be
sent tomessage - the buffer, from which the data will be writtencompletionHandler - CompletionHandler,
which will get notified, when write will be completedpushbackHandler - PushBackHandler, which will be notified
if message was accepted by transport write queue or refusedpublic boolean isOpen()
ConnectionConnection open and ready.
Returns true, if connection is open and ready, or false
otherwise.isOpen in interface CloseableisOpen in interface Connection<SocketAddress>public void assertOpen()
throws IOException
ConnectionIOException giving the reason why this Connection
was closed.assertOpen in interface CloseableassertOpen in interface Connection<SocketAddress>IOExceptionpublic boolean isClosed()
public CloseReason getCloseReason()
ConnectionCloseReason if this Connection has been closed,
or null otherwise.getCloseReason in interface Connection<SocketAddress>CloseReason if this Connection has been closed,
or null otherwisepublic void terminateSilently()
ConnectionConnection silently, no notification required on
completion or failure.terminateSilently in interface CloseableterminateSilently in interface Connection<SocketAddress>public GrizzlyFuture<Closeable> terminate()
ConnectionConnectionterminate in interface Closeableterminate in interface Connection<SocketAddress>Future, which could be checked in case, if close operation
will be run asynchronouslypublic void terminateWithReason(IOException reason)
ConnectionConnection.terminateSilently(), but additionally
provides the reason why the Connection will be closed.terminateWithReason in interface CloseableterminateWithReason in interface Connection<SocketAddress>public GrizzlyFuture<Closeable> close()
ConnectionConnectionclose in interface Closeableclose in interface Connection<SocketAddress>Future, which could be checked in case, if close operation
will be run asynchronouslypublic void close(CompletionHandler<Closeable> completionHandler)
close() with the following GrizzlyFuture.addCompletionHandler(org.glassfish.grizzly.CompletionHandler) callConnectionclose in interface Closeableclose in interface Connection<SocketAddress>completionHandler - CompletionHandler to be called, when
the connection is closed.public final void closeSilently()
ConnectionConnection silently, no notification required on
completion or failure.closeSilently in interface CloseablecloseSilently in interface Connection<SocketAddress>public void closeWithReason(IOException reason)
ConnectionConnection.closeSilently(), but additionally
provides the reason why the Connection will be closed.closeWithReason in interface CloseablecloseWithReason in interface Connection<SocketAddress>public GrizzlyFuture<CloseReason> closeFuture()
closeFuture in interface CloseableFuture, that will be notified once this Closeable
is closedprotected void closeGracefully0(CompletionHandler<Closeable> completionHandler, CloseReason closeReason)
protected void terminate0(CompletionHandler<Closeable> completionHandler, CloseReason reason)
protected void doClose()
throws IOException
IOExceptionpublic void addCloseListener(CloseListener closeListener)
Connection.CloseListener, which will be notified once Connection
will be closed.addCloseListener in interface CloseableaddCloseListener in interface Connection<SocketAddress>closeListener - Connection.CloseListener.public boolean removeCloseListener(CloseListener closeListener)
Connection.CloseListener.removeCloseListener in interface CloseableremoveCloseListener in interface Connection<SocketAddress>closeListener - Connection.CloseListener.public void addCloseListener(Connection.CloseListener closeListener)
ConnectionConnection.CloseListener, which will be notified once Connection
will be closed.addCloseListener in interface Connection<SocketAddress>closeListener - Connection.CloseListenerpublic boolean removeCloseListener(Connection.CloseListener closeListener)
ConnectionConnection.CloseListener.removeCloseListener in interface Connection<SocketAddress>closeListener - Connection.CloseListener.public void notifyConnectionError(Throwable error)
notifyConnectionError in interface Connection<SocketAddress>error - Throwable.public final MonitoringConfig<ConnectionProbe> getMonitoringConfig()
MonitoringConfig.getMonitoringConfig in interface Connection<SocketAddress>getMonitoringConfig in interface MonitoringAware<ConnectionProbe>MonitoringConfig.protected static void notifyProbesBind(NIOConnection connection)
ConnectionProbes about the bind event.connection - the Connection event occurred on.protected static void notifyProbesAccept(NIOConnection serverConnection, NIOConnection clientConnection)
ConnectionProbes about the accept event.serverConnection - the server Connection, which accepted the client connection.clientConnection - the client Connection.protected static void notifyProbesConnect(NIOConnection connection)
ConnectionProbes about the connect event.connection - the Connection event occurred on.protected static void notifyProbesRead(NIOConnection connection, Buffer data, int size)
ConnectionProbes about the read event.protected static void notifyProbesWrite(NIOConnection connection, Buffer data, long size)
ConnectionProbes about the write event.protected static void notifyIOEventReady(NIOConnection connection, IOEvent ioEvent)
ConnectionProbes about the IO Event ready event.connection - the Connection event occurred on.ioEvent - the IOEvent.protected static void notifyIOEventEnabled(NIOConnection connection, IOEvent ioEvent)
ConnectionProbes about the IO Event enabled event.connection - the Connection event occurred on.ioEvent - the IOEvent.protected static void notifyIOEventDisabled(NIOConnection connection, IOEvent ioEvent)
ConnectionProbes about the IO Event disabled event.connection - the Connection event occurred on.ioEvent - the IOEvent.protected static void notifyProbesClose(NIOConnection connection)
ConnectionProbes about the close event.connection - the Connection event occurred on.protected static void notifyProbesError(NIOConnection connection, Throwable error)
ConnectionProbes about the error.connection - the Connection event occurred on.protected void preClose()
protected void enableInitialOpRead()
throws IOException
IOExceptionpublic void simulateIOEvent(IOEvent ioEvent) throws IOException
simulateIOEvent in interface Connection<SocketAddress>IOExceptionpublic final void enableIOEvent(IOEvent ioEvent) throws IOException
enableIOEvent in interface Connection<SocketAddress>IOExceptionpublic final void disableIOEvent(IOEvent ioEvent) throws IOException
disableIOEvent in interface Connection<SocketAddress>IOExceptionprotected final void checkEmptyRead(int size)
Copyright © 2012-2017, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.