1 17 package org.apache.servicemix.components.util; 18 19 import java.util.Date ; 20 21 import javax.jbi.JBIException; 22 import javax.resource.spi.work.Work ; 23 import javax.resource.spi.work.WorkManager ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.servicemix.components.varscheduler.ScheduleIterator; 28 import org.apache.servicemix.components.varscheduler.Scheduler; 29 import org.apache.servicemix.components.varscheduler.SchedulerTask; 30 import org.apache.servicemix.jbi.framework.ComponentContextImpl; 31 32 38 public abstract class PollingComponentSupport extends ComponentSupport implements Work { 39 private static final Log log = LogFactory.getLog(PollingComponentSupport.class); 40 private WorkManager workManager; 41 private Scheduler scheduler; 42 private Date firstTime; 43 private long period = 5000; 44 private long delay; 45 private SchedulerTask schedulerTask; 46 private ScheduleIterator scheduleIterator; 47 private boolean started; 48 private boolean scheduleExecutedFlag; 49 50 55 public abstract void poll() throws Exception ; 56 57 public void release() { 58 } 59 60 public void run() { 61 try { 62 poll(); 63 } 64 catch (Exception e) { 65 log.error("Caught exception while polling: " + e, e); 66 } 67 } 68 69 public WorkManager getWorkManager() { 72 return workManager; 73 } 74 75 public void setWorkManager(WorkManager workManager) { 76 this.workManager = workManager; 77 } 78 79 public long getDelay() { 80 return delay; 81 } 82 83 public void setDelay(long delay) { 84 this.delay = delay; 85 } 86 87 public Date getFirstTime() { 88 return firstTime; 89 } 90 91 public void setFirstTime(Date firstTime) { 92 this.firstTime = firstTime; 93 } 94 95 public long getPeriod() { 96 return period; 97 } 98 99 public void setPeriod(long period) { 100 this.period = period; 101 } 102 103 public Scheduler getScheduler() { 104 return scheduler; 105 } 106 107 public void setScheduler(Scheduler scheduler) { 108 this.scheduler = scheduler; 109 } 110 111 public synchronized void start() throws JBIException { 112 if (!started) { 113 started = true; 114 if (schedulerTask != null) { 115 schedulerTask.cancel(); 116 } 117 schedulerTask = new PollSchedulerTask(); 118 this.scheduler.schedule(schedulerTask,scheduleIterator); 119 } 120 super.start(); 121 } 122 123 public synchronized void stop() throws JBIException { 124 if (schedulerTask != null) { 125 schedulerTask.cancel(); 126 schedulerTask = null; 127 } 128 scheduleExecutedFlag = false; 129 started = false; 130 super.stop(); 131 } 132 133 public synchronized void shutDown() throws JBIException { 134 stop(); 135 scheduler.cancel(); 136 scheduler = null; 137 scheduleIterator = null; 138 workManager = null; 139 super.shutDown(); 140 } 141 142 protected void init() throws JBIException { 145 if (scheduler == null) { 146 scheduler = new Scheduler(true); 147 } 148 if (scheduleIterator == null) { 149 scheduleIterator = new PollScheduleIterator(); 150 } 151 if (workManager == null) { 152 ComponentContextImpl context = (ComponentContextImpl) getContext(); 153 workManager = context.getWorkManager(); 154 } 155 super.init(); 156 157 } 158 159 private class PollSchedulerTask extends SchedulerTask { 160 public void run() { 161 try { 162 getWorkManager().doWork(PollingComponentSupport.this); 165 } 166 catch (Throwable e) { 167 log.error("Failed to schedule work: " + e, e); 168 } 169 } 170 } 171 172 private class PollScheduleIterator implements ScheduleIterator { 173 public Date nextExecution() { 174 long nextTime = System.currentTimeMillis(); 175 if (scheduleExecutedFlag) { 176 nextTime += period; 177 } else { 178 if (firstTime != null) { 179 nextTime = firstTime.getTime(); 180 } 181 nextTime += delay; 182 scheduleExecutedFlag = true; 183 } 184 return (started) ? new Date (nextTime) : null; 185 } 186 } 187 } | Popular Tags |