Work managers
A work manager is a thread pool created for Java Platform, Enterprise Edition (Java EE) applications that use asynchronous beans.
- Asynchronous beans
- CommonJ work manager (For details, see the CommonJ work manager section in this article.)
- ExecutorService work manager
The work managers provide a programming model for the Java EE 1.4 applications. For more information, see the Programming model section.
The javax.resource.spi.work.WorkManager class is a Java interface to be used by
Java EE Connector Architecture (JCA) resource adapters. It is not an actual implementation of
the WorkManager which is used by Java EE applications.
When writing a Web or Enterprise JavaBeans (EJB) component that uses asynchronous beans, the developer should include a resource reference in each component that needs access to a work manager. For more information on resource references, refer to the References topic. The component looks up a work manager using a logical name in the component, java:comp namespace, just as it looks up a data source, enterprise bean or connection factory.
The deployer binds physical work managers to logical work managers when the application is deployed.
For example, if a developer needs three thread pools to partition work between bronze, silver, and gold levels, the developer writes the component to pick a logical pool based on an attribute in the client application profile. The deployer has the flexibility to decide how to map this request for three thread pools. The deployer might decide to use a single thread pool on a small machine. In this case, the deployer binds all three resource references to the same work manager instance (that is, the same JNDI name). A larger machine might support three thread pools, so the deployer binds each resource reference to a different work manager. Work managers can be shared between multiple Java™ EE applications installed on the same server.
An application developer can use as many logical work managers as necessary. The deployer chooses whether to map one physical work manager or several to the logical work manager defined in the application.
All Java EE components that need to share asynchronous scope objects must use the same work manager. These scope objects have an affinity with a single work manager. An application that uses asynchronous scopes should verify that all of the components using scope objects use the same work manager.
When multiple work managers are defined, the underlying thread pools are created in a Java virtual machine (JVM) only if an application within that JVM looks up the work manager. For example, there might be ten thread pools (work managers) defined, but none are actually created until an application looks these pools up.
CommonJ Work Manager
The CommonJ work manager is similar to the work manager. The difference between the two is that the CommonJ work manager contains a subset of the asynchronous beans work manager methods. Although CommonJ work manager functions in a Java EE 1.4 environment, the interface does not return a new instance for each JNDI naming lookup, since this specification is not included in the Java EE specification.
Remote start of work. The CommonJ Work specification optional feature for work running remotely is not supported. Even if a unit of work implements the java.io.Serializable interface, the unit of work does not run remotely.
How to look up a work manager
InitialContext ic = new InitialContext();
WorkManager wm = (WorkManager)ic.lookup("java:comp/env/wm/myWorkManager");Inheritance Java EE contexts
- Internationalization context
- When this option is selected and the internationalization service is enabled, and the internationalization context that exists on the scheduling thread is available on the target thread.
- Work area
- When this option is selected, the work area context for every work area partition that exists on the scheduling thread is available on the target thread.
- Application profile (deprecated)
- Application profile context is not supported and not available for Java EE 1.4 applications. For Java EE 1.3 applications, when this option is selected, the application profile service is enabled, and the application profile service property, 5.x compatibility mode, is selected. The application profile task that is associated with the scheduling thread is available on the target thread for Java EE 1.3 applications. For Java EE 1.4 applications, the application profile task is a property of its associated unit of work, rather than a thread. This option has no effect on the behavior of the task in Java EE 1.4 applications. The scheduled work that runs in a Java EE 1.4 application does not receive the application profiling task of the scheduling thread.
- Security
- The asynchronous bean can be run as anonymous or as the client authenticated on the thread that created it. This behavior is useful because the asynchronous bean can do only what the caller can do. This action is more useful than a RUN_AS mechanism, for example, which prevents this kind of behavior. When you select the Security option, the JAAS subject that exists on the scheduling thread is available on the target thread. If not selected, the thread runs anonymously.
- Component metadata
- Component metadata is relevant only when the asynchronous bean is a simple Java object. If the bean is a Java EE component, such as an enterprise bean, the component metadata is active.
The contexts that can be inherited depend on the work manager used by the application that creates the asynchronous bean. Using the administrative console, the administrator defines the sticky context policy of a work manager by selecting the services on which the work manager is to be made available.
Programming model
- CommonJ Specification. The Application Server Version 6.0 CommonJ programming model uses the WorkManager and TimerManager to manage threads and timers asynchronously in the Java EE 1.4 environment.
- Asynchronous beans and CommonJ specification extensions. The current asynchronous beans Event Source, asynchronous scopes, subsystem monitors and Java EE Context interfaces are a part of the CommonJ extension.
| CommonJ package | API | Asynchronous beans package | API |
|---|---|---|---|
| Work manager | Work manager | ||
| Asynchronous beans | Field - IMMEDIATE (long) | Field - IMMEDIATE (int) | |
| Field - INDEFINITE | Field - INDEFINITE | ||
| schedule(Work) throws WorkException, IllegalArgumentException | startWork(Work) throws WorkException, IllegalArgumentException | ||
| schedule(Work, WorkListener) throws WorkException,
IllegalArgumentException Important: Configure the work
manager work timeout property to the value you previously specified
as timeout_ms on startWork. The default timeout
value is INDEFINITE.
|
startWork(Work, timeout_ms, WorkListener) throws WorkException, IllegalArgumentException | ||
| waitForAll(workItems, timeout_ms) | join(workItems, JOIN_AND, timeout_ms) | ||
| waitForAny(workItems, timeout_ms) | join(workItems, JOIN_OR, timeout_ms) | ||
| WorkItem | WorkItem | ||
| getResult | getResult | ||
| getStatus | getStatus | ||
| WorkListener | WorkListener | ||
| workAccepted(WorkEvent) | workAccepted(WorkEvent) | ||
| workCompleted(WorkEvent) | workCompleted(WorkEvent) | ||
| workRejected(WorkEvent) | workRejected(WorkEvent) | ||
| workStarted(WorkEvent) | workStarted(WorkEvent) | ||
| WorkEvent | WorkEvent | ||
| Field - WORK_ACCEPTED | Field - WORK_ACCEPTED | ||
| Field - WORK_COMPLETED | Field - WORK_COMPLETED | ||
| Field - WORK_REJECTED | Field - WORK_REJECTED | ||
| Field - WORK_STARTED | Field - WORK_STARTED | ||
| getException | getException | ||
| getType | getType | ||
| getWorkItem().getResult() Important: This
API is valid only after the work is complete.
|
getWork | ||
| Work | (extends Runnable) | Work | (Extends Runnable) |
| isDaemon | * | ||
| release | release | ||
| RemoteWorkItem | RemoteWorkItem capability is not provided by WebSphere Application Sever. Use Distributed WorkManager in the WebSphere Extended Deployment product. | NA | |
| TimerManager | AlarmManager | ||
| resume | * | ||
| schedule(Listener, Date) | create(Listener, context, time) ** need to convert the parameters | ||
| schedule(Listener, Date, period) | |||
| schedule(Listener, delay, period) | |||
| scheduleAtFixedRate (Listener, Date, period) | |||
| scheduleAtFixedRate (Listener, delay, period) | |||
| stop | |||
| suspend | |||
| Timer | Alarm | ||
| cancel | cancel | ||
| getPeriod | |||
| getTimerListener | getAlarmListener | ||
| scheduledExecutionTime | |||
| TimerListener | AlarmListener | ||
| timerExpired(timer) | fired(alarm) | ||
| StopTimerListener | Not applicable | ||
| timerStop(timer) | |||
| CancelTimerListener | Not applicable | ||
| timerCancel(timer) | |||
| WorkException | (Extends Exception) | WorkException | (Extends WsException) |
| WorkCompletedException | (Extends WorkException) | WorkCompletedException | (Extends WorkException) |
| WorkRejectedException | (Extends WorkException) | WorkRejectedException | (Extends WorkException) |
Work manager examples
| Asynchronous beans | CommonJ | ExecutorService |
|---|---|---|
|
|
|
| Asynchronous beans | CommonJ | ExecutorService |
|---|---|---|
|
|
|
| Asynchronous beans | CommonJ | ExecutorService |
|---|---|---|
|
|
|
Timer manager examples
| Asynchronous beans | CommonJ |
|---|---|
|
|
| Asynchronous beans | CommonJ |
|---|---|
|
|