1 17 18 package org.apache.geronimo.timer; 19 20 import java.util.TimerTask ; 21 22 import javax.transaction.RollbackException ; 23 import javax.transaction.SystemException ; 24 import javax.transaction.Synchronization ; 25 import javax.transaction.Status ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 36 public class ExecutorFeedingTimerTask extends TimerTask { 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 e) { 58 log.warn("Exception canceling task", e); 59 throw (IllegalStateException ) new IllegalStateException ("RollbackException when trying to register Cancel Synchronization").initCause(e); 60 } catch (SystemException e) { 61 log.warn("Exception canceling task", e); 62 throw (IllegalStateException ) new IllegalStateException ("SystemException when trying to register Cancel Synchronization").initCause(e); 63 } 64 cancelled = true; 67 return super.cancel(); 68 } 69 70 public boolean isCancelled() { 71 return cancelled; 72 } 73 74 private void doCancel() { 75 try { 76 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 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 { 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 |