1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 package javax.resource.spi.work;
38
39 import java.lang.Object;
40 import java.lang.Runnable;
41 import java.lang.Exception;
42 import java.lang.Throwable;
43
44 /**
45 * This interface models a <code>WorkManager</code> which provides a facility
46 * to submit <code>Work</code> instances for execution. This frees the user
47 * from having to create Java threads directly to do work. Further, this
48 * allows efficient pooling of thread resources and more control over thread
49 * usage.
50 *
51 * The various stages in <code>Work</code> processing are:
52 * <ul>
53 * <li> work submit: A <code>Work</code> instance is being submitted for
54 * execution. The <code>Work</code> instance could either be accepted or
55 * rejected with a <code>WorkRejectedException</code> set to an appropriate
56 * error code. </li>
57 *
58 * <li> work accepted: The submitted <code>Work</code> instance has been
59 * accepted. The accepted <code>Work</code> instance could either start
60 * execution or could be rejected again with a
61 * <code>WorkRejectedException</code> set to an appropriate error code.
62 * There is no guarantee on when the execution would start unless a start
63 * timeout duration is specified. When a start timeout is specified, the
64 * <code>Work</code> execution must be started within the specified
65 * duration (not a real-time guarantee), failing which a
66 * <code>WorkRejectedException</code> set to an error code
67 * (<code>WorkRejected.TIMED_OUT</code>) is thrown. </li>
68 *
69 * <li> work rejected: The <code>Work</code> instance has been rejected.
70 * The <code>Work</code> instance could be rejected during <code>Work</code>
71 * submittal or after the <code>Work</code> instance has been accepted
72 * (but before Work instance starts execution). The rejection could be due
73 * to internal factors or start timeout expiration. A
74 * <code>WorkRejectedException</code> with an appropriate error code
75 * (indicates the reason) is thrown in both cases. </li>
76 *
77 * <li> work started: The execution of the <code>Work</code>
78 * instance has started. This means that a thread
79 * has been allocated for its execution. But this
80 * does not guarantee that the allocated thread has been scheduled to run
81 * on a CPU resource. Once execution is started, the allocated thread
82 * sets up an appropriate execution context (transaction , security, etc)
83 * and calls Work.run(). Note, any exception thrown during execution context
84 * setup or Work.run() leads to completion of processing. </li>
85 *
86 * <li> work completed: The execution of the <code>Work</code> has been
87 * completed. The execution could complete with or without an exception.
88 * The <code>WorkManager</code> catches any exception thrown during
89 * <code>Work</code> processing (which includes execution context setup),
90 * and wraps it with a <code>WorkCompletedException</code>. </li>
91 * </ul>
92 *
93 * @version 1.0
94 * @author Ram Jeyaraman
95 */
96 public interface WorkManager {
97
98 /**
99 * A constant to indicate timeout duration. A zero timeout value indicates
100 * an action be performed immediately.
101 */
102 long IMMEDIATE = 0L;
103
104 /**
105 * A constant to indicate timeout duration. A maximum timeout value
106 * indicates that an action be performed arbitrarily without any time
107 * constraint.
108 */
109 long INDEFINITE = Long.MAX_VALUE;
110
111 /**
112 * A constant to indicate an unknown start delay duration or other unknown
113 * values.
114 */
115 long UNKNOWN = -1;
116
117 /**
118 * Accepts a <code>Work</code> instance for processing. This call
119 * blocks until the <code>Work</code> instance completes execution.
120 * There is no guarantee on when the accepted <code>Work</code>
121 * instance would start execution ie., there is no time constraint
122 * to start execution.
123 *
124 * @param work The unit of work to be done.
125 * Could be long or short-lived.
126 *
127 * @throws WorkRejectedException indicates that a
128 * <code>Work</code> instance has been rejected from further processing.
129 * This can occur due to internal factors.
130 *
131 * @throws WorkCompletedException indicates that a
132 * <code>Work</code> instance has completed execution with an exception.
133 */
134 void doWork(Work work) // startTimeout = INDEFINITE
135 throws WorkException;
136
137 /**
138 * Accepts a <code>Work</code> instance for processing. This call
139 * blocks until the <code>Work</code> instance completes execution.
140 *
141 * @param work The unit of work to be done.
142 * Could be long or short-lived.
143 *
144 * @param startTimeout a time duration (in milliseconds)
145 * within which the execution of the <code>Work</code> instance must
146 * start. Otherwise, the <code>Work</code> instance is rejected with a
147 * <code>WorkRejectedException</code> set to an appropriate error code
148 * (<code>WorkRejectedException.TIMED_OUT</code>). Note, this
149 * does not offer real-time guarantees.
150 *
151 * @param execContext an object containing the execution
152 * context with which the submitted <code>Work</code> instance must
153 * be executed.
154 *
155 * @param workListener an object which would be notified
156 * when the various <code>Work</code> processing events (work accepted,
157 * work rejected, work started, work completed) occur.
158 *
159 * @throws WorkRejectedException indicates that a
160 * <code>Work</code> instance has been rejected from further processing.
161 * This can occur due to internal factors or start timeout expiration.
162 *
163 * @throws WorkCompletedException indicates that a
164 * <code>Work</code> instance has completed execution with an exception.
165 */
166 void doWork(Work work, long startTimeout,
167 ExecutionContext execContext, WorkListener workListener)
168 throws WorkException;
169
170 /**
171 * Accepts a <code>Work</code> instance for processing. This call
172 * blocks until the <code>Work</code> instance starts execution
173 * but not until its completion. There is no guarantee on when
174 * the accepted <code>Work</code> instance would start
175 * execution ie., there is no time constraint to start execution.
176 *
177 * @param work The unit of work to be done.
178 * Could be long or short-lived.
179 *
180 * @return the time elapsed (in milliseconds) from <code>Work</code>
181 * acceptance until start of execution. Note, this does not offer
182 * real-time guarantees. It is valid to return -1, if the actual start
183 * delay duration is unknown.
184 *
185 * @throws WorkRejectedException indicates that a
186 * <code>Work</code> instance has been rejected from further processing.
187 * This can occur due to internal factors.
188 */
189 long startWork(Work work) // startTimeout = INDEFINITE
190 throws WorkException;
191
192 /**
193 * Accepts a <code>Work</code> instance for processing. This call
194 * blocks until the <code>Work</code> instance starts execution
195 * but not until its completion. There is no guarantee on when
196 * the accepted <code>Work</code> instance would start
197 * execution ie., there is no time constraint to start execution.
198 *
199 * @param work The unit of work to be done.
200 * Could be long or short-lived.
201 *
202 * @param startTimeout a time duration (in milliseconds)
203 * within which the execution of the <code>Work</code> instance must
204 * start. Otherwise, the <code>Work</code> instance is rejected with a
205 * <code>WorkRejectedException</code> set to an appropriate error code
206 * (<code>WorkRejectedException.TIMED_OUT</code>). Note, this
207 * does not offer real-time guarantees.
208 *
209 * @param execContext an object containing the execution
210 * context with which the submitted <code>Work</code> instance must
211 * be executed.
212 *
213 * @param workListener an object which would be notified
214 * when the various <code>Work</code> processing events (work accepted,
215 * work rejected, work started, work completed) occur.
216 *
217 * @return the time elapsed (in milliseconds) from <code>Work</code>
218 * acceptance until start of execution. Note, this does not offer
219 * real-time guarantees. It is valid to return -1, if the actual start
220 * delay duration is unknown.
221 *
222 * @throws WorkRejectedException indicates that a
223 * <code>Work</code> instance has been rejected from further processing.
224 * This can occur due to internal factors or start timeout expiration.
225 */
226 long startWork(Work work, long startTimeout,
227 ExecutionContext execContext, WorkListener workListener)
228 throws WorkException;
229
230 /**
231 * Accepts a <code>Work</code> instance for processing. This call
232 * does not block and returns immediately once a <code>Work</code>
233 * instance has been accepted for processing. There is no guarantee
234 * on when the submitted <code>Work</code> instance would start
235 * execution ie., there is no time constraint to start execution.
236 *
237 * @param work The unit of work to be done.
238 * Could be long or short-lived.
239 *
240 * @throws WorkRejectedException indicates that a
241 * <code>Work</code> instance has been rejected from further processing.
242 * This can occur due to internal factors.
243 */
244 void scheduleWork(Work work) // startTimeout = INDEFINITE
245 throws WorkException;
246
247 /**
248 * Accepts a <code>Work</code> instance for processing. This call
249 * does not block and returns immediately once a <code>Work</code>
250 * instance has been accepted for processing.
251 *
252 * @param work The unit of work to be done.
253 * Could be long or short-lived.
254 *
255 * @param startTimeout a time duration (in milliseconds)
256 * within which the execution of the <code>Work</code> instance must
257 * start. Otherwise, the <code>Work</code> instance is rejected with a
258 * <code>WorkRejectedException</code> set to an appropriate error code
259 * (<code>WorkRejectedException.TIMED_OUT</code>). Note, this
260 * does not offer real-time guarantees.
261 *
262 * @param execContext an object containing the execution
263 * context with which the submitted <code>Work</code> instance must
264 * be executed.
265 *
266 * @param workListener an object which would be notified
267 * when the various <code>Work</code> processing events (work accepted,
268 * work rejected, work started, work completed) occur.
269 *
270 * @throws WorkRejectedException indicates that a
271 * <code>Work</code> instance has been rejected from further processing.
272 * This can occur due to internal factors.
273 */
274 void scheduleWork(Work work, long startTimeout,
275 ExecutionContext execContext, WorkListener workListener)
276 throws WorkException;
277 }