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

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

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

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 Runs specified job on a specified schedule.
 
@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 ScheduledJob implements Runnable { private String mName; private Runnable mJob; private Schedule mSchedule; private Thread mThread; private boolean mIsActive = true; // Should this job still be managed by the Scheduler private boolean mIsPaused = false; private boolean mIsCurrentlyExecuting = false; private boolean mExecuteNow = false; private Date mLastExecutionStartDate; private final static Logger LOGGER = Logger.getLogger(Scheduler.class.getPackage().getName()); //########################################################################### // CONSTRUCTORS //########################################################################### //------------------------------------------------------------------------ public ScheduledJob(Runnable inJob, Schedule inSchedule) { mJob = inJob; mSchedule = inSchedule; } //########################################################################### // PUBLIC METHODS //########################################################################### //------------------------------------------------------------------------ public ScheduledJob setName(String inValue) { mName = inValue; return this; } //------------------------------------------------------------------------ public String name() { return (StringUtil.isSet(mName) ? mName : mJob.getClass().getSimpleName()); } //------------------------------------------------------------------------ @Override public String toString() { return name(); } //------------------------------------------------------------------------ public Runnable getJob() { return mJob; } //------------------------------------------------------------------------ public Schedule getSchedule() { return mSchedule; } //------------------------------------------------------------------------ public Thread getThread() { return mThread; } //------------------------------------------------------------------------ public Date getLastExecutionStartDate() { return mLastExecutionStartDate; } //------------------------------------------------------------------------ public void start() { if (null == mThread) { mThread = new Thread(this); mThread.setName(name()); mThread.start(); } mIsActive = true; } //------------------------------------------------------------------------ public void stop() { mIsActive = false; mThread.interrupt(); } //------------------------------------------------------------------------ public void pause() { mIsPaused = true; } //------------------------------------------------------------------------ public void resume() { mIsPaused = false; } //------------------------------------------------------------------------ public void executeNow() { if (! mIsCurrentlyExecuting) { mExecuteNow = true; mThread.interrupt(); } } //------------------------------------------------------------------------ public void run() { while (mIsActive) { try { Date nextScheduledExecutionTime = mSchedule.next(); if (nextScheduledExecutionTime != null) { if (! mExecuteNow) { LOGGER.log(Level.FINE, "Next scheduled execution time: " + nextScheduledExecutionTime); // Just sleep until it is time to execute this job again Thread.sleep(nextScheduledExecutionTime.getTime() - System.currentTimeMillis()); } if (mIsPaused && ! mExecuteNow) { LOGGER.log(Level.FINE, "Skipping this execution instance since this job is currently paused."); } else { mExecuteNow = false; mIsCurrentlyExecuting = true; mLastExecutionStartDate = new Date(); mJob.run(); } } } catch (InterruptedException e) { // Ignore } catch (Throwable e2) { e2.printStackTrace(); //TODO: Have the scheduler log the exception } } LOGGER.log(Level.INFO, "Scheduled job " + mJob.getClass().getSimpleName() + " is exiting and will no longer be run."); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy