1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util.xa; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
|
15 | |
import java.util.ArrayList; |
16 | |
import java.util.Collection; |
17 | |
import java.util.Collections; |
18 | |
import java.util.Iterator; |
19 | |
|
20 | |
import javax.transaction.Status; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public abstract class AbstractResourceManager |
30 | |
{ |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public static final int SHUTDOWN_MODE_NORMAL = 0; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public static final int SHUTDOWN_MODE_ROLLBACK = 1; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public static final int SHUTDOWN_MODE_KILL = 2; |
46 | |
|
47 | |
protected static final int OPERATION_MODE_STOPPED = 0; |
48 | |
protected static final int OPERATION_MODE_STOPPING = 1; |
49 | |
protected static final int OPERATION_MODE_STARTED = 2; |
50 | |
protected static final int OPERATION_MODE_STARTING = 3; |
51 | |
protected static final int OPERATION_MODE_RECOVERING = 4; |
52 | |
|
53 | |
protected static final int DEFAULT_TIMEOUT_MSECS = 5000; |
54 | |
protected static final int DEFAULT_COMMIT_TIMEOUT_FACTOR = 2; |
55 | |
|
56 | 0 | protected Collection globalTransactions = Collections.synchronizedCollection(new ArrayList()); |
57 | 0 | protected int operationMode = OPERATION_MODE_STOPPED; |
58 | 0 | protected long defaultTimeout = DEFAULT_TIMEOUT_MSECS; |
59 | 0 | protected Log logger = getLogger(); |
60 | 0 | protected boolean dirty = false; |
61 | |
|
62 | |
protected abstract Log getLogger(); |
63 | |
|
64 | |
public synchronized void start() throws ResourceManagerSystemException |
65 | |
{ |
66 | 0 | logger.info("Starting ResourceManager"); |
67 | 0 | operationMode = OPERATION_MODE_STARTING; |
68 | |
|
69 | 0 | doStart(); |
70 | 0 | recover(); |
71 | |
|
72 | 0 | operationMode = OPERATION_MODE_STARTED; |
73 | 0 | if (dirty) |
74 | |
{ |
75 | 0 | logger |
76 | |
.warn("Started ResourceManager, but in dirty mode only (Recovery of pending transactions failed)"); |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | 0 | logger.info("Started ResourceManager"); |
81 | |
} |
82 | 0 | } |
83 | |
|
84 | |
protected void doStart() throws ResourceManagerSystemException |
85 | |
{ |
86 | |
|
87 | 0 | } |
88 | |
|
89 | |
protected void recover() throws ResourceManagerSystemException |
90 | |
{ |
91 | |
|
92 | 0 | } |
93 | |
|
94 | |
public synchronized void stop() throws ResourceManagerSystemException |
95 | |
{ |
96 | 0 | stop(SHUTDOWN_MODE_NORMAL); |
97 | 0 | } |
98 | |
|
99 | |
public synchronized boolean stop(int mode) throws ResourceManagerSystemException |
100 | |
{ |
101 | 0 | return stop(mode, getDefaultTransactionTimeout() * DEFAULT_COMMIT_TIMEOUT_FACTOR); |
102 | |
} |
103 | |
|
104 | |
public synchronized boolean stop(int mode, long timeOut) throws ResourceManagerSystemException |
105 | |
{ |
106 | 0 | logger.info("Stopping ResourceManager"); |
107 | 0 | operationMode = OPERATION_MODE_STOPPING; |
108 | |
|
109 | |
|
110 | 0 | boolean success = shutdown(mode, timeOut); |
111 | |
|
112 | |
|
113 | 0 | if (success) |
114 | |
{ |
115 | 0 | operationMode = OPERATION_MODE_STOPPED; |
116 | 0 | logger.info("Stopped ResourceManager"); |
117 | |
} |
118 | |
else |
119 | |
{ |
120 | 0 | logger.warn("Failed to stop ResourceManager"); |
121 | |
} |
122 | |
|
123 | 0 | return success; |
124 | |
} |
125 | |
|
126 | |
protected boolean shutdown(int mode, long timeoutMSecs) |
127 | |
{ |
128 | 0 | switch (mode) |
129 | |
{ |
130 | |
case SHUTDOWN_MODE_NORMAL : |
131 | 0 | return waitForAllTxToStop(timeoutMSecs); |
132 | |
case SHUTDOWN_MODE_ROLLBACK : |
133 | 0 | throw new UnsupportedOperationException(); |
134 | |
|
135 | |
case SHUTDOWN_MODE_KILL : |
136 | 0 | return true; |
137 | |
default : |
138 | 0 | return false; |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public long getDefaultTransactionTimeout() |
146 | |
{ |
147 | 0 | return defaultTimeout; |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public void setDefaultTransactionTimeout(long timeout) |
156 | |
{ |
157 | 0 | defaultTimeout = timeout; |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public AbstractTransactionContext startTransaction(Object session) throws ResourceManagerException |
176 | |
{ |
177 | 0 | return createTransactionContext(session); |
178 | |
} |
179 | |
|
180 | |
public void beginTransaction(AbstractTransactionContext context) throws ResourceManagerException |
181 | |
{ |
182 | |
|
183 | 0 | assureStarted(); |
184 | |
|
185 | 0 | synchronized (context) |
186 | |
{ |
187 | 0 | if (logger.isDebugEnabled()) |
188 | |
{ |
189 | 0 | logger.debug("Beginning transaction " + context); |
190 | |
} |
191 | 0 | doBegin(context); |
192 | 0 | context.status = Status.STATUS_ACTIVE; |
193 | 0 | if (logger.isDebugEnabled()) |
194 | |
{ |
195 | 0 | logger.debug("Began transaction " + context); |
196 | |
} |
197 | 0 | } |
198 | 0 | globalTransactions.add(context); |
199 | 0 | } |
200 | |
|
201 | |
public int prepareTransaction(AbstractTransactionContext context) throws ResourceManagerException |
202 | |
{ |
203 | 0 | assureReady(); |
204 | 0 | synchronized (context) |
205 | |
{ |
206 | 0 | if (logger.isDebugEnabled()) |
207 | |
{ |
208 | 0 | logger.debug("Preparing transaction " + context); |
209 | |
} |
210 | 0 | context.status = Status.STATUS_PREPARING; |
211 | 0 | int status = doPrepare(context); |
212 | 0 | context.status = Status.STATUS_PREPARED; |
213 | 0 | if (logger.isDebugEnabled()) |
214 | |
{ |
215 | 0 | logger.debug("Prepared transaction " + context); |
216 | |
} |
217 | 0 | return status; |
218 | 0 | } |
219 | |
} |
220 | |
|
221 | |
public void rollbackTransaction(AbstractTransactionContext context) throws ResourceManagerException |
222 | |
{ |
223 | 0 | assureReady(); |
224 | 0 | synchronized (context) |
225 | |
{ |
226 | 0 | if (logger.isDebugEnabled()) |
227 | |
{ |
228 | 0 | logger.debug("Rolling back transaction " + context); |
229 | |
} |
230 | |
try |
231 | |
{ |
232 | 0 | context.status = Status.STATUS_ROLLING_BACK; |
233 | 0 | doRollback(context); |
234 | 0 | context.status = Status.STATUS_ROLLEDBACK; |
235 | 0 | } |
236 | 0 | catch (Error e) |
237 | |
{ |
238 | 0 | setDirty(context, e); |
239 | 0 | throw e; |
240 | |
} |
241 | 0 | catch (RuntimeException e) |
242 | |
{ |
243 | 0 | setDirty(context, e); |
244 | 0 | throw e; |
245 | |
} |
246 | 0 | catch (ResourceManagerSystemException e) |
247 | |
{ |
248 | 0 | setDirty(context, e); |
249 | 0 | throw e; |
250 | |
} |
251 | |
finally |
252 | |
{ |
253 | 0 | globalTransactions.remove(context); |
254 | 0 | context.finalCleanUp(); |
255 | |
|
256 | 0 | context.notifyFinish(); |
257 | 0 | } |
258 | 0 | if (logger.isDebugEnabled()) |
259 | |
{ |
260 | 0 | logger.debug("Rolled back transaction " + context); |
261 | |
} |
262 | 0 | } |
263 | 0 | } |
264 | |
|
265 | |
public void setTransactionRollbackOnly(AbstractTransactionContext context) |
266 | |
throws ResourceManagerException |
267 | |
{ |
268 | 0 | context.status = Status.STATUS_MARKED_ROLLBACK; |
269 | 0 | } |
270 | |
|
271 | |
public void commitTransaction(AbstractTransactionContext context) throws ResourceManagerException |
272 | |
{ |
273 | 0 | assureReady(); |
274 | 0 | if (context.status == Status.STATUS_MARKED_ROLLBACK) |
275 | |
{ |
276 | 0 | throw new ResourceManagerException(CoreMessages.transactionMarkedForRollback()); |
277 | |
} |
278 | 0 | synchronized (context) |
279 | |
{ |
280 | 0 | if (logger.isDebugEnabled()) |
281 | |
{ |
282 | 0 | logger.debug("Committing transaction " + context); |
283 | |
} |
284 | |
try |
285 | |
{ |
286 | 0 | context.status = Status.STATUS_COMMITTING; |
287 | 0 | doCommit(context); |
288 | 0 | context.status = Status.STATUS_COMMITTED; |
289 | 0 | } |
290 | 0 | catch (Error e) |
291 | |
{ |
292 | 0 | setDirty(context, e); |
293 | 0 | throw e; |
294 | |
} |
295 | 0 | catch (RuntimeException e) |
296 | |
{ |
297 | 0 | setDirty(context, e); |
298 | 0 | throw e; |
299 | |
} |
300 | 0 | catch (ResourceManagerSystemException e) |
301 | |
{ |
302 | 0 | setDirty(context, e); |
303 | 0 | throw e; |
304 | |
} |
305 | 0 | catch (ResourceManagerException e) |
306 | |
{ |
307 | 0 | logger.warn("Could not commit tx " + context + ", rolling back instead", e); |
308 | 0 | doRollback(context); |
309 | 0 | } |
310 | |
finally |
311 | |
{ |
312 | 0 | globalTransactions.remove(context); |
313 | 0 | context.finalCleanUp(); |
314 | |
|
315 | 0 | context.notifyFinish(); |
316 | 0 | } |
317 | 0 | if (logger.isDebugEnabled()) |
318 | |
{ |
319 | 0 | logger.debug("Committed transaction " + context); |
320 | |
} |
321 | 0 | } |
322 | 0 | } |
323 | |
|
324 | |
protected abstract AbstractTransactionContext createTransactionContext(Object session); |
325 | |
|
326 | |
protected abstract void doBegin(AbstractTransactionContext context); |
327 | |
|
328 | |
protected abstract int doPrepare(AbstractTransactionContext context); |
329 | |
|
330 | |
protected abstract void doCommit(AbstractTransactionContext context) throws ResourceManagerException; |
331 | |
|
332 | |
protected abstract void doRollback(AbstractTransactionContext context) throws ResourceManagerException; |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
protected boolean waitForAllTxToStop(long timeoutMSecs) |
339 | |
{ |
340 | 0 | long startTime = System.currentTimeMillis(); |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
Collection transactionsToStop; |
351 | 0 | synchronized (globalTransactions) |
352 | |
{ |
353 | 0 | transactionsToStop = new ArrayList(globalTransactions); |
354 | 0 | } |
355 | 0 | for (Iterator it = transactionsToStop.iterator(); it.hasNext();) |
356 | |
{ |
357 | 0 | long remainingTimeout = startTime - System.currentTimeMillis() + timeoutMSecs; |
358 | |
|
359 | 0 | if (remainingTimeout <= 0) |
360 | |
{ |
361 | 0 | return false; |
362 | |
} |
363 | |
|
364 | 0 | AbstractTransactionContext context = (AbstractTransactionContext) it.next(); |
365 | 0 | synchronized (context) |
366 | |
{ |
367 | 0 | if (!context.finished) |
368 | |
{ |
369 | 0 | logger.info("Waiting for tx " + context + " to finish for " + remainingTimeout |
370 | |
+ " milli seconds"); |
371 | |
} |
372 | 0 | while (!context.finished && remainingTimeout > 0) |
373 | |
{ |
374 | |
try |
375 | |
{ |
376 | 0 | context.wait(remainingTimeout); |
377 | |
} |
378 | 0 | catch (InterruptedException e) |
379 | |
{ |
380 | 0 | return false; |
381 | 0 | } |
382 | 0 | remainingTimeout = startTime - System.currentTimeMillis() + timeoutMSecs; |
383 | |
} |
384 | 0 | if (context.finished) |
385 | |
{ |
386 | 0 | logger.info("Tx " + context + " finished"); |
387 | |
} |
388 | |
else |
389 | |
{ |
390 | 0 | logger.warn("Tx " + context + " failed to finish in given time"); |
391 | |
} |
392 | 0 | } |
393 | |
} |
394 | |
|
395 | 0 | return (globalTransactions.size() == 0); |
396 | |
} |
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
protected void setDirty(AbstractTransactionContext context, Throwable t) |
406 | |
{ |
407 | 0 | logger.error("Fatal error during critical commit/rollback of transaction " + context |
408 | |
+ ", setting resource manager to dirty.", t); |
409 | 0 | dirty = true; |
410 | 0 | } |
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
|
416 | |
|
417 | |
protected void assureStarted() throws ResourceManagerSystemException |
418 | |
{ |
419 | 0 | if (operationMode != OPERATION_MODE_STARTED) |
420 | |
{ |
421 | 0 | throw new ResourceManagerSystemException(CoreMessages.resourceManagerNotStarted()); |
422 | |
} |
423 | |
|
424 | |
|
425 | 0 | if (dirty) |
426 | |
{ |
427 | 0 | throw new ResourceManagerSystemException(CoreMessages.resourceManagerDirty()); |
428 | |
} |
429 | 0 | } |
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
protected void assureReady() throws ResourceManagerSystemException |
438 | |
{ |
439 | 0 | if (operationMode != OPERATION_MODE_STARTED && operationMode != OPERATION_MODE_STOPPING) |
440 | |
{ |
441 | 0 | throw new ResourceManagerSystemException(CoreMessages.resourceManagerNotReady()); |
442 | |
} |
443 | |
|
444 | |
|
445 | 0 | if (dirty) |
446 | |
{ |
447 | 0 | throw new ResourceManagerSystemException(CoreMessages.resourceManagerDirty()); |
448 | |
} |
449 | 0 | } |
450 | |
|
451 | |
} |