KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.TimerTask JavaDoc;
21
22 import javax.transaction.RollbackException JavaDoc;
23 import javax.transaction.SystemException JavaDoc;
24 import javax.transaction.Synchronization JavaDoc;
25 import javax.transaction.Status JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  *
32  *
33  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
34  *
35  * */

36 public class ExecutorFeedingTimerTask extends TimerTask JavaDoc {
37
38     private static final Log log = LogFactory.getLog(ExecutorFeedingTimerTask.class);
39
40     private final WorkInfo workInfo;
41     private final ThreadPooledTimer threadPooledTimer;
42     boolean cancelled = false;
43
44     public ExecutorFeedingTimerTask(WorkInfo workInfo, ThreadPooledTimer threadPooledTimer) {
45         this.workInfo = workInfo;
46         this.threadPooledTimer = threadPooledTimer;
47     }
48
49     public void run() {
50         threadPooledTimer.getExecutor().execute(workInfo.getExecutorTask());
51     }
52
53     public boolean cancel() {
54         threadPooledTimer.removeWorkInfo(workInfo);
55         try {
56             threadPooledTimer.registerSynchronization(new CancelSynchronization(this));
57         } catch (RollbackException JavaDoc e) {
58             log.warn("Exception canceling task", e);
59             throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("RollbackException when trying to register Cancel Synchronization").initCause(e);
60         } catch (SystemException JavaDoc e) {
61             log.warn("Exception canceling task", e);
62             throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("SystemException when trying to register Cancel Synchronization").initCause(e);
63         }
64         // One cancels the task at this specific time. If the transaction is
65
// rolled-back, one will recreate it.
66
cancelled = true;
67         return super.cancel();
68     }
69
70     public boolean isCancelled() {
71         return cancelled;
72     }
73
74     private void doCancel() {
75         try {
76             // Impacts the timer storage only if the timer is cancelled
77
// in the scope of a committed transactions.
78
threadPooledTimer.getWorkerPersistence().cancel(workInfo.getId());
79         } catch (PersistenceException e) {
80             log.warn("Exception canceling task", e);
81         }
82     }
83
84     private void rollbackCancel() {
85         threadPooledTimer.addWorkInfo(workInfo);
86         
87         // The transaction has been rolled-back, we need to restore the
88
// task as if cancel has been called.
89
if ( workInfo.isOneTime() ) {
90             threadPooledTimer.getTimer().schedule(
91                 new ExecutorFeedingTimerTask(workInfo, threadPooledTimer),
92                 workInfo.getTime());
93         } else if ( workInfo.getAtFixedRate() ) {
94             threadPooledTimer.getTimer().scheduleAtFixedRate(
95                 new ExecutorFeedingTimerTask(workInfo, threadPooledTimer),
96                 workInfo.getTime(), workInfo.getPeriod().longValue());
97         } else {
98             threadPooledTimer.getTimer().schedule(
99                 new ExecutorFeedingTimerTask(workInfo, threadPooledTimer),
100                 workInfo.getTime(), workInfo.getPeriod().longValue());
101         }
102     }
103     
104     private static class CancelSynchronization implements Synchronization JavaDoc {
105
106         private final ExecutorFeedingTimerTask worker;
107
108         public CancelSynchronization(ExecutorFeedingTimerTask worker) {
109             this.worker = worker;
110         }
111
112         public void beforeCompletion() {
113         }
114
115         public void afterCompletion(int status) {
116             if (status == Status.STATUS_COMMITTED) {
117                 worker.doCancel();
118             } else if (status == Status.STATUS_ROLLEDBACK) {
119                 worker.rollbackCancel();
120             }
121         }
122
123     }
124
125 }
126
Popular Tags