A work manager is a thread pool created for Java Platform, Enterprise Edition (Java EE) applications that use asynchronous beans.
The work managers provide a programming model for the Java EE 1.4 applications. For more information, see the Programming model section in this article.
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.
Work manager scheduled work runs in a
thread pool in the servant that is associated with that work manager.
The work manager does not control this work. The asynchronous bean
or CommonJ interface creates the work. Then, a workload management
(WLM) enclave runs the work, and WLM manages the work's thread based
on the enclave's service and report class. However, the enclave is
not associated with the service class in this servant or in any other
servant to which the service class is bound. Asynchronous beans are
entirely contained within the servant where they are created. For
more information about WLM enclave creation, see topic Work
requests.
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.
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.
InitialContext ic = new InitialContext(); WorkManager wm = (WorkManager)ic.lookup("java:comp/env/wm/myWorkManager");
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.
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) |
Asynchronous beans | CommonJ |
InitialContext ctx = new InitialContext(); com.ibm.websphere.asynchbeans.WorkManager wm = (com.ibm.websphere.asynchbeans.WorkManager) ctx.lookup(“java:comp/env/wm/MyWorkMgr”); |
InitialContext ctx = new InitialContext(); commonj.work.WorkManager wm = (commonj.work.WorkManager) ctx.lookup(“java:comp/env/wm/MyWorkMgr”); |
Asynchronous beans | CommonJ |
public class MyWork implements com.ibm.websphere.asynchbeans.Work { public void release() { ...... } public void run() { System.out.println(“Running.....”); } |
public class MyWork implements commonj.work.Work{ public boolean isDaemon() { return false; } public void release () { ..... } public void run () { System.out.println(“Running.....”); } |
Asynchronous beans | CommonJ |
MyWork work1 = new MyWork(); MyWork work2 = new MyWork(); WorkItem item1; WorkItem item2; Item1=wm.startWork(work1); Item2=wm.startWork(work2); // case 1: block until all items are done ArrayList col1 = new ArrayList(); Col1.add(item1); Col1.add(item2); wm.join(col1, WorkManager.JOIN_AND, WorkManager.INDEFINITE); // when the works are done System.out.println(“work1 data=”+work1.getData()); System.out.println(“work2 data=”+work2.getData()); // you should complete case 1 before case 2 //case 2: wait up to 1000 milliseconds for any of the items to complete. Boolean ret = wm.join(col1, WorkManager.JOIN_OR, 1000); |
MyWork work1 = new MyWork(); MyWork work2 = new MyWork(); WorkItem item1; WorkItem item2; Item1=wm.schedule(work1 ); Item2=wm.schedule(work2); // case 1: block until all items are done Collection col1 = new ArrayList(); col1.add(item1); col1.add(item2); wm.waitForAll(col1, WorkManager.INDEFINITE); // when the works are done System.out.println(“work1 data=”+work1.getData()); System.out.println(“work2 data=”+work2.getData()); // // you should complete case 1 before case 2 //case 2: wait up to 1000 milliseconds for any of the items to complete. Collection finished = wm.waitForAny(col1, 1000); // check the workItems status if (finished != null) { Iterator I = finished.iterator(); if (i.hasNext()) { WorkItem wi = (WorkItem) i.next(); if (wi.equals(item1)) { System.out.println(“work1 = “+ work1.getData()); } else if (wi.equals(item2)) { System.out.println(“work1 = “+ work1.getData()); } } } |
Asynchronous beans | CommonJ |
InitialContext ctx = new InitialContext(); com.ibm.websphere.asynchbeans.WorkManager wm = (com.ibm.websphere.asynchbeans.WorkManager) ctx.lookup(“java:comp/env/wm/MyWorkMgr”); AsynchScope ascope; Try { Ascope = wm.createAsynchScope(“ABScope”); } Catch (DuplicateKeyException ex) { Ascope = wm.findAsynchScope(“ABScope”); ex.printStackTrace(); } // get an AlarmManager AlarmManager aMgr= ascope.getAlarmManager(); |
InitialContext ctx = new InitialContext(); Commonj.timers.TimerManager tm = (commonj.timers.TimerManager) ctx.lookup(“java:comp/env/tm/MyTimerManager”); |
Asynchronous beans | CommonJ |
// create alarm ABAlarmListener listener = new ABAlarmListener(); Alarm am = aMgr.create(listener, “SomeContext”, 1000*60); |
// create Timer TimerListener listener = new StockQuoteTimerListener(“qqq”, “johndoe@example.com”); Timer timer = tm.schedule(listener, 1000*60); // Fixed-delay: schedule timer to expire in // 60 seconds from now and repeat every // hour thereafter. Timer timer = tm.schedule(listener, 1000*60, 1000*30); // Fixed-rate: schedule timer to expire in // 60 seconds from now and repeat every // hour thereafter Timer timer = tm.scheduleAtFixedRate(listener, 1000*60, 1000*30); |