public interface ManagedThreadFactory extends ThreadFactory
ThreadFactory
.A ManagedThreadFactory extends the Java™ SE ThreadFactory to provide a method for creating threads for execution in a Java™ EE environment. Implementations of the ManagedThreadFactory 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 ManagedThreadFactory objects using resource environment references.
The Concurrency Utilities for Java™ EE specification describes several behaviors that a ManagedThreadFactory can implement. The Application Component Provider and Deployer identify these requirements and map the resource environment reference appropriately.
Threads returned from the newThread()
method should implement the
ManageableThread
interface.
The Runnable task that is allocated to the new thread using the
ThreadFactory.newThread(Runnable)
method
will run with the application component context of the component instance
that created (looked-up) this ManagedThreadFactory instance.
The task runs without an explicit transaction (they do not enlist in the application
component's transaction). If a transaction is required, use a
javax.transaction.UserTransaction
instance. A UserTransaction instance is
available in JNDI using the name: "java:comp/UserTransaction"
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(); }A ManagedThreadFactory can be used with Java SE ExecutorService implementations directly.
Example:
/** * Create a ThreadPoolExecutor using a ManagedThreadFactory. * Resource Mappings: * type: javax.enterprise.concurrent.ManagedThreadFactory * jndi-name: concurrent/tf/DefaultThreadFactory */ @Resource(name="concurrent/tf/DefaultThreadFactory") ManagedThreadFactory tf; public ExecutorService getManagedThreadPool() { // All threads will run as part of this application component. return new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10), tf); }
newThread
Copyright © 1996-2015, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.