Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 package org.apache.tuscany.sca.core.work.impl;
 
 import java.util.Map;
 import java.util.UUID;
 
A thread-pool based implementation for the JSR-237 work manager.

This implementation supports only local work.

TODO Elaborate the implementation.

Version:
$Rev: 998309 $ $Date: 2010-09-17 23:37:49 +0300 (Fri, 17 Sep 2010) $
 
 public class ThreadPoolWorkManager {
 
     // Map of work items currently handled by the work manager
 
     // Thread-pool
     protected ExecutorService executor;

    
Initializes the thread-pool.

Parameters:
threadPoolSize Thread-pool size. If the size <1, then a cached pool is created
 
     public ThreadPoolWorkManager(int threadPoolSize) {
         ThreadFactory factory = new ThreadFactory() {
             public Thread newThread(Runnable r) {
                 Thread thread = new Thread(r);
                 thread.setDaemon(true);
                 return thread;
             }
         };
         if (threadPoolSize <= 0) {
 
             // Creates a new Executor, use a custom ThreadFactory that
             // creates daemon threads.
              = Executors.newCachedThreadPool(factory);
         } else {
              = Executors.newFixedThreadPool(threadPoolSizefactory);
         }
     }

    
Schedules a unit of work asynchronously.

Parameters:
work Work that needs to be scheduled.
Returns:
Work Work item representing the asynchronous work
 
     public WorkItem schedule(Work workthrows IllegalArgumentException {
         return schedule(worknull);
     }

    
Schedules a unit of work asynchronously.

Parameters:
work Work that needs to be scheduled.
workListener Work listener for callbacks.
Returns:
Work Work item representing the asynchronous work
 
     public WorkItem schedule(Work workWorkListener workListenerthrows IllegalArgumentException {
 
         WorkItem workItem = new WorkItem(UUID.randomUUID().toString(), work);
         if (workListener != null) {
             .put(workItemworkListener);
         }
         workAccepted(workItemwork);
         if (scheduleWork(workworkItem)) {
             return workItem;
         } else {
            workItem.setStatus(.);
            if (workListener != null) {
                workListener.workRejected(new WorkEvent(workItem));
            }
            throw new IllegalArgumentException("Unable to schedule work");
        }
    }

    
Wait for all the specified units of work to finish.

Parameters:
works Units of the work that need to finish.
timeout Timeout for waiting for the units of work to finish.
    public boolean waitForAll(Collection workslong timeout) {
        throw new UnsupportedOperationException("waitForAll not supported");
    }

    
Wait for any of the specified units of work to finish.

Parameters:
works Units of the work that need to finish.
timeout Timeout for waiting for the units of work to finish.
    public Collection waitForAny(Collection workslong timeout) {
        throw new UnsupportedOperationException("waitForAny not supported");
    }

    
Method provided for subclasses to indicate a work acceptance.

Parameters:
workItem Work item representing the work that was accepted.
work Work that was accepted.
    private void workAccepted(final WorkItem workItemfinal Work work) {
        WorkListener listener = .get(workItem);
        if (listener != null) {
            workItem.setStatus(.);
            WorkEvent event = new WorkEvent(workItem);
            listener.workAccepted(event);
        }
    }
    /*
     * Method to indicate a work start.
     */
    private void workStarted(final WorkItem workItemfinal Work work) {
        WorkListener listener = .get(workItem);
        if (listener != null) {
            workItem.setStatus(.);
            WorkEvent event = new WorkEvent(workItem);
            listener.workStarted(event);
        }
    }
    /*
     * Method to indicate a work completion.
     */
    private void workCompleted(final WorkItem workItemfinal Work work) {
        workCompleted(workItemworknull);
    }
    /*
     * Method to indicate a work completion.
     */
    private void workCompleted(final WorkItem workItemfinal Work workfinal WorkSchedulerException exception) {
        WorkListener listener = .get(workItem);
        if (listener != null) {
            workItem.setStatus(.);
            workItem.setResult(work);
            workItem.setException(exception);
            WorkEvent event = new WorkEvent(workItem);
            listener.workCompleted(event);
            .remove(workItem);
        }
    }
    /*
     * Schedules the work using the ThreadPool.
     */
    private boolean scheduleWork(final Work workfinal WorkItem workItem) {
        try {
            .execute(new DecoratingWork(workItemwork));
            return true;
        } catch (RejectedExecutionException ex) {
            return false;
        }
    }
    /*
     * Class that decorates the original worker so that it can get callbacks when work is done.
     */
    private final class DecoratingWork implements Runnable {
        // Work item for this work.
        private WorkItem workItem;
        // The original work.
        private Work decoratedWork;
        /*
         * Initializes the work item and underlying work.
         */
        private DecoratingWork(final WorkItem workItemfinal Work decoratedWork) {
            this. = workItem;
            this. = decoratedWork;
        }
        /*
         * Overrides the run method.
         */
        public void run() {
            workStarted();
            try {
                .run();
                workCompleted();
            } catch (Throwable th) {
                workCompleted(new WorkSchedulerException(th.getMessage(), th));
            }
        }
    }
    public void destroy() {
        .shutdown();
    }
        return ;
    }
New to GrepCode? Check out our FAQ X