com.sun.enterprise.web.connector.grizzly
Class DefaultReadTask

java.lang.Object
  extended by com.sun.enterprise.web.connector.grizzly.TaskBase
      extended by com.sun.enterprise.web.connector.grizzly.DefaultReadTask
All Implemented Interfaces:
ReadTask, Task, TaskListener, Runnable, Callable
Direct Known Subclasses:
AsyncReadTask, ReadBlockingTask, SSLReadTask, XAReadTask

public class DefaultReadTask
extends TaskBase
implements ReadTask

Read available data on a non blocking SocketChannel. StreamAlgorithm stategy will decide if more bytes are required or not. Once the StreamAlgorithm is ready, the ProcessorTask attached to this class will be executed.

Author:
Scott Oaks, Jean-Francois Arcand

Field Summary
protected  StreamAlgorithm algorithm
          The Algorithm used to parse the request and determine of the bytes has been all read from the SocketChannel
protected  ByteBuffer byteBuffer
          The ByteBuffer used by this task to buffer the request stream.
protected  boolean bytesAvailable
          true only when another object has already read bytes from the channel.
protected  ByteBufferInputStream inputStream
          The recycled OutputStream used by this buffer.
protected  int maxPostSize
          Max post size.
protected  ProcessorTask processorTask
          The ProcessorTask used by this class.
protected  TaskContext taskContext
          The TaskContext instance associated with this object.
protected  TaskEvent taskEvent
          The TaskEvent instance used by this object to notify its listeners
protected  boolean useByteBufferView
          Create view ByteBuffer from another ByteBuffer
protected  boolean useDirectByteBuffer
          Is the ByteBuffer used by the ReadTask use direct ByteBuffer or not.
 
Fields inherited from class com.sun.enterprise.web.connector.grizzly.TaskBase
key, listeners, pipeline, recycle, selectorThread, type
 
Fields inherited from interface com.sun.enterprise.web.connector.grizzly.Task
ACCEPT_TASK, PROCESSOR_TASK, READ_TASK
 
Constructor Summary
DefaultReadTask()
           
 
Method Summary
 void attachProcessor(ProcessorTask processorTask)
          Force this task to always use the same ProcessorTask instance.
protected  void configureProcessorTask()
          Set appropriate attribute on the ProcessorTask.
 void detachProcessor()
          Return the ProcessorTask to the pool.
 void doTask()
          Read data from the socket and process it using this thread, and only if the StreamAlgorith stategy determine no more bytes are are needed.
protected  void doTask(ByteBuffer byteBuffer)
          Pull data from the socket and store it inside the ByteBuffer.
 boolean executeProcessorTask()
          Execute the ProcessorTask only if the request has been fully read.
protected  void finishConnection()
          Cancel the SelectionKey and close its underlying SocketChannel.
 ByteBuffer getByteBuffer()
          Return the underlying ByteBuffer used by this class.
 long getIdleTime()
          Return the time in milliseconds this Task is allowed to be idle.
 ProcessorTask getProcessorTask()
          Return the associated ProcessorTask.
 void initialize(StreamAlgorithm algorithm, boolean useDirectByteBuffer, boolean useByteBufferView)
           
protected  void manageKeepAlive(boolean keepAlive, int count, Exception exception)
          Evaluate if the SelectionKey needs to be registered to the main Selector
 void recycle()
          Clear the current state and make this object ready for another request.
 void registerKey()
          Register the SelectionKey with the Selector
protected  void returnTask()
          Return this object to the pool
 void setByteBuffer(ByteBuffer byteBuffer)
          Set the underlying ByteBuffer used by this class.
 void setBytesAvailable(boolean bytesAvailable)
          If the attached byteBuffer was already filled, tell the Algorithm to re-use the bytes.
 void setIdleTime(long idleTime)
          Set the time in milliseconds this Task is allowed to be idle.
 void taskEvent(TaskEvent event)
          This method is invoked when a WorkerThread starts processing a Task object.
 void terminate(boolean keepAlive)
          Complete the processing.
 
Methods inherited from class com.sun.enterprise.web.connector.grizzly.TaskBase
addTaskListener, call, cancelTask, clearTaskListeners, execute, fireTaskEvent, getKeepAliveStats, getPipeline, getRecycle, getRequestGroupInfo, getSelectionKey, getSelectorThread, getSocket, getTaskListeners, getType, isMonitoringEnabled, removeTaskListener, run, setPipeline, setRecycle, setSelectionKey, setSelectorThread
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sun.enterprise.web.connector.grizzly.Task
addTaskListener, cancelTask, clearTaskListeners, execute, getKeepAliveStats, getPipeline, getRecycle, getRequestGroupInfo, getSelectionKey, getSelectorThread, getTaskListeners, getType, isMonitoringEnabled, removeTaskListener, setPipeline, setRecycle, setSelectionKey, setSelectorThread
 
Methods inherited from interface java.lang.Runnable
run
 
Methods inherited from interface java.util.concurrent.Callable
call
 

Field Detail

taskContext

protected TaskContext taskContext
The TaskContext instance associated with this object. The TaskContext is initialized at startup and then recycled.


taskEvent

protected TaskEvent taskEvent
The TaskEvent instance used by this object to notify its listeners


byteBuffer

protected ByteBuffer byteBuffer
The ByteBuffer used by this task to buffer the request stream.


processorTask

protected ProcessorTask processorTask
The ProcessorTask used by this class.


maxPostSize

protected int maxPostSize
Max post size.


inputStream

protected ByteBufferInputStream inputStream
The recycled OutputStream used by this buffer.


algorithm

protected StreamAlgorithm algorithm
The Algorithm used to parse the request and determine of the bytes has been all read from the SocketChannel


bytesAvailable

protected boolean bytesAvailable
true only when another object has already read bytes from the channel.


useDirectByteBuffer

protected boolean useDirectByteBuffer
Is the ByteBuffer used by the ReadTask use direct ByteBuffer or not.


useByteBufferView

protected boolean useByteBufferView
Create view ByteBuffer from another ByteBuffer

Constructor Detail

DefaultReadTask

public DefaultReadTask()
Method Detail

initialize

public void initialize(StreamAlgorithm algorithm,
                       boolean useDirectByteBuffer,
                       boolean useByteBufferView)
Specified by:
initialize in interface ReadTask

attachProcessor

public void attachProcessor(ProcessorTask processorTask)
Force this task to always use the same ProcessorTask instance.

Specified by:
attachProcessor in interface ReadTask

configureProcessorTask

protected void configureProcessorTask()
Set appropriate attribute on the ProcessorTask.


detachProcessor

public void detachProcessor()
Return the ProcessorTask to the pool.

Specified by:
detachProcessor in interface ReadTask

doTask

public void doTask()
            throws IOException
Read data from the socket and process it using this thread, and only if the StreamAlgorith stategy determine no more bytes are are needed.

Specified by:
doTask in interface Task
Throws:
IOException

doTask

protected void doTask(ByteBuffer byteBuffer)
Pull data from the socket and store it inside the ByteBuffer. The StreamAlgorithM implementation will take care of determining if we need to register again to the main Selector or execute the request using temporary Selector

Parameters:
byteBuffer -

manageKeepAlive

protected void manageKeepAlive(boolean keepAlive,
                               int count,
                               Exception exception)
Evaluate if the SelectionKey needs to be registered to the main Selector


executeProcessorTask

public boolean executeProcessorTask()
                             throws IOException
Execute the ProcessorTask only if the request has been fully read. Guest the size of the request by using the content-type HTTP headers.

Returns:
false if the request wasn't fully read by the channel. so we need to respin the key on the Selector.
Throws:
IOException

returnTask

protected void returnTask()
Return this object to the pool


taskEvent

public void taskEvent(TaskEvent event)
Description copied from interface: TaskListener
This method is invoked when a WorkerThread starts processing a Task object.

Specified by:
taskEvent in interface TaskListener
Overrides:
taskEvent in class TaskBase

terminate

public void terminate(boolean keepAlive)
Complete the processing.

Specified by:
terminate in interface ReadTask

recycle

public void recycle()
Clear the current state and make this object ready for another request.

Specified by:
recycle in interface Task
Overrides:
recycle in class TaskBase

finishConnection

protected void finishConnection()
Cancel the SelectionKey and close its underlying SocketChannel. Add this Task to the Keep-Alive sub-system.


registerKey

public void registerKey()
Register the SelectionKey with the Selector


getProcessorTask

public ProcessorTask getProcessorTask()
Return the associated ProcessorTask.

Returns:
the associated ProcessorTask, null if not used.

getByteBuffer

public ByteBuffer getByteBuffer()
Return the underlying ByteBuffer used by this class.

Specified by:
getByteBuffer in interface ReadTask

setByteBuffer

public void setByteBuffer(ByteBuffer byteBuffer)
Set the underlying ByteBuffer used by this class.


setBytesAvailable

public void setBytesAvailable(boolean bytesAvailable)
If the attached byteBuffer was already filled, tell the Algorithm to re-use the bytes.

Specified by:
setBytesAvailable in interface ReadTask

setIdleTime

public void setIdleTime(long idleTime)
Set the time in milliseconds this Task is allowed to be idle.

Specified by:
setIdleTime in interface ReadTask

getIdleTime

public long getIdleTime()
Return the time in milliseconds this Task is allowed to be idle.

Specified by:
getIdleTime in interface ReadTask


Copyright © 2005-2015 Oracle Corporation. All Rights Reserved.