org.glassfish.grizzly.nio
Class AbstractNIOAsyncQueueWriter

java.lang.Object
  extended by org.glassfish.grizzly.AbstractWriter<java.net.SocketAddress>
      extended by org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter
All Implemented Interfaces:
AsyncQueue, AsyncQueueWriter<java.net.SocketAddress>, Writer<java.net.SocketAddress>
Direct Known Subclasses:
TCPNIOAsyncQueueWriter, UDPNIOAsyncQueueWriter

public abstract class AbstractNIOAsyncQueueWriter
extends AbstractWriter<java.net.SocketAddress>
implements AsyncQueueWriter<java.net.SocketAddress>

The AsyncQueueWriter implementation, based on the Java NIO

Author:
Alexey Stashok, Ryan Lubke, Gustav Trede

Field Summary
protected  int maxPendingBytes
           
protected  int maxWriteReentrants
           
protected  NIOTransport transport
           
 
Constructor Summary
AbstractNIOAsyncQueueWriter(NIOTransport transport)
           
 
Method Summary
 boolean canWrite(Connection connection, int size)
          
 void close()
          Close AsyncQueueProcessor and release associated resources
protected  AsyncWriteQueueRecord createRecord(Connection connection, Buffer message, java.util.concurrent.Future<WriteResult<Buffer,java.net.SocketAddress>> future, WriteResult<Buffer,java.net.SocketAddress> currentResult, CompletionHandler<WriteResult<Buffer,java.net.SocketAddress>> completionHandler, java.net.SocketAddress dstAddress, boolean isEmptyRecord)
           
protected static void failWriteRecord(AsyncWriteQueueRecord record, java.lang.Throwable e)
           
 int getMaxPendingBytesPerConnection()
          
 int getMaxWriteReentrants()
          Returns the maximum number of write() method reentrants a thread is allowed to made.
 boolean isReady(Connection connection)
          Checks whether there is ready data in AsyncQueue, associated with the Connection.
 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(Connection connection)
           
protected static void onWriteComplete(AsyncWriteQueueRecord record)
           
protected static void onWriteFailure(Connection connection, AsyncWriteQueueRecord failedRecord, java.io.IOException e)
           
protected  void onWriteIncomplete(AsyncWriteQueueRecord record)
           
 boolean processAsync(Context context)
          Callback method, which is called async.
 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.
 GrizzlyFuture<WriteResult<Buffer,java.net.SocketAddress>> write(Connection connection, java.net.SocketAddress dstAddress, Buffer buffer, CompletionHandler<WriteResult<Buffer,java.net.SocketAddress>> completionHandler, Interceptor<WriteResult<Buffer,java.net.SocketAddress>> interceptor)
          Method writes the buffer to the specific address.
 GrizzlyFuture<WriteResult<Buffer,java.net.SocketAddress>> write(Connection connection, java.net.SocketAddress dstAddress, Buffer buffer, CompletionHandler<WriteResult<Buffer,java.net.SocketAddress>> completionHandler, Interceptor<WriteResult<Buffer,java.net.SocketAddress>> interceptor, MessageCloner<Buffer> cloner)
          Method writes the Buffer to the specific address.
protected abstract  int write0(NIOConnection connection, AsyncWriteQueueRecord queueRecord)
           
 
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

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<java.net.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

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<java.net.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<java.net.SocketAddress>
Returns:
the maximum number of bytes that may be pending to be written to a particular Connection.

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<java.net.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<java.net.SocketAddress>
Parameters:
maxWriteReentrants - the maximum number of write() method calls a thread is allowed to make.

write

public GrizzlyFuture<WriteResult<Buffer,java.net.SocketAddress>> write(Connection connection,
                                                                       java.net.SocketAddress dstAddress,
                                                                       Buffer buffer,
                                                                       CompletionHandler<WriteResult<Buffer,java.net.SocketAddress>> completionHandler,
                                                                       Interceptor<WriteResult<Buffer,java.net.SocketAddress>> interceptor)
                                                                throws java.io.IOException
Description copied from interface: Writer
Method writes the buffer to the specific address.

Specified by:
write in interface Writer<java.net.SocketAddress>
Parameters:
connection - the Connection to write to
dstAddress - the destination address the buffer will be sent to
buffer - the buffer, from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed
interceptor - Interceptor, which will be able to intercept control each time new portion of a data was written from a buffer. The interceptor can decide, whether asynchronous write is completed or not, or provide other processing instructions.
Returns:
Future, using which it's possible to check the result
Throws:
java.io.IOException

write

public GrizzlyFuture<WriteResult<Buffer,java.net.SocketAddress>> write(Connection connection,
                                                                       java.net.SocketAddress dstAddress,
                                                                       Buffer buffer,
                                                                       CompletionHandler<WriteResult<Buffer,java.net.SocketAddress>> completionHandler,
                                                                       Interceptor<WriteResult<Buffer,java.net.SocketAddress>> interceptor,
                                                                       MessageCloner<Buffer> cloner)
                                                                throws java.io.IOException
Method writes the Buffer to the specific address.

Specified by:
write in interface AsyncQueueWriter<java.net.SocketAddress>
Parameters:
connection - the Connection to write to
dstAddress - the destination address the message will be sent to
buffer - the Buffer from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed
interceptor - Interceptor, which will be able to intercept control each time new portion of a data was written from a buffer. The interceptor can decide, whether asynchronous write is completed or not, or provide other processing instructions.
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
Returns:
Future, using which it's possible to check the result
Throws:
java.io.IOException

createRecord

protected AsyncWriteQueueRecord createRecord(Connection connection,
                                             Buffer message,
                                             java.util.concurrent.Future<WriteResult<Buffer,java.net.SocketAddress>> future,
                                             WriteResult<Buffer,java.net.SocketAddress> currentResult,
                                             CompletionHandler<WriteResult<Buffer,java.net.SocketAddress>> completionHandler,
                                             java.net.SocketAddress dstAddress,
                                             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.

processAsync

public boolean processAsync(Context context)
                     throws java.io.IOException
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:
true, if there are pending elements to be processed, or false otherwise.
Throws:
java.io.IOException

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

close

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

Specified by:
close in interface AsyncQueue

onWriteComplete

protected static void onWriteComplete(AsyncWriteQueueRecord record)
                               throws java.io.IOException
Throws:
java.io.IOException

onWriteIncomplete

protected final void onWriteIncomplete(AsyncWriteQueueRecord record)
                                throws java.io.IOException
Throws:
java.io.IOException

onWriteFailure

protected static void onWriteFailure(Connection connection,
                                     AsyncWriteQueueRecord failedRecord,
                                     java.io.IOException e)

failWriteRecord

protected static void failWriteRecord(AsyncWriteQueueRecord record,
                                      java.lang.Throwable e)

write0

protected abstract int write0(NIOConnection connection,
                              AsyncWriteQueueRecord queueRecord)
                       throws java.io.IOException
Throws:
java.io.IOException

onReadyToWrite

protected abstract void onReadyToWrite(Connection connection)
                                throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2011 Oracle Corpration. All Rights Reserved.