FileDocCategorySizeDatePackage
DefaultAsyncHandler.javaAPI DocGlassfish v2 API8316Fri May 04 22:37:06 BST 2007com.sun.enterprise.web.connector.grizzly.async

DefaultAsyncHandler

public class DefaultAsyncHandler extends Object implements com.sun.enterprise.web.connector.grizzly.AsyncHandler
Default implementation of AsyncHandler. This class handle the aysnchronous execution of a ProcessorTask. The request processing is executed by doing: (1) Wrap the ProcessorTask using an instance of AsyncTask (2) Execute the AsyncTask using the wrapped ProcessorTask Pipeline (3) If the AsyncTask has been interrupted but ready to be removed from the interrupted queue, remove it and execute the remaining operations.
author
Jeanfrancois Arcand

Fields Summary
private ConcurrentLinkedQueue
asyncProcessors
Cache instance of AsyncTask
private ConcurrentLinkedQueue
interrruptedQueue
A queue used to cache interrupted AsyncTask.
private ArrayList
asyncFilters
The AsyncFilter to execute asynchronous operations on a ProcessorTask.
private String
asyncExecutorClassName
The AsyncExecutor class name to use.
Constructors Summary
public DefaultAsyncHandler()

    // ------------------------------------------------- Constructor --------//
    
    
      
    
Methods Summary
public voidaddAsyncFilter(com.sun.enterprise.web.connector.grizzly.AsyncFilter asyncFilter)
Add an AsyncFilter

        asyncFilters.add(asyncFilter);
    
public voidaddToInterruptedQueue(com.sun.enterprise.web.connector.grizzly.AsyncTask task)
Add a Task to the interrupted queue.

        interrruptedQueue.offer(task);
    
public java.lang.StringgetAsyncExecutorClassName()
Get the code>AsyncExecutor used by this object.

        return asyncExecutorClassName;
    
private com.sun.enterprise.web.connector.grizzly.AsyncTaskgetAsyncProcessorTask()
Return an instance of AsyncTask, which is configured and ready to be executed.

        AsyncTask asyncTask = asyncProcessors.poll();
        if ( asyncTask == null) {
            asyncTask = newAsyncProcessorTask();
        } else {
            asyncTask.recycle();
        }
        return asyncTask;
    
public voidhandle(com.sun.enterprise.web.connector.grizzly.Task task)
Handle an instance of a Task. This method is invoked first by a ProcessorTask, which delegate its execution to this handler. Second, this method is invoked once a ProcessorTask needs to be removed from the interrupted queue.

        
        AsyncTask apt = null;
        if (task.getType() == Task.PROCESSOR_TASK) {
            apt = getAsyncProcessorTask();
            apt.setProcessorTask((ProcessorTask)task);
            apt.setSelectorThread(task.getSelectorThread());
        }
        
        boolean wasInterrupted = interrruptedQueue.remove(task);
        if ( !wasInterrupted && apt == null) {
            String errorMsg = "";
            if ( task.getSelectionKey() != null ) {
                errorMsg = "Connection " + task.getSelectionKey().channel()
                            + " wasn't interrupted";
            } 
            throw new IllegalStateException(errorMsg);
        } else if ( apt == null ){
            apt = (AsyncTask)task;
        }
        apt.execute();
    
private com.sun.enterprise.web.connector.grizzly.AsyncExecutornewAsyncExecutor(com.sun.enterprise.web.connector.grizzly.AsyncTask asyncTask)
Create an instance of DefaultAsyncExecutor

        
        Class className = null; 
        AsyncExecutor asyncExecutor = null;
        try{                              
            className = Class.forName(asyncExecutorClassName);
            asyncExecutor = (AsyncExecutor)className.newInstance();
        } catch (ClassNotFoundException ex){
            throw new RuntimeException(ex);
        } catch (InstantiationException ex){
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex){
            throw new RuntimeException(ex);
        }
        
        if ( asyncExecutor != null ){
            asyncExecutor.setAsyncTask(asyncTask);
            asyncExecutor.setAsyncHandler(this);
            
            for (AsyncFilter l : asyncFilters){
                asyncExecutor.addAsyncFilter(l);
            }
        }
        return asyncExecutor;
    
private com.sun.enterprise.web.connector.grizzly.AsyncTasknewAsyncProcessorTask()
Create an instance of AsyncTask

        AsyncTask asyncTask = new AsyncProcessorTask();
        asyncTask.setAsyncExecutor(newAsyncExecutor(asyncTask));  
        return asyncTask;
    
public booleanremoveAsyncFilter(com.sun.enterprise.web.connector.grizzly.AsyncFilter asyncFilter)
Remove an AsyncFilter

        return asyncFilters.remove(asyncFilter);
    
public voidremoveFromInterruptedQueue(com.sun.enterprise.web.connector.grizzly.AsyncTask task)
Remove the Task from the interrupted queue.

        interrruptedQueue.remove(task);
    
public voidreturnTask(com.sun.enterprise.web.connector.grizzly.AsyncTask asyncTask)
Return th Task to the pool

        asyncProcessors.offer(asyncTask);
    
public voidsetAsyncExecutorClassName(java.lang.String asyncExecutorClassName)
Set the AsyncExecutor used by this object.

        this.asyncExecutorClassName = asyncExecutorClassName;