org.glassfish.grizzly.nio
Class AbstractNIOAsyncQueueWriter

java.lang.Object
  extended by org.glassfish.grizzly.AbstractWriter<SocketAddress>
      extended by 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>

The AsyncQueueWriter implementation, 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.AsyncQueueWriter
AsyncQueueWriter.Reentrant
 
Nested classes/interfaces inherited from interface org.glassfish.grizzly.asyncqueue.AsyncQueue
AsyncQueue.AsyncResult
 
Field Summary
protected static int EMPTY_RECORD_SPACE_VALUE
           
protected  int maxPendingBytes
           
protected  int maxWriteReentrants
           
protected  NIOTransport transport
           
 
Fields inherited from interface org.glassfish.grizzly.asyncqueue.AsyncQueueWriter
AUTO_SIZE, UNLIMITED_SIZE
 
Fields inherited from interface org.glassfish.grizzly.asyncqueue.AsyncQueue
EXPECTING_MORE_OPTION
 
Constructor Summary
AbstractNIOAsyncQueueWriter(NIOTransport transport)
           
 
Method Summary
protected  AsyncWriteQueueRecord aggregate(TaskQueue<AsyncWriteQueueRecord> connectionQueue)
          Aggregates records in a queue to be written as one chunk.
 boolean canWrite(Connection connection, int size)
          
 void close()
          Close AsyncQueueProcessor and release associated resources
protected  AsyncWriteQueueRecord createRecord(Connection connection, WritableMessage message, WriteResult<WritableMessage,SocketAddress> currentResult, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, SocketAddress dstAddress, PushBackHandler pushBackHandler, boolean isEmptyRecord)
           
 int getMaxPendingBytesPerConnection()
          
 int getMaxWriteReentrants()
          Returns the maximum number of write() method reentrants a thread is allowed to made.
 AsyncQueueWriter.Reentrant getWriteReentrant()
          Returns the current write reentrants counter.
 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.
 boolean isMaxReentrantsReached(AsyncQueueWriter.Reentrant reentrant)
          Returns true, if max number of write->completion-handler reentrants has been reached for the passed AsyncQueueWriter.Reentrant object, and next write will happen in the separate thread.
 boolean isReady(Connection connection)
          Checks whether there is ready data in AsyncQueue, associated with the Connection.
 void notifyWritePossible(Connection connection, WriteHandler writeHandler, int size)
          Registers WriteHandler, which will be notified ones Buffer of "size"-bytes can be written.
protected static void offerToTaskQueue(NIOConnection nioConnection, AsyncWriteQueueRecord queueRecord, TaskQueue<AsyncWriteQueueRecord> taskQueue)
           
 void onClose(Connection connection)
          Callback method, which is called, when Connection has been closed, to let processor release a connection associated resources.
protected abstract  void onReadyToWrite(NIOConnection connection)
           
protected static void onWriteFailure(Connection connection, AsyncWriteQueueRecord failedRecord, Throwable e)
           
 AsyncQueue.AsyncResult processAsync(Context context)
          Callback method, which is called async.
 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.
 void setMaxPendingBytesPerConnection(int maxPendingBytes)
          Configures the maximum number of bytes pending to be written for a particular Connection.
 void setMaxWriteReentrants(int maxWriteReentrants)
          Sets the maximum number of write() method reentrants a thread is allowed to made.
 void write(Connection connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, PushBackHandler pushBackHandler)
          Method writes the WritableMessage to the specific address.
 void write(Connection connection, SocketAddress dstAddress, WritableMessage message, CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler, PushBackHandler pushBackHandler, MessageCloner<WritableMessage> cloner)
          Method writes the Buffer to the specific address.
protected abstract  long write0(NIOConnection connection, AsyncWriteQueueRecord queueRecord)
           
protected  void writeQueueRecord(AsyncWriteQueueRecord queueRecord, MessageCloner<WritableMessage> cloner, PushBackContext pushBackContext)
           
 
Methods inherited from class org.glassfish.grizzly.AbstractWriter
write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.glassfish.grizzly.Writer
write, write, write, write
 

Field Detail

EMPTY_RECORD_SPACE_VALUE

protected static final int EMPTY_RECORD_SPACE_VALUE
See Also:
Constant Field Values

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

public boolean canWrite(Connection connection,
                        int size)

Specified by:
canWrite in interface AsyncQueueWriter<SocketAddress>
Parameters:
connection - the Connection to test whether or not the specified number of bytes can be written to.
size - number of bytes to write.
Returns:
true if the queue has not exceeded it's maximum size in bytes of pending writes, otherwise false

notifyWritePossible

public void notifyWritePossible(Connection connection,
                                WriteHandler writeHandler,
                                int size)
Registers WriteHandler, which will be notified ones Buffer of "size"-bytes can be written. Note: using this method from different threads simultaneously may lead to quick situation changes, so at time WriteHandler is called - the queue may become busy again. It's recommended to use this method together with PushBackHandler to have a chance to handle such a situations properly.

Specified by:
notifyWritePossible in interface AsyncQueueWriter<SocketAddress>
Parameters:
connection - Connection
writeHandler - WriteHandler to be notified.
size - number of bytes queue has to be able to accept before notifying WriteHandler.

setMaxPendingBytesPerConnection

public void setMaxPendingBytesPerConnection(int maxPendingBytes)
Configures the maximum number of bytes pending to be written for a particular Connection.

Specified by:
setMaxPendingBytesPerConnection in interface AsyncQueueWriter<SocketAddress>
Parameters:
maxPendingBytes - maximum number of bytes that may be pending to be written to a particular Connection.

getMaxPendingBytesPerConnection

public int getMaxPendingBytesPerConnection()

Specified by:
getMaxPendingBytesPerConnection in interface AsyncQueueWriter<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 the Socket send buffer size.

getMaxWriteReentrants

public int getMaxWriteReentrants()
Returns the maximum number of write() method reentrants a thread is allowed to made. This is related to possible write()->onComplete()->write()->... chain, which may grow infinitely and cause StackOverflow. Using maxWriteReentrants value it's possible to limit such a chain.

Specified by:
getMaxWriteReentrants in interface AsyncQueueWriter<SocketAddress>
Returns:
the maximum number of write() method reentrants a thread is allowed to make.

setMaxWriteReentrants

public void setMaxWriteReentrants(int maxWriteReentrants)
Sets the maximum number of write() method reentrants a thread is allowed to made. This is related to possible write()->onComplete()->write()->... chain, which may grow infinitely and cause StackOverflow. Using maxWriteReentrants value it's possible to limit such a chain.

Specified by:
setMaxWriteReentrants in interface AsyncQueueWriter<SocketAddress>
Parameters:
maxWriteReentrants - the maximum number of write() method calls a thread is allowed to make.

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.

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.

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 connection,
                  SocketAddress dstAddress,
                  WritableMessage message,
                  CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler,
                  PushBackHandler pushBackHandler)
Description copied from interface: Writer
Method writes the WritableMessage to the specific address.

Specified by:
write in interface Writer<SocketAddress>
Parameters:
connection - the Connection to write to
dstAddress - the destination address the WritableMessage will be sent to
message - the WritableMessage, from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed
pushBackHandler - PushBackHandler, which will be notified if message was accepted by transport write queue or refused

write

public void write(Connection connection,
                  SocketAddress dstAddress,
                  WritableMessage message,
                  CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler,
                  PushBackHandler pushBackHandler,
                  MessageCloner<WritableMessage> cloner)
Method writes the Buffer to the specific address.

Specified by:
write in interface AsyncQueueWriter<SocketAddress>
Parameters:
connection - the Connection to write to
dstAddress - the destination address the WritableMessage will be sent to
message - the WritableMessage, from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed
pushBackHandler - PushBackHandler, which will be notified if message was accepted by transport write queue or refused
cloner - 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

writeQueueRecord

protected void writeQueueRecord(AsyncWriteQueueRecord queueRecord,
                                MessageCloner<WritableMessage> cloner,
                                PushBackContext pushBackContext)

processAsync

public AsyncQueue.AsyncResult processAsync(Context context)
Callback method, which is called async. to process ready AsyncQueue, which are associated with the given Connection

Specified by:
processAsync in interface AsyncQueue
Parameters:
context - Context
Returns:
AsyncQueue.AsyncResult, depending on async queue status.

offerToTaskQueue

protected static void offerToTaskQueue(NIOConnection nioConnection,
                                       AsyncWriteQueueRecord queueRecord,
                                       TaskQueue<AsyncWriteQueueRecord> taskQueue)

createRecord

protected AsyncWriteQueueRecord createRecord(Connection connection,
                                             WritableMessage message,
                                             WriteResult<WritableMessage,SocketAddress> currentResult,
                                             CompletionHandler<WriteResult<WritableMessage,SocketAddress>> completionHandler,
                                             SocketAddress dstAddress,
                                             PushBackHandler pushBackHandler,
                                             boolean isEmptyRecord)

isReady

public final boolean isReady(Connection connection)
Checks whether there is ready data in AsyncQueue, associated with the Connection.

Specified by:
isReady in interface AsyncQueue
Parameters:
connection - Connection
Returns:
true, if there is ready data, or false otherwise.

onClose

public void onClose(Connection connection)
Callback method, which is called, when Connection has been closed, to let processor release a connection associated resources.

Specified by:
onClose in interface AsyncQueue
Parameters:
connection - Connection

getWriteReentrant

public AsyncQueueWriter.Reentrant getWriteReentrant()
Returns the current write reentrants counter. Might be useful, if developer wants to use custom notification mechanism, based on on AsyncQueueWriter.canWrite(org.glassfish.grizzly.Connection, int) and various write methods.

Specified by:
getWriteReentrant in interface AsyncQueueWriter<SocketAddress>

isMaxReentrantsReached

public boolean isMaxReentrantsReached(AsyncQueueWriter.Reentrant reentrant)
Description copied from interface: AsyncQueueWriter
Returns true, if max number of write->completion-handler reentrants has been reached for the passed AsyncQueueWriter.Reentrant object, and next write will happen in the separate thread.

Specified by:
isMaxReentrantsReached in interface AsyncQueueWriter<SocketAddress>
Parameters:
reentrant - AsyncQueueWriter.Reentrant object.
Returns:
true, if max number of write->completion-handler reentrants has been reached for the passed AsyncQueueWriter.Reentrant object, and next write will happen in the separate thread.

close

public final void close()
Close AsyncQueueProcessor and release associated resources

Specified by:
close in interface AsyncQueue

onWriteFailure

protected static void onWriteFailure(Connection connection,
                                     AsyncWriteQueueRecord failedRecord,
                                     Throwable e)

write0

protected abstract long 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.



Copyright © 2012 Oracle Corporation. All Rights Reserved.