All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.enterprise.concurrent.ManagedExecutorService Maven / Gradle / Ivy

The newest version!
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2010-2018 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
 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
 * or 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 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 javax.enterprise.concurrent;

import java.util.concurrent.ExecutorService;

/**
 * A manageable version of a {@link java.util.concurrent.ExecutorService}.
 * 

* A ManagedExecutorService extends the Java™ SE ExecutorService to provide * methods for submitting tasks for execution in a Java™ EE environment. * Implementations of the ManagedExecutorService are * provided by a Java™ EE Product Provider. Application Component Providers * use the Java Naming and Directory Interface™ (JNDI) to look-up instances of one * or more ManagedExecutorService objects using resource environment references. * ManagedExecutorService instances can also be injected into application * components through the use of the {@code Resource} annotation. *

* The Concurrency Utilities for Java™ EE specification describes several * behaviors that a ManagedExecutorService can implement. The Application * Component Provider and Deployer identify these requirements and map the * resource environment reference appropriately. *

* The most common uses for a ManagedExecutorService is to run short-duration asynchronous * tasks such as for processing of asynchronous methods in Enterprise * JavaBean™ (EJB™) or for processing async tasks for Servlets that * supports asynchronous processing. *

* Tasks are run in managed threads provided by the Java™ EE Product Provider * and are run within the application component context that submitted the task. * All tasks run without an explicit transaction (they do not enlist in the application * component's transaction). If a transaction is required, use a * {@link javax.transaction.UserTransaction} instance. A UserTransaction instance is * available in JNDI using the name: "java:comp/UserTransaction" or by * requesting an injection of a {@link javax.transaction.UserTransaction} object * using the {@code Resource} annotation. *

* Example: *

 * public run() {
 *   // Begin of task
 *   InitialContext ctx = new InitialContext();
 *   UserTransaction ut = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
 *   ut.begin();
 *
 *   // Perform transactional business logic
 *
 *   ut.commit();
 * }
 * 
* * Tasks can optionally provide an {@link ManagedTaskListener} to receive * notifications of lifecycle events, through the use of {@link ManagedTask} * interface. *

* Example: *

 * public class MyRunnable implements Runnable, ManagedTask {
 *   ...
 *   public void run() {
 *     ...
 *   }
 * 
 *   public ManagedTaskListener getManagedTaskListener() {
 *     return myManagedTaskListener;
 *   }
 *   ...
 * }
 * 
 * MyRunnable task = ...;
 * ManagedExecutorService executor = ...;
 * 
 * executor.submit(task); // lifecycle events will be notified to myManagedTaskListener
 * 
* * Asynchronous tasks are typically submitted to the ManagedExecutorService using one * of the {@code submit} methods, each of which return a {@link java.util.concurrent.Future} * instance. The {@code Future} represents the result of the task and can also be used to * check if the task is complete or wait for its completion. *

* * If the task is canceled, the result for the task is a * {@link java.util.concurrent.CancellationException} exception. If the task is unable * to run due to a reason other than cancellation, the result is a * {@link AbortedException} exception. *

* *Example: *

 * /**
 *  * Retrieve all accounts from several account databases in parallel.
 *  * Resource Mappings:
 *  *  type:      javax.enterprise.concurrent.ManagedExecutorService
 *  *  jndi-name: concurrent/ThreadPool
 *  */
 * public List<Account> getAccounts(long accountId) {
 *   try {
 *       javax.naming.InitialContext ctx = new InitialContext();
 *       ManagedExecutorService mes = (ManagedExecutorService)
 *           ctx.lookup("java:comp/env/concurrent/ThreadPool");
 *
 *       // Create a set of tasks to perform the account retrieval.
 *       ArrayList<Callable<Account>> retrieverTasks = new ArrayList<Callable<Account>>();
 *       retrieverTasks.add(new EISAccountRetriever());
 *       retrieverTasks.add(new RDBAccountRetriever());
 *
 *       // Submit the tasks to the thread pool and wait for them
 *       // to complete (successfully or otherwise).
 *       List<Future<Account>> taskResults= mes.invokeAll(retrieverTasks);
 *
 *       // Retrieve the results from the resulting Future list.
 *       ArrayList<Account> results = new ArrayList<Account>();
 *       for(Future<Account> taskResult : taskResults) {
 *           try {
 *               results.add(taskResult.get());
 *           } catch (ExecutionException e) {
 *               Throwable cause = e.getCause();
 *               // Handle the AccountRetrieverError.
 *           }
 *       }
 *
 *       return results;
 *
 *   } catch (NamingException e) {
 *       // Throw exception for fatal error.
 *   } catch (InterruptedException e) {
 *       // Throw exception for shutdown or other interrupt condition.
 *   }
 * }
 *
 *
 * public class EISAccountRetriever implements Callable<Account> {
 *     public Account call() {
 *         // Connect to our eis system and retrieve the info for the account.
 *         //...
 *         return null;
 *   }
 * }
 *
 * public class RDBAccountRetriever implements Callable<Account>> {
 *     public Account call() {
 *         // Connect to our database and retrieve the info for the account.
 *         //...
 *   }
 * }
 *
 * public class Account {
 *     // Some account data...
 * }
 * 
*

* * @since 1.0 */ public interface ManagedExecutorService extends ExecutorService { }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy