package test;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
public class Test
{
public static void main(String args[]) throws SchedulerException
{
//Step 1) Create SchedulerFactory
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
//Step 2) Create Scheduler
Scheduler sched = schedFact.getScheduler();
//Step 3) Start Scheduler
sched.start();
//Step 4) Create JobDetail. Maybe set JobDataMap here.
JobDetail jobDetail = new JobDetail("myJob", null, DumbJob.class);
//Step 5) Create Trigger
Trigger trigger = getOneTimeTrigger();
//Step 6) Schedule Job
sched.scheduleJob(jobDetail, trigger);
}
/**
* This trigger fire every hour, start on the next even hour
* @return
*/
private static Trigger getHourlyTrigger()
{
Trigger trigger = TriggerUtils.makeHourlyTrigger(); // fire every hour
trigger.setStartTime(TriggerUtils.getEvenHourDate(new Date())); // start on the next even hour
trigger.setName("myTrigger");
return trigger;
}
/**
* This trigger fire every minute, start now.
* @return
*/
private static Trigger getMinutelyTrigger()
{
Trigger trigger = TriggerUtils.makeMinutelyTrigger(); // fire every minute
trigger.setStartTime(new Date()); // start now
trigger.setName("myTrigger");
return trigger;
}
/**
* This trigger fire 4 times, and start immediately, 5 seconds between the fire.
* @return
*/
private static Trigger getFourTimesTrigger()
{
Trigger trigger = TriggerUtils.makeImmediateTrigger(3, 5000);
trigger.setStartTime(new Date()); // start now
trigger.setName("myTrigger");
return trigger;
}
/**
* This trigger fire 1 time, and start 10 seconds later
* @return
*/
private static Trigger getOneTimeTrigger()
{
long startTime = System.currentTimeMillis() + 10000L;
//public SimpleTrigger(String name, String group, Date startTime, Date endTime, int repeatCount, long repeatInterval)
SimpleTrigger trigger = new SimpleTrigger("myTrigger", null, new Date(startTime), null, 0, 0L);
return trigger;
}
/**
* Create a trigger that fires immediately, then repeats every 60 seconds, forever
* @return
*/
private static Trigger getForeverTrigger()
{
SimpleTrigger trigger = new SimpleTrigger("myTrigger", null, new Date(), null, SimpleTrigger.REPEAT_INDEFINITELY, 60L * 1000L);
return trigger;
}
/**
* Create a trigger that fires on March 17 of the year 2002 at precisely 10:30 am, and repeats 5 times (for a total of 6 firings) - with a 30 second delay between each firing
* @return
*/
private static Trigger getSharpTimeTrigger()
{
java.util.Calendar cal = new java.util.GregorianCalendar(2002, cal.MARCH, 17);
cal.set(cal.HOUR, 10);
cal.set(cal.MINUTE, 30);
cal.set(cal.SECOND, 0);
cal.set(cal.MILLISECOND, 0);
Date startTime = cal.getTime();
SimpleTrigger trigger = new SimpleTrigger("myTrigger", null, startTime, null, 5, 30L * 1000L);
return trigger;
}
}