001package com.hfg.util.scheduler; 002 003import java.util.ArrayList; 004import java.util.Collection; 005import java.util.Collections; 006import java.util.List; 007import java.util.logging.Level; 008import java.util.logging.Logger; 009 010//------------------------------------------------------------------------------ 011/** 012 Runs specified jobs on individualized schedules. 013 <div> 014 @author J. Alex Taylor, hairyfatguy.com 015 </div> 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class Scheduler 039{ 040 041 private boolean mIsActive = true; 042 private boolean mIsPaused = false; 043 044 private List<ScheduledJob> mJobs = new ArrayList<>(5); 045 046 private final static Logger LOGGER = Logger.getLogger(Scheduler.class.getPackage().getName()); 047 048 static 049 { 050 LOGGER.setUseParentHandlers(false); 051 } 052 053 //--------------------------------------------------------------------------- 054 public static Logger getLogger() 055 { 056 return LOGGER; 057 } 058 059 //########################################################################### 060 // CONSTRUCTORS 061 //########################################################################### 062 063 //--------------------------------------------------------------------------- 064 public Scheduler() 065 { 066 } 067 068 //########################################################################### 069 // PUBLIC METHODS 070 //########################################################################### 071 072 //--------------------------------------------------------------------------- 073 public void add(ScheduledJob inScheduledJob) 074 { 075 if (inScheduledJob != null) 076 { 077 mJobs.add(inScheduledJob); 078 079 if (mIsPaused) 080 { 081 inScheduledJob.pause(); 082 } 083 084 inScheduledJob.start(); 085 } 086 } 087 088 //--------------------------------------------------------------------------- 089 public void add(Runnable inJob, Schedule inSchedule) 090 { 091 ScheduledJob scheduledJob = new ScheduledJob(inJob, inSchedule); 092 mJobs.add(scheduledJob); 093 094 if (mIsPaused) 095 { 096 scheduledJob.pause(); 097 } 098 099 scheduledJob.start(); 100 } 101 102 //--------------------------------------------------------------------------- 103 public Collection<ScheduledJob> getScheduledJobs() 104 { 105 return Collections.unmodifiableCollection(mJobs); 106 } 107 108 //--------------------------------------------------------------------------- 109 public void clear() 110 { 111 for (ScheduledJob job : mJobs) 112 { 113 remove(job); 114 } 115 } 116 117 //--------------------------------------------------------------------------- 118 public boolean remove(ScheduledJob inJob) 119 { 120 boolean result = false; 121 if (mJobs.contains(inJob)) 122 { 123 result = mJobs.remove(inJob); 124 inJob.stop(); 125 } 126 127 return result; 128 } 129 130 //--------------------------------------------------------------------------- 131 public boolean remove(String inJobName) 132 { 133 boolean result = false; 134 for (ScheduledJob scheduledJob : mJobs) 135 { 136 if (scheduledJob.name().equals(inJobName)) 137 { 138 result = remove(scheduledJob); 139 break; 140 } 141 } 142 143 return result; 144 } 145 146 //--------------------------------------------------------------------------- 147 public void stop() 148 { 149 LOGGER.log(Level.INFO, "Stopping scheduler..."); 150 mIsActive = false; 151 for (ScheduledJob scheduledJob : mJobs) 152 { 153 scheduledJob.stop(); 154 } 155 } 156 157 //--------------------------------------------------------------------------- 158 public synchronized void pause() 159 { 160 mIsPaused = true; 161 for (ScheduledJob scheduledJob : mJobs) 162 { 163 scheduledJob.pause(); 164 } 165 } 166 167 //--------------------------------------------------------------------------- 168 public void pause(String inJobName) 169 { 170 for (ScheduledJob scheduledJob : mJobs) 171 { 172 if (scheduledJob.name().equals(inJobName)) 173 { 174 scheduledJob.pause(); 175 } 176 } 177 } 178 179 //--------------------------------------------------------------------------- 180 public void pause(ScheduledJob inJob) 181 { 182 if (mJobs.contains(inJob)) 183 { 184 inJob.pause(); 185 } 186 } 187 188 //--------------------------------------------------------------------------- 189 public synchronized void resume() 190 { 191 mIsPaused = false; 192 for (ScheduledJob scheduledJob : mJobs) 193 { 194 scheduledJob.resume(); 195 } 196 } 197 198 //--------------------------------------------------------------------------- 199 public void resume(String inJobName) 200 { 201 for (ScheduledJob scheduledJob : mJobs) 202 { 203 if (scheduledJob.name().equals(inJobName)) 204 { 205 scheduledJob.resume(); 206 } 207 } 208 } 209 210 //--------------------------------------------------------------------------- 211 public void resume(ScheduledJob inJob) 212 { 213 if (mJobs.contains(inJob)) 214 { 215 inJob.resume(); 216 } 217 } 218 219 //--------------------------------------------------------------------------- 220 public void destroy() 221 { 222 stop(); 223 } 224}