1 28 package de.nava.informa.utils.toolkit; 29 30 import de.nava.informa.core.ChannelIF; 31 32 import java.util.IdentityHashMap ; 33 import java.util.Map ; 34 import java.util.Timer ; 35 import java.util.TimerTask ; 36 37 46 public class Scheduler { 47 private Timer timer; 48 private SchedulerCallbackIF callback; 49 private Map timers = new IdentityHashMap (); 50 51 56 public Scheduler(SchedulerCallbackIF callback) { 57 this.callback = callback; 58 59 timer = new Timer (true); 60 } 61 62 69 public final void schedule(ChannelIF channel, long period, int priority) { 70 ChannelRecord record = new ChannelRecord(channel, period, priority); 72 73 resched(record, period); 74 } 75 76 81 public final void unschedule(ChannelIF channel) { 82 ChannelRecord record = null; 83 synchronized (timers) { 84 SchedulerTask tt = (SchedulerTask) timers.get(channel); 85 if (tt != null) { 86 timers.remove(channel); 87 tt.cancel(); 88 record = tt.getRecord(); 89 record.setCanceled(true); 90 } 91 } 92 } 93 94 99 public final void triggerNow(ChannelIF channel) { 100 final SchedulerTask task; 101 synchronized (timers) { 102 task = (SchedulerTask) timers.get(channel); 103 } 104 if (task != null) { 105 final ChannelRecord record = task.getRecord(); 106 resched(record, record.getPeriod()); 107 } 108 } 109 110 115 public final synchronized void rescheduleAll(long period) { 116 final ChannelIF[] channels; 117 118 synchronized (timers) { 120 channels = (ChannelIF[]) timers.keySet().toArray(new ChannelIF[0]); 121 } 122 123 for (int i = 0; i < channels.length; i++) { 125 final ChannelIF channel = channels[i]; 126 rescheduleChannel(channel, period); 127 } 128 } 129 130 137 public final void rescheduleChannel(final ChannelIF channel, long period) { 138 final SchedulerTask task = (SchedulerTask) timers.get(channel); 139 140 if (task == null) { 141 schedule(channel, period, ChannelRecord.PRIO_NORMAL); 142 } else { 143 final ChannelRecord record = task.getRecord(); 144 145 synchronized (timers) { 147 timers.remove(channel); 148 } 149 150 task.cancel(); 151 152 long timePassed = System.currentTimeMillis() - task.scheduledExecutionTime(); 159 long delay = 0; 160 if (timePassed >= 0) { 161 delay = period - timePassed; 162 if (delay < 0) { 163 delay = 0; 164 } 165 } 166 167 sched(record, delay, period); 168 } 169 } 170 171 177 private void resched(ChannelRecord record, long period) { 178 ChannelIF channel = record.getChannel(); 179 180 unschedule(channel); 182 183 sched(record, 0, period); 185 } 186 187 194 private void sched(ChannelRecord record, long delay, long period) { 195 record.setCanceled(false); 196 197 ChannelIF channel = record.getChannel(); 198 SchedulerTask tt = new SchedulerTask(record); 199 200 synchronized (timers) { 201 timers.put(channel, tt); 202 } 203 204 timer.schedule(tt, delay, period); 205 } 206 207 210 private class SchedulerTask extends TimerTask { 211 private ChannelRecord record; 212 213 218 public SchedulerTask(ChannelRecord record) { 219 this.record = record; 220 } 221 222 225 public void run() { 226 callback.process(record); 227 } 228 229 234 public ChannelRecord getRecord() { 235 return record; 236 } 237 } 238 } 239 | Popular Tags |