KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > work > 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.jca.work;
18
19 import javax.resource.spi.BootstrapContext JavaDoc;
20 import javax.resource.spi.work.ExecutionContext JavaDoc;
21 import javax.resource.spi.work.Work JavaDoc;
22 import javax.resource.spi.work.WorkException JavaDoc;
23 import javax.resource.spi.work.WorkListener JavaDoc;
24 import javax.resource.spi.work.WorkManager JavaDoc;
25 import javax.resource.spi.work.WorkRejectedException JavaDoc;
26
27 import org.springframework.core.task.AsyncTaskExecutor;
28 import org.springframework.core.task.TaskRejectedException;
29 import org.springframework.core.task.TaskTimeoutException;
30 import org.springframework.scheduling.SchedulingException;
31 import org.springframework.scheduling.SchedulingTaskExecutor;
32 import org.springframework.util.Assert;
33
34 /**
35  * {@link org.springframework.core.task.TaskExecutor} implementation
36  * that delegates to a JCA 1.5 WorkManager, implementing the
37  * {@link javax.resource.spi.work.WorkManager} interface.
38  *
39  * <p>This is mainly intended for use within a JCA ResourceAdapter implementation,
40  * but may also be used in a standalone environment, delegating to a locally
41  * embedded WorkManager implementation (such as Geronimo's).
42  *
43  * <p>Also implements the JCA 1.5 WorkManager interface itself, delegating all
44  * calls to the target WorkManager. Hence, a caller can choose whether it wants
45  * to talk to this executor through the Spring TaskExecutor interface or the
46  * JCA 1.5 WorkManager interface.
47  *
48  * @author Juergen Hoeller
49  * @since 2.0.3
50  * @see #setWorkManager
51  * @see javax.resource.spi.work.WorkManager#scheduleWork
52  */

53 public class WorkManagerTaskExecutor implements SchedulingTaskExecutor, AsyncTaskExecutor, WorkManager JavaDoc {
54
55     private WorkManager JavaDoc workManager = new SimpleTaskWorkManager();
56
57     private boolean blockUntilStarted = false;
58
59     private boolean blockUntilCompleted = false;
60
61     private WorkListener JavaDoc workListener;
62
63
64     /**
65      * Create a new WorkManagerTaskExecutor, expecting bean-style configuration.
66      * @see #setWorkManager
67      */

68     public WorkManagerTaskExecutor() {
69     }
70
71     /**
72      * Create a new WorkManagerTaskExecutor for the given WorkManager.
73      * @param workManager the JCA WorkManager to delegate to
74      */

75     public WorkManagerTaskExecutor(WorkManager JavaDoc workManager) {
76         setWorkManager(workManager);
77     }
78
79
80     /**
81      * Specify the JCA WorkManager to delegate to.
82      */

83     public void setWorkManager(WorkManager JavaDoc workManager) {
84         Assert.notNull(workManager, "WorkManager must not be null");
85         this.workManager = workManager;
86     }
87
88     /**
89      * Specify the JCA BootstrapContext that contains the
90      * WorkManager to delegate to.
91      */

92     public void setBootstrapContext(BootstrapContext JavaDoc bootstrapContext) {
93         Assert.notNull(bootstrapContext, "BootstrapContext must not be null");
94         this.workManager = bootstrapContext.getWorkManager();
95     }
96
97     /**
98      * Set whether to let {@link #execute} block until the work
99      * has been actually started.
100      * <p>Uses the JCA <code>startWork</code> operation underneath,
101      * instead of the default <code>scheduleWork</code>.
102      * @see javax.resource.spi.work.WorkManager#startWork
103      * @see javax.resource.spi.work.WorkManager#scheduleWork
104      */

105     public void setBlockUntilStarted(boolean blockUntilStarted) {
106         this.blockUntilStarted = blockUntilStarted;
107     }
108
109     /**
110      * Set whether to let {@link #execute} block until the work
111      * has been completed.
112      * <p>Uses the JCA <code>doWork</code> operation underneath,
113      * instead of the default <code>scheduleWork</code>.
114      * @see javax.resource.spi.work.WorkManager#doWork
115      * @see javax.resource.spi.work.WorkManager#scheduleWork
116      */

117     public void setBlockUntilCompleted(boolean blockUntilCompleted) {
118         this.blockUntilCompleted = blockUntilCompleted;
119     }
120
121     /**
122      * Specify a JCA 1.5 WorkListener to apply, if any.
123      * <p>This shared WorkListener instance will be passed on to the
124      * WorkManager by all {@link #execute} calls on this TaskExecutor.
125      */

126     public void setWorkListener(WorkListener JavaDoc workListener) {
127         this.workListener = workListener;
128     }
129
130
131     //-------------------------------------------------------------------------
132
// Implementation of the Spring SchedulingTaskExecutor interface
133
//-------------------------------------------------------------------------
134

135     public void execute(Runnable JavaDoc task) {
136         execute(task, TIMEOUT_INDEFINITE);
137     }
138
139     public void execute(Runnable JavaDoc task, long startTimeout) {
140         Assert.state(this.workManager != null, "No WorkManager specified");
141         Work JavaDoc work = new DelegatingWork(task);
142         try {
143             if (this.blockUntilCompleted) {
144                 if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) {
145                     this.workManager.doWork(work, startTimeout, null, this.workListener);
146                 }
147                 else {
148                     this.workManager.doWork(work);
149                 }
150             }
151             else if (this.blockUntilStarted) {
152                 if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) {
153                     this.workManager.startWork(work, startTimeout, null, this.workListener);
154                 }
155                 else {
156                     this.workManager.startWork(work);
157                 }
158             }
159             else {
160                 if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) {
161                     this.workManager.scheduleWork(work, startTimeout, null, this.workListener);
162                 }
163                 else {
164                     this.workManager.scheduleWork(work);
165                 }
166             }
167         }
168         catch (WorkRejectedException JavaDoc ex) {
169             if (WorkException.START_TIMED_OUT.equals(ex.getErrorCode())) {
170                 throw new TaskTimeoutException("JCA WorkManager rejected task because of timeout: " + task, ex);
171             }
172             else {
173                 throw new TaskRejectedException("JCA WorkManager rejected task: " + task, ex);
174             }
175         }
176         catch (WorkException JavaDoc ex) {
177             throw new SchedulingException("Could not schedule task on JCA WorkManager", ex);
178         }
179     }
180
181     /**
182      * This task executor prefers short-lived work units.
183      */

184     public boolean prefersShortLivedTasks() {
185         return true;
186     }
187
188
189     //-------------------------------------------------------------------------
190
// Implementation of the JCA WorkManager interface
191
//-------------------------------------------------------------------------
192

193     public void doWork(Work JavaDoc work) throws WorkException JavaDoc {
194         this.workManager.doWork(work);
195     }
196
197     public void doWork(Work JavaDoc work, long delay, ExecutionContext JavaDoc executionContext, WorkListener JavaDoc workListener)
198             throws WorkException JavaDoc {
199
200         this.workManager.doWork(work, delay, executionContext, workListener);
201     }
202
203     public long startWork(Work JavaDoc work) throws WorkException JavaDoc {
204         return this.workManager.startWork(work);
205     }
206
207     public long startWork(Work JavaDoc work, long delay, ExecutionContext JavaDoc executionContext, WorkListener JavaDoc workListener)
208             throws WorkException JavaDoc {
209
210         return this.workManager.startWork(work, delay, executionContext, workListener);
211     }
212
213     public void scheduleWork(Work JavaDoc work) throws WorkException JavaDoc {
214         this.workManager.scheduleWork(work);
215     }
216
217     public void scheduleWork(Work JavaDoc work, long delay, ExecutionContext JavaDoc executionContext, WorkListener JavaDoc workListener)
218             throws WorkException JavaDoc {
219
220         this.workManager.scheduleWork(work, delay, executionContext, workListener);
221     }
222
223 }
224
Popular Tags