Java Job Scheduling Using Concurrent API

Just rescuing some valuable blog entries.

Simplicity and better control are my main reasons for choosing ClockDaemon from Doug Lea's Concurrent API that will be included in the JDK 1.5 Tiger over OpenSymphony's Quartz component. I won't be talking about it much as to why is it so. Hopefully, a code sample and common sense can give clarity to the unenlightened. The sample uses only one the most useful administrative methods for scheduling, the executePeriodically().



import EDU.oswego.cs.dl.util.concurrent.ClockDaemon;

/**
* Description:
*
* A simple job scheduler.
*
* @author Jared Odulio
*
*
*/
public class Scheduler {

private ClockDaemon clockDaemon;

public Scheduler(){
clockDaemon = new ClockDaemon();
}

public void startJob(long periods){
clockDaemon.executePeriodically(periods, new Job(), true);
}

public static void main(String[] args) {

long periods = Long.parseLong(args[0]);
new Scheduler().startJob(periods);

}


class Job implements Runnable{

/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// TODO Do something here. like write to log file etc...
System.out.println("Scheduled job has been triggered");

}
}
}





Of course, the code is not in a extremely good format(the use of inner class) for it to be "copied-and-pasted" on anyone's production project. But the point of this sample is the ClockDaemon API. I still hope the "expert" or some self-righteous-and-the-only-one-he-thinks-knows-everything American Software Architect would criticize on the point of this article.

Comments

Popular Posts