Package org.glassfish.grizzly.nio
Class AbstractNIOAsyncQueueWriter
- java.lang.Object
-
- org.glassfish.grizzly.AbstractWriter<SocketAddress>
-
- org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter
-
- All Implemented Interfaces:
AsyncQueue,AsyncQueueWriter<SocketAddress>,Writer<SocketAddress>
- Direct Known Subclasses:
TCPNIOAsyncQueueWriter,UDPNIOAsyncQueueWriter
public abstract class AbstractNIOAsyncQueueWriter extends AbstractWriter<SocketAddress> implements AsyncQueueWriter<SocketAddress>
TheAsyncQueueWriterimplementation, based on the Java NIO- Author:
- Alexey Stashok, Ryan Lubke, Gustav Trede
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.glassfish.grizzly.asyncqueue.AsyncQueue
AsyncQueue.AsyncResult
-
Nested classes/interfaces inherited from interface org.glassfish.grizzly.Writer
Writer.Reentrant
-
-
Field Summary
Fields Modifier and Type Field Description protected intmaxPendingBytesprotected intmaxWriteReentrantsprotected NIOTransporttransport-
Fields inherited from interface org.glassfish.grizzly.asyncqueue.AsyncQueue
EXPECTING_MORE_OPTION
-
Fields inherited from interface org.glassfish.grizzly.asyncqueue.AsyncQueueWriter
AUTO_SIZE, UNLIMITED_SIZE
-
-
Constructor Summary
Constructors Constructor Description AbstractNIOAsyncQueueWriter(NIOTransport transport)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected AsyncWriteQueueRecordaggregate(TaskQueue<AsyncWriteQueueRecord> connectionQueue)Aggregates records in a queue to be written as one chunk.booleancanWrite(Connection<SocketAddress> connection)Returntrueif the connection has not exceeded it's maximum size in bytes of pending writes, otherwisefalse.booleancanWrite(Connection<SocketAddress> connection, int size)Deprecated.voidclose()Close AsyncQueueProcessor and release associated resourcesprotected AsyncWriteQueueRecordcreateRecord(Connection connection, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, SocketAddress dstAddress, PushBackHandler pushBackHandler, boolean isUncountable)intgetMaxPendingBytesPerConnection()booleanisAllowDirectWrite()Returns true, if async write queue is allowed to write buffer directly during write(...) method call, w/o adding buffer to the queue, or false otherwise.booleanisReady(Connection connection)Checks whether there is ready data inAsyncQueue, associated with theConnection.voidnotifyWritePossible(Connection<SocketAddress> connection, WriteHandler writeHandler)RegistersWriteHandler, which will be notified ones at least one byte can be written.voidnotifyWritePossible(Connection<SocketAddress> connection, WriteHandler writeHandler, int size)Deprecated.voidonClose(Connection connection)Callback method, which is called, whenConnectionhas been closed, to let processor release a connection associated resources.protected abstract voidonReadyToWrite(NIOConnection connection)protected static voidonWriteFailure(Connection connection, AsyncWriteQueueRecord failedRecord, Throwable e)AsyncQueue.AsyncResultprocessAsync(Context context)Callback method, which is called async.voidsetAllowDirectWrite(boolean isAllowDirectWrite)Set true, if async write queue is allowed to write buffer directly during write(...) method call, w/o adding buffer to the queue, or false otherwise.voidsetMaxPendingBytesPerConnection(int maxPendingBytes)Configures the maximum number of bytes pending to be written for a particularConnection.voidwrite(Connection<SocketAddress> connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, MessageCloner<WritableMessage> cloner)Method writes theWritableMessageto the specific address.voidwrite(Connection<SocketAddress> connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, PushBackHandler pushBackHandler)Deprecated.voidwrite(Connection<SocketAddress> connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, PushBackHandler pushBackHandler, MessageCloner<WritableMessage> cloner)Deprecated.protected abstract RecordWriteResultwrite0(NIOConnection connection, AsyncWriteQueueRecord queueRecord)-
Methods inherited from class org.glassfish.grizzly.AbstractWriter
write, write, write, write
-
-
-
-
Field Detail
-
transport
protected final NIOTransport transport
-
maxPendingBytes
protected volatile int maxPendingBytes
-
maxWriteReentrants
protected volatile int maxWriteReentrants
-
-
Constructor Detail
-
AbstractNIOAsyncQueueWriter
public AbstractNIOAsyncQueueWriter(NIOTransport transport)
-
-
Method Detail
-
canWrite
@Deprecated public boolean canWrite(Connection<SocketAddress> connection, int size)
Deprecated.- Specified by:
canWritein interfaceAsyncQueueWriter<SocketAddress>- Parameters:
connection- theConnectionto test whether or not the specified number of bytes can be written to.size- number of bytes to write.- Returns:
trueif the queue has not exceeded it's maximum size in bytes of pending writes, otherwisefalse
-
canWrite
public boolean canWrite(Connection<SocketAddress> connection)
Description copied from interface:WriterReturntrueif the connection has not exceeded it's maximum size in bytes of pending writes, otherwisefalse.- Specified by:
canWritein interfaceWriter<SocketAddress>- Parameters:
connection- theConnectionto test whether or not the specified number of bytes can be written to.- Returns:
trueif the connection has not exceeded it's maximum size in bytes of pending writes, otherwisefalse
-
notifyWritePossible
@Deprecated public void notifyWritePossible(Connection<SocketAddress> connection, WriteHandler writeHandler, int size)
Deprecated.RegistersWriteHandler, which will be notified ones theConnectionis able to accept more bytes to be written. Note: using this method from different threads simultaneously may lead to quick situation changes, so at timeWriteHandleris called - the queue may become busy again.- Specified by:
notifyWritePossiblein interfaceAsyncQueueWriter<SocketAddress>- Parameters:
connection-ConnectionwriteHandler-WriteHandlerto be notified.size- number of bytes queue has to be able to accept before notifyingWriteHandler.
-
notifyWritePossible
public void notifyWritePossible(Connection<SocketAddress> connection, WriteHandler writeHandler)
Description copied from interface:WriterRegistersWriteHandler, which will be notified ones at least one byte can be written. This method call is equivalent to call notifyWritePossible(connection, writeHandler, 1); Note: using this method from different threads simultaneously may lead to quick situation changes, so at timeWriteHandleris called - the queue may become busy again.- Specified by:
notifyWritePossiblein interfaceWriter<SocketAddress>- Parameters:
connection-ConnectionwriteHandler-WriteHandlerto be notified.
-
setMaxPendingBytesPerConnection
public void setMaxPendingBytesPerConnection(int maxPendingBytes)
Configures the maximum number of bytes pending to be written for a particularConnection.- Specified by:
setMaxPendingBytesPerConnectionin interfaceAsyncQueueWriter<SocketAddress>- Parameters:
maxPendingBytes- maximum number of bytes that may be pending to be written to a particularConnection.
-
getMaxPendingBytesPerConnection
public int getMaxPendingBytesPerConnection()
- Specified by:
getMaxPendingBytesPerConnectionin interfaceAsyncQueueWriter<SocketAddress>- Returns:
- the maximum number of bytes that may be pending to be written to a particular
Connection. By default, this will be four times the size of theSocketsend buffer size.
-
isAllowDirectWrite
public boolean isAllowDirectWrite()
Returns true, if async write queue is allowed to write buffer directly during write(...) method call, w/o adding buffer to the queue, or false otherwise.- Specified by:
isAllowDirectWritein interfaceAsyncQueueWriter<SocketAddress>- Returns:
- true, if async write queue is allowed to write buffer directly during write(...) method call, w/o adding buffer to the queue, or false otherwise.
-
setAllowDirectWrite
public void setAllowDirectWrite(boolean isAllowDirectWrite)
Set true, if async write queue is allowed to write buffer directly during write(...) method call, w/o adding buffer to the queue, or false otherwise.- Specified by:
setAllowDirectWritein interfaceAsyncQueueWriter<SocketAddress>- Parameters:
isAllowDirectWrite- true, if async write queue is allowed to write buffer directly during write(...) method call, w/o adding buffer to the queue, or false otherwise.
-
write
public void write(Connection<SocketAddress> connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, MessageCloner<WritableMessage> cloner)
Description copied from interface:WriterMethod writes theWritableMessageto the specific address.- Specified by:
writein interfaceWriter<SocketAddress>- Parameters:
connection- theConnectionto write todstAddress- the destination address theWritableMessagewill be sent tomessage- theWritableMessage, from which the data will be writtencompletionHandler-CompletionHandler, which will get notified, when write will be completedcloner- theMessageCloner, which will be able to clone the message in case it can't be completely written in the current thread.
-
write
@Deprecated public void write(Connection<SocketAddress> connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, PushBackHandler pushBackHandler)
Deprecated.Description copied from interface:WriterMethod writes theWritableMessageto the specific address.- Specified by:
writein interfaceWriter<SocketAddress>- Parameters:
connection- theConnectionto write todstAddress- the destination address theWritableMessagewill be sent tomessage- theWritableMessage, 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 refused
-
write
@Deprecated public void write(Connection<SocketAddress> connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, PushBackHandler pushBackHandler, MessageCloner<WritableMessage> cloner)
Deprecated.Method writes theBufferto the specific address.- Specified by:
writein interfaceAsyncQueueWriter<SocketAddress>- Parameters:
connection- theConnectionto write todstAddress- the destination address theWritableMessagewill be sent tomessage- theWritableMessage, 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 refusedcloner-MessageCloner, which will be invoked by AsyncQueueWriter, if message could not be written to a channel directly and has to be put on a asynchronous queue
-
processAsync
public AsyncQueue.AsyncResult processAsync(Context context)
Callback method, which is called async. to process readyAsyncQueue, which are associated with the givenConnection- Specified by:
processAsyncin interfaceAsyncQueue- Parameters:
context-Context- Returns:
AsyncQueue.AsyncResult, depending on async queue status.
-
createRecord
protected AsyncWriteQueueRecord createRecord(Connection connection, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, SocketAddress dstAddress, PushBackHandler pushBackHandler, boolean isUncountable)
-
isReady
public final boolean isReady(Connection connection)
Checks whether there is ready data inAsyncQueue, associated with theConnection.- Specified by:
isReadyin interfaceAsyncQueue- Parameters:
connection-Connection- Returns:
- true, if there is ready data, or false otherwise.
-
onClose
public void onClose(Connection connection)
Callback method, which is called, whenConnectionhas been closed, to let processor release a connection associated resources.- Specified by:
onClosein interfaceAsyncQueue- Parameters:
connection-Connection
-
close
public final void close()
Close AsyncQueueProcessor and release associated resources- Specified by:
closein interfaceAsyncQueue
-
onWriteFailure
protected static void onWriteFailure(Connection connection, AsyncWriteQueueRecord failedRecord, Throwable e)
-
write0
protected abstract RecordWriteResult write0(NIOConnection connection, AsyncWriteQueueRecord queueRecord) throws IOException
- Throws:
IOException
-
onReadyToWrite
protected abstract void onReadyToWrite(NIOConnection connection) throws IOException
- Throws:
IOException
-
aggregate
protected AsyncWriteQueueRecord aggregate(TaskQueue<AsyncWriteQueueRecord> connectionQueue)
Aggregates records in a queue to be written as one chunk.
-
-