All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.scheduler.Scheduler Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.scheduler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

//------------------------------------------------------------------------------
/**
 Runs specified jobs on individualized schedules.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class Scheduler { private boolean mIsActive = true; private boolean mIsPaused = false; private List mJobs = new ArrayList<>(5); private final static Logger LOGGER = Logger.getLogger(Scheduler.class.getPackage().getName()); static { LOGGER.setUseParentHandlers(false); } //--------------------------------------------------------------------------- public static Logger getLogger() { return LOGGER; } //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public Scheduler() { } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public void add(ScheduledJob inScheduledJob) { if (inScheduledJob != null) { mJobs.add(inScheduledJob); if (mIsPaused) { inScheduledJob.pause(); } inScheduledJob.start(); } } //--------------------------------------------------------------------------- public void add(Runnable inJob, Schedule inSchedule) { ScheduledJob scheduledJob = new ScheduledJob(inJob, inSchedule); mJobs.add(scheduledJob); if (mIsPaused) { scheduledJob.pause(); } scheduledJob.start(); } //--------------------------------------------------------------------------- public Collection getScheduledJobs() { return Collections.unmodifiableCollection(mJobs); } //--------------------------------------------------------------------------- public void clear() { for (ScheduledJob job : mJobs) { remove(job); } } //--------------------------------------------------------------------------- public void remove(ScheduledJob inJob) { if (mJobs.contains(inJob)) { mJobs.remove(inJob); inJob.stop(); } } //--------------------------------------------------------------------------- public void stop() { LOGGER.log(Level.INFO, "Stopping scheduler..."); mIsActive = false; for (ScheduledJob scheduledJob : mJobs) { scheduledJob.stop(); } } //--------------------------------------------------------------------------- public synchronized void pause() { mIsPaused = true; for (ScheduledJob scheduledJob : mJobs) { scheduledJob.pause(); } } //--------------------------------------------------------------------------- public void pause(String inJobName) { for (ScheduledJob scheduledJob : mJobs) { if (scheduledJob.name().equals(inJobName)) { scheduledJob.pause(); } } } //--------------------------------------------------------------------------- public void pause(ScheduledJob inJob) { if (mJobs.contains(inJob)) { inJob.pause(); } } //--------------------------------------------------------------------------- public synchronized void resume() { mIsPaused = false; for (ScheduledJob scheduledJob : mJobs) { scheduledJob.resume(); } } //--------------------------------------------------------------------------- public void resume(String inJobName) { for (ScheduledJob scheduledJob : mJobs) { if (scheduledJob.name().equals(inJobName)) { scheduledJob.resume(); } } } //--------------------------------------------------------------------------- public void resume(ScheduledJob inJob) { if (mJobs.contains(inJob)) { inJob.resume(); } } //--------------------------------------------------------------------------- public void destroy() { stop(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy