KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > timer > TransactionalExecutorTask


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.timer;
19
20 import javax.transaction.TransactionManager JavaDoc;
21 import javax.transaction.Status JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public class TransactionalExecutorTask implements ExecutorTask {
30     private static final Log log = LogFactory.getLog(TransactionalExecutorTask.class);
31
32     private final Runnable JavaDoc userTask;
33     private final WorkInfo workInfo;
34     private final ThreadPooledTimer threadPooledTimer;
35
36     private final TransactionManager JavaDoc transactionManager;
37     private final int repeatCount;
38
39     public TransactionalExecutorTask(Runnable JavaDoc userTask, WorkInfo workInfo, ThreadPooledTimer threadPooledTimer, TransactionManager JavaDoc transactionManager, int repeatCount) {
40         this.userTask = userTask;
41         this.workInfo = workInfo;
42         this.threadPooledTimer = threadPooledTimer;
43         this.transactionManager = transactionManager;
44         this.repeatCount = repeatCount;
45     }
46
47     public void run() {
48         try {
49             // try to do the work until it succeeded or we reach the repeat count
50
boolean succeeded = false;
51             for (int tries = 0; !succeeded && tries < repeatCount; tries++) {
52                 try {
53                     if (!beginWork()) {
54                         break;
55                     }
56
57                     work();
58                 } finally {
59                     succeeded = completeWork();
60                 }
61             }
62
63             // if this was a one time thing, remove the job
64
if (workInfo.isOneTime()) {
65                 threadPooledTimer.removeWorkInfo(workInfo);
66             }
67
68             // if we didn't succeed, log it
69
if (!succeeded) {
70                 log.warn("Failed to execute work successfully");
71             }
72         } catch (RuntimeException JavaDoc e) {
73             log.warn("RuntimeException occured while running user task", e);
74             throw e;
75         } catch (Error JavaDoc e) {
76             log.warn("Error occured while running user task", e);
77             throw e;
78         }
79     }
80
81     private boolean beginWork() {
82         try {
83             transactionManager.begin();
84         } catch (Exception JavaDoc e) {
85             log.warn("Exception occured while starting container transaction", e);
86             return false;
87         }
88         return true;
89     }
90
91     private void work() {
92         try {
93             userTask.run();
94         } catch (Exception JavaDoc e) {
95             log.warn("Exception occured while running user task", e);
96         }
97     }
98
99     private boolean completeWork() {
100         try {
101             if (transactionManager.getStatus() == Status.STATUS_ACTIVE) {
102                 // clean up the work persistent data
103
try {
104                     threadPooledTimer.workPerformed(workInfo);
105                 } catch (PersistenceException e) {
106                     log.warn("Exception occured while updating timer persistent state", e);
107                 }
108
109                 // commit the tx
110
transactionManager.commit();
111
112                 // all is cool
113
return true;
114             } else {
115                 // tx was marked rollback, so roll it back
116
transactionManager.rollback();
117             }
118         } catch (Exception JavaDoc e) {
119             log.warn("Exception occured while completing container transaction", e);
120         }
121         // something bad happened
122
return false;
123     }
124
125 }
126
Popular Tags