1 28 package de.nava.informa.utils.poller; 29 30 import de.nava.informa.core.ChannelIF; 31 import de.nava.informa.utils.toolkit.WorkerThread; 32 import de.nava.informa.utils.toolkit.WorkerThreadFactoryIF; 33 import de.nava.informa.utils.toolkit.WorkersManager; 34 import de.nava.informa.utils.toolkit.Scheduler; 35 import de.nava.informa.utils.toolkit.ChannelRecord; 36 import de.nava.informa.utils.toolkit.SchedulerCallbackIF; 37 38 67 public class Poller { 68 private static final long DEFAULT_POLL_PERIOD = 3600000; private static final int DEFAULT_WORKER_THREADS = 5; 70 71 private WorkersManager workersManager; 72 private Scheduler scheduler; 73 74 private CompositeObserver compositeObserver; 75 private CompositeApprover compositeApprover; 76 77 private long globalPollPeriod = DEFAULT_POLL_PERIOD; 78 79 82 public Poller() { 83 this(DEFAULT_WORKER_THREADS); 84 } 85 86 91 public Poller(int workerThreads) { 92 compositeObserver = new CompositeObserver(); 94 compositeApprover = new CompositeApprover(); 95 96 workersManager = new WorkersManager(new PollerThreadFactory(), workerThreads); 98 99 scheduler = new Scheduler(new SchedulerCallback()); 101 } 102 103 110 public final void setWorkerThreads(int count) { 111 workersManager.setWorkerThreads(count); 112 } 113 114 119 public final void registerChannel(ChannelIF channel) { 120 scheduler.schedule(channel, globalPollPeriod, ChannelRecord.PRIO_NORMAL); 121 } 122 123 129 public final void registerChannel(ChannelIF channel, long period) { 130 scheduler.schedule(channel, period, ChannelRecord.PRIO_NORMAL); 131 } 132 133 139 public final void updateChannel(ChannelIF channel) { 140 scheduler.triggerNow(channel); 141 } 142 143 148 public final void unregisterChannel(ChannelIF channel) { 149 scheduler.unschedule(channel); 150 } 151 152 157 public final void addObserver(PollerObserverIF observer) { 158 compositeObserver.add(observer); 159 } 160 161 166 public final void removeObserver(PollerObserverIF observer) { 167 compositeObserver.remove(observer); 168 } 169 170 175 public final void addApprover(PollerApproverIF approver) { 176 compositeApprover.add(approver); 177 } 178 179 184 public final void removeApprover(PollerApproverIF approver) { 185 compositeApprover.remove(approver); 186 } 187 188 193 public final void setPeriod(long period) { 194 this.globalPollPeriod = period; 195 scheduler.rescheduleAll(period); 196 } 197 198 201 private class SchedulerCallback implements SchedulerCallbackIF { 202 207 public void process(ChannelRecord record) { 208 workersManager.process(record); 209 } 210 } 211 212 215 private class PollerThreadFactory implements WorkerThreadFactoryIF { 216 221 public WorkerThread create() { 222 return new PollerWorkerThread(compositeObserver, compositeApprover); 223 } 224 } 225 } 226 | Popular Tags |