Friday, January 17, 2014

Schedules a task run within a period (with start time and end time)

package scheduler;

import java.util.logging.Logger;

public class PeriodTaskTest {

private static Logger log = Logger.getLogger(PeriodTaskTest.class.getName());

public static void main(String[] args) {
log.info("Main started.");
new AlarmTask(9, 22, 0,9,24,0,10*1000).activateThenStop();
   log.info("Main ended.");
}

}

class AlarmTask extends PeriodTask{
private int fCount;
private Logger log = Logger.getLogger(AlarmTask.class.getName());

public AlarmTask(int startHour,int startMin,int startSec,int endHour,int endMin,int endSec,long aDelayBetween) {
super(startHour,startMin,startSec,endHour,endMin,endSec,aDelayBetween);
}

@Override
final protected void execute() {
     ++fCount;
     log.info("beep " + fCount);
}

}



package scheduler;

import java.util.Calendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public abstract class PeriodTask implements Runnable {

private Logger log = Logger.getLogger("Auspix");
private final ScheduledExecutorService fScheduler;
private final long fInitialDelay;
private final long fDelayBetweenRuns;
private final long fShutdownAfter;
private static final int NUM_THREADS = 2;
private static final boolean DONT_INTERRUPT_IF_RUNNING = false;

@Override
public void run() {
execute();
}

public PeriodTask(int startHour,int startMin,int startSec,int endHour,int endMin,int endSec,long aDelayBetween){
log.info(String.format("Will start on %s and end on %s every %s seconds.", startHour+":"+startMin+":"+startSec,endHour+":"+endMin+":"+endSec,aDelayBetween));
Calendar calStart = getCal(startHour,startMin,startSec);
Calendar calEnd = getCal(endHour,endMin,endSec);
long aInitialDelay = calStart.getTimeInMillis() - System.currentTimeMillis();
long aStopAfter = calEnd.getTimeInMillis() - System.currentTimeMillis();
log.info(String.format("start time is %tT, end time is %tT. Initial delay is %d, will stop after %d.",calStart.getTime(),calEnd.getTime(),aInitialDelay,aStopAfter));
fInitialDelay = aInitialDelay;
fDelayBetweenRuns = aDelayBetween;
fShutdownAfter = aStopAfter;
fScheduler = Executors.newScheduledThreadPool(NUM_THREADS);
}

private Calendar getCal(int startHour, int startMin, int startSec) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, startHour);
cal.set(Calendar.MINUTE, startMin);
cal.set(Calendar.SECOND,startSec);
return (Calendar)cal.clone();
}

public void activateThenStop() {
ScheduledFuture<?> actionFuture = fScheduler.scheduleAtFixedRate(this,
fInitialDelay, fDelayBetweenRuns, TimeUnit.MILLISECONDS);
Runnable stopAlarm = new StopTask(actionFuture);
fScheduler.schedule(stopAlarm, fShutdownAfter, TimeUnit.MILLISECONDS);
}

protected abstract void execute();

private final class StopTask implements Runnable {
private ScheduledFuture<?> fSchedFuture;
private Logger log = Logger.getLogger("Auspix");

public StopTask(ScheduledFuture<?> aSchedFuture) {
fSchedFuture = aSchedFuture;
}

@Override
public void run() {
log.info("Stopping .");
fSchedFuture.cancel(DONT_INTERRUPT_IF_RUNNING);
fScheduler.shutdown();
}

}

}

No comments:

Post a Comment