KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > commonj > WorkManagerTaskExecutor


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.commonj;
18
19 import java.util.Collection JavaDoc;
20
21 import javax.naming.NamingException JavaDoc;
22
23 import commonj.work.Work;
24 import commonj.work.WorkException;
25 import commonj.work.WorkItem;
26 import commonj.work.WorkListener;
27 import commonj.work.WorkManager;
28 import commonj.work.WorkRejectedException;
29
30 import org.springframework.beans.factory.InitializingBean;
31 import org.springframework.core.task.TaskRejectedException;
32 import org.springframework.jndi.JndiLocatorSupport;
33 import org.springframework.scheduling.SchedulingException;
34 import org.springframework.scheduling.SchedulingTaskExecutor;
35 import org.springframework.util.Assert;
36
37 /**
38  * TaskExecutor implementation that delegates to a CommonJ WorkManager,
39  * implementing the {@link commonj.work.WorkManager} interface,
40  * which either needs to be specified as reference or through the JNDI name.
41  *
42  * <p><b>This is the central convenience class for setting up a
43  * CommonJ WorkManager in a Spring context.</b>
44  *
45  * <p>Also implements the CommonJ WorkManager interface itself, delegating all
46  * calls to the target WorkManager. Hence, a caller can choose whether it wants
47  * to talk to this executor through the Spring TaskExecutor interface or the
48  * CommonJ WorkManager interface.
49  *
50  * <p>The CommonJ WorkManager will usually be retrieved from the application
51  * server's JNDI environment, as defined in the server's management console.
52  *
53  * <p><b>Note: At the time of this writing, the CommonJ WorkManager facility
54  * is only supported on IBM WebSphere 6.0+ and BEA WebLogic 9.0+,
55  * despite being such a crucial API for an application server.</b>
56  * (There is a similar facility available on WebSphere 5.0 Enterprise,
57  * though, which we will discuss below.)
58  *
59  * <p>A similar facility is available on WebSphere 5.0/5.1, under the name
60  * "Asynch Beans". Its central interface is called WorkManager too and is
61  * also obtained from JNDI, just like a standard CommonJ WorkManager.
62  * However, this WorkManager variant is notably different: The central
63  * execution method is called "startWork" instead of "schedule",
64  * and takes a slightly different Work interface as parameter.
65  *
66  * <p>Support for this WebSphere 5 variant can be built with this class
67  * and its helper DelegatingWork as template: Call the WorkManager's
68  * <code>startWork(Work)</code> instead of <code>schedule(Work)</code>
69  * in the <code>execute(Runnable)</code> implementation. Furthermore,
70  * for simplicity's sake, drop the entire "Implementation of the CommonJ
71  * WorkManager interface" section (and the corresponding
72  * <code>implements WorkManager</code> clause at the class level).
73  * Of course, you also need to change all <code>commonj.work</code> imports in
74  * your WorkManagerTaskExecutor and DelegatingWork variants to the corresponding
75  * WebSphere API imports (<code>com.ibm.websphere.asynchbeans.WorkManager</code>
76  * and <code>com.ibm.websphere.asynchbeans.Work</code>, respectively).
77  * This should be sufficient to get a TaskExecutor adapter for WebSphere 5.
78  *
79  * @author Juergen Hoeller
80  * @since 2.0
81  */

82 public class WorkManagerTaskExecutor extends JndiLocatorSupport
83         implements SchedulingTaskExecutor, WorkManager, InitializingBean {
84
85     private WorkManager workManager;
86
87     private String JavaDoc workManagerName;
88
89     private WorkListener workListener;
90
91
92     /**
93      * Specify the CommonJ WorkManager to delegate to.
94      * <p>Alternatively, you can also specify the JNDI name
95      * of the target WorkManager.
96      * @see #setWorkManagerName
97      */

98     public void setWorkManager(WorkManager workManager) {
99         this.workManager = workManager;
100     }
101
102     /**
103      * Set the JNDI name of the CommonJ WorkManager.
104      * <p>This can either be a fully qualified JNDI name,
105      * or the JNDI name relative to the current environment
106      * naming context if "resourceRef" is set to "true".
107      * @see #setWorkManager
108      * @see #setResourceRef
109      */

110     public void setWorkManagerName(String JavaDoc workManagerName) {
111         this.workManagerName = workManagerName;
112     }
113
114     /**
115      * Specify a CommonJ WorkListener to apply, if any.
116      * <p>This shared WorkListener instance will be passed on to the
117      * WorkManager by all {@link #execute} calls on this TaskExecutor.
118      */

119     public void setWorkListener(WorkListener workListener) {
120         this.workListener = workListener;
121     }
122
123     public void afterPropertiesSet() throws NamingException JavaDoc {
124         if (this.workManager == null) {
125             if (this.workManagerName == null) {
126                 throw new IllegalArgumentException JavaDoc("Either 'workManager' or 'workManagerName' must be specified");
127             }
128             this.workManager = (WorkManager) lookup(this.workManagerName, WorkManager.class);
129         }
130     }
131
132
133     //-------------------------------------------------------------------------
134
// Implementation of the Spring SchedulingTaskExecutor interface
135
//-------------------------------------------------------------------------
136

137     public void execute(Runnable JavaDoc task) {
138         Assert.state(this.workManager != null, "No WorkManager specified");
139         Work work = new DelegatingWork(task);
140         try {
141             if (this.workListener != null) {
142                 this.workManager.schedule(work, this.workListener);
143             }
144             else {
145                 this.workManager.schedule(work);
146             }
147         }
148         catch (WorkRejectedException ex) {
149             throw new TaskRejectedException("CommonJ WorkManager did not accept task: " + task, ex);
150         }
151         catch (WorkException ex) {
152             throw new SchedulingException("Could not schedule task on CommonJ WorkManager", ex);
153         }
154     }
155
156     /**
157      * This task executor prefers short-lived work units.
158      */

159     public boolean prefersShortLivedTasks() {
160         return true;
161     }
162
163
164     //-------------------------------------------------------------------------
165
// Implementation of the CommonJ WorkManager interface
166
//-------------------------------------------------------------------------
167

168     public WorkItem schedule(Work work)
169             throws WorkException, IllegalArgumentException JavaDoc {
170
171         return this.workManager.schedule(work);
172     }
173
174     public WorkItem schedule(Work work, WorkListener workListener)
175             throws WorkException, IllegalArgumentException JavaDoc {
176
177         return this.workManager.schedule(work, workListener);
178     }
179
180     public boolean waitForAll(Collection JavaDoc workItems, long timeout)
181             throws InterruptedException JavaDoc, IllegalArgumentException JavaDoc {
182
183         return this.workManager.waitForAll(workItems, timeout);
184     }
185
186     public Collection JavaDoc waitForAny(Collection JavaDoc workItems, long timeout)
187             throws InterruptedException JavaDoc, IllegalArgumentException JavaDoc {
188
189         return this.workManager.waitForAny(workItems, timeout);
190     }
191
192 }
193
Popular Tags