Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
 package com.sun.enterprise.web.connector.grizzly.async;
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.

Jeanfrancois Arcand
 public class DefaultAsyncHandler implements AsyncHandler{
Cache instance of AsyncTask
     private Queue<AsyncTask>
             asyncProcessors = new ConcurrentQueue<AsyncTask>("DefaultAsyncHandler.asyncProcessors");
A queue used to cache interrupted AsyncTask.
     private Queue<AsyncTask>
             interruptedQueue = new ConcurrentQueue<AsyncTask>("DefaultAsyncHandler.interruptedQueue");
The AsyncFilter to execute asynchronous operations on a ProcessorTask.
     private ArrayList<AsyncFilterasyncFilters = 
             new ArrayList<AsyncFilter>();   
The AsyncExecutor class name to use.
     private String asyncExecutorClassName 
         = "com.sun.enterprise.web.connector.grizzly.async.DefaultAsyncExecutor";
     // ------------------------------------------------- Constructor --------//
    public DefaultAsyncHandler() {
Create an instance of AsyncTask
    private AsyncTask newAsyncProcessorTask(){
        AsyncTask asyncTask = new AsyncProcessorTask();
        return asyncTask;
Create an instance of DefaultAsyncExecutor
    private AsyncExecutor newAsyncExecutor(AsyncTask asyncTask){
        Class className = null
        AsyncExecutor asyncExecutor = null;
            className = Class.forName();
            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 ){
            for (AsyncFilter l : ){
        return asyncExecutor;
Return an instance of AsyncTask, which is configured and ready to be executed.
    private AsyncTask getAsyncProcessorTask(){
        AsyncTask asyncTask = .poll();
        if (asyncTask == null) {
            asyncTask = newAsyncProcessorTask();
        } else {
        return asyncTask;
    // ---------------------------------------------------- Interface -------//
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.
    public void handle(Task task){
        AsyncTask apt = null;
        if (task.getType() == .) {
            apt = getAsyncProcessorTask();
        boolean wasInterrupted = .remove(apt);
        if ( !wasInterrupted && apt == null) {
            // Try to remove the ProcessorTask itself
        // Last chance to execute asynchronously.
        if ( apt == null ){
            apt = (AsyncTask)task;
        if (apt != null){

Return th Task to the pool
    public void returnTask(AsyncTask asyncTask){
Add a Task to the interrupted queue.
    public void addToInterruptedQueue(AsyncTask task){
Remove the Task from the interrupted queue.
    public void removeFromInterruptedQueue(AsyncTask task){
Set the AsyncExecutor used by this object.
    public void setAsyncExecutorClassName(String asyncExecutorClassName){
        this. = asyncExecutorClassName;
Get the code>AsyncExecutor used by this object.
        return ;
Add an AsyncFilter
    public void addAsyncFilter(AsyncFilter asyncFilter) {

Remove an AsyncFilter
    public boolean removeAsyncFilter(AsyncFilter asyncFilter) {
        return .remove(asyncFilter);
New to GrepCode? Check out our FAQ X