1 28 package de.nava.informa.utils.cleaner; 29 30 import de.nava.informa.utils.toolkit.WorkerThread; 31 32 import de.nava.informa.core.ChannelIF; 33 import de.nava.informa.utils.toolkit.Scheduler; 34 import de.nava.informa.utils.toolkit.SchedulerCallbackIF; 35 import de.nava.informa.utils.toolkit.WorkersManager; 36 import de.nava.informa.utils.toolkit.ChannelRecord; 37 import de.nava.informa.utils.toolkit.WorkerThreadFactoryIF; 38 39 65 public class Cleaner { 66 67 private static final long DEFAULT_CLEANING_PERIOD = 3600000; private static final int DEFAULT_WORKER_THREADS = 2; 69 70 private WorkersManager workersManager; 71 private Scheduler scheduler; 72 73 private CompositeObserver compositeObserver; 74 private CompositeMatcher compositeMatcher; 75 76 private long globalPollPeriod = DEFAULT_CLEANING_PERIOD; 77 78 83 public Cleaner() { 84 this(DEFAULT_WORKER_THREADS); 85 } 86 87 94 public Cleaner(int workerThreads) { 95 this.compositeMatcher = new CompositeMatcher(); 97 this.compositeObserver = new CompositeObserver(); 98 99 workersManager = new WorkersManager(new CleanerThreadFactory(), workerThreads); 101 102 scheduler = new Scheduler(new SchedulerCallback()); 104 } 105 106 111 public final void addObserver(CleanerObserverIF observer) { 112 compositeObserver.add(observer); 113 } 114 115 120 public final void addMatcher(CleanerMatcherIF matcher) { 121 compositeMatcher.add(matcher); 122 } 123 124 129 public final void removeObserver(CleanerObserverIF observer) { 130 compositeObserver.remove(observer); 131 } 132 133 138 public final void removeMatcher(CleanerMatcherIF matcher) { 139 compositeMatcher.remove(matcher); 140 } 141 142 147 public final void registerChannel(ChannelIF channel) { 148 scheduler.schedule(channel, globalPollPeriod, ChannelRecord.PRIO_NORMAL); 149 } 150 151 157 public final void registerChannel(ChannelIF channel, long period) { 158 scheduler.schedule(channel, period, ChannelRecord.PRIO_NORMAL); 159 } 160 161 166 public final void unregisterChannel(ChannelIF channel) { 167 scheduler.unschedule(channel); 168 } 169 170 176 public final void cleanChannel(ChannelIF channel) { 177 scheduler.triggerNow(channel); 178 } 179 180 188 public final void setWorkerThreads(int count) { 189 workersManager.setWorkerThreads(count); 190 } 191 192 197 public final void setPeriod(long period) { 198 this.globalPollPeriod = period; 199 scheduler.rescheduleAll(period); 200 } 201 202 205 private class CleanerThreadFactory implements WorkerThreadFactoryIF { 206 211 public WorkerThread create() { 212 return new CleanerWorkerThread(compositeObserver, compositeMatcher); 213 } 214 } 215 216 219 private class SchedulerCallback implements SchedulerCallbackIF { 220 225 public void process(ChannelRecord record) { 226 workersManager.process(record); 227 } 228 } 229 } 230 | Popular Tags |