FileDocCategorySizeDatePackage
AsyncReadTask.javaAPI DocGlassfish v2 API7025Tue Jul 10 13:20:24 BST 2007com.sun.enterprise.web.connector.grizzly

AsyncReadTask

public class AsyncReadTask extends DefaultReadTask
This task execute and then notify its listener, which usualy execute on their own thread. The task perform the same operation as its parent, but the attached ProcessorTask execute on its own thread.
author
Jean-Francois Arcand

Fields Summary
Constructors Summary
public AsyncReadTask()

        super();
    
Methods Summary
public voidconfigureProcessorTask()
Set appropriate attribute on the ProcessorTask.

        super.configureProcessorTask();
        if ( !getTaskListeners().contains(processorTask) ){
            processorTask.addTaskListener(this);
            addTaskListener((TaskListener)processorTask);
        }
    
public voiddoTask()
Read data from the socket and process it using this thread, and only if the StreamAlgorith stategy determine no more bytes are are needed.

  
        doTask(byteBuffer);
    
public booleanexecuteProcessorTask()
Execute the ProcessorTask

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

                  
        boolean registerKey = false;
        
        if (SelectorThread.logger().isLoggable(Level.FINEST))
            SelectorThread.logger().log(Level.FINEST,"executeProcessorTask");
        
        if (  algorithm.getHandler() != null && algorithm.getHandler()
                .handle(null, Handler.REQUEST_BUFFERED) == Handler.BREAK ){
            return true;
        }
        
        // Get a processor task. If the processorTask != null, that means we
        // failed to load all the bytes in a single channel.read().
        if (processorTask == null){
            attachProcessor(selectorThread.getProcessorTask());
        } else {
            // When spawnProcessorTask is true, we must reconfigure the 
            // ProcessorTask
            configureProcessorTask();
        }        
        
        if (taskEvent == null){
            taskContext = new TaskContext();
            taskEvent = new TaskEvent<TaskContext>(taskContext);
        }
   
        // Call the listener and execute the task on it's own pipeline.
        taskEvent.setStatus(TaskEvent.START);        
        taskContext.setInputStream(inputStream);
        taskEvent.attach(taskContext);
        fireTaskEvent(taskEvent); 
        return false;
    
public voidinitialize(StreamAlgorithm algorithm, boolean useDirectByteBuffer, boolean useByteBufferView)

        super.initialize(algorithm,useDirectByteBuffer,useByteBufferView);
        byteBuffer = algorithm.allocate(useDirectByteBuffer,
                useByteBufferView,selectorThread.getBufferSize());
    
protected voidmanageKeepAlive(boolean keepAlive, int count, java.lang.Exception exception)
Manage the SelectionKey

         

        // The key is invalid when the Task has been cancelled.
        if ( count <= 0 || exception != null){
            if ( exception != null){
                // Make sure we have detached the processorTask
                detachProcessor();
                if (SelectorThread.logger().isLoggable(Level.FINE)){
                    SelectorThread.logger().log
                       (Level.FINE, 
                           "SocketChannel Read Exception:",exception);
                }
            }
            terminate(false);
        }
    
public voidrecycle()
Clear the current state and make this object ready for another request.

        byteBuffer = algorithm.postParse(byteBuffer);   
        byteBuffer.clear(); 
        inputStream.recycle();
        algorithm.recycle();
        key = null;
        inputStream.setSelectionKey(null);                             
    
public voidsetByteBuffer(java.nio.ByteBuffer srcBB)
Set the underlying ByteBuffer used by this class.

        if ( srcBB != byteBuffer){
            if (srcBB.capacity() > byteBuffer.capacity()) {
                byteBuffer = algorithm.allocate(useDirectByteBuffer,
                                                useByteBufferView,srcBB.capacity()); 
            }
            srcBB.flip();
            byteBuffer.put(srcBB);
            srcBB.clear();
        } else {
            byteBuffer = srcBB;
        }
    
public voidterminate(boolean keepAlive)
Complete the transaction.

     
        if (keepAlive){
            detachProcessor();        
            registerKey(); 
            returnTask();
        } else {
            super.terminate(keepAlive);    
        }