We will get a first taste of Quartz, by executing a very simple job. The following snippet of code shows how easy it is to schedule a job.
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); Scheduler sched = schedFact.getScheduler(); sched.start(); JobDetail jobDetail = new JobDetail("myJob", null, HelloJob.class); Trigger trigger = TriggerUtils.makeHourlyTrigger(); // fire every hour trigger.setStartTime(TriggerUtils.getEvenHourDate(new Date())); // start on the next even hour trigger.setName("myTrigger"); sched.scheduleJob(jobDetail, trigger);
The following code assumes a HelloJob
class exists. It is a very simple class that implements the job interface and just prints a message to the console.
package packtpub.osw; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * Hello world job. */ public class HelloJob implements Job { public void execute(JobExecutionContext ctx) throws JobExecutionException...