spring 定时任务

11年前
quartz-1.5.1.jar


package test.quartz;

import java.util.ArrayList;
import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;

public class TimerTest {
public static void main(String... strings) {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

Scheduler sched;
try {

// sched = schedFact.getScheduler();
sched = StdSchedulerFactory.getDefaultScheduler();

JobDetail jobDetail = new JobDetail("myJob", // job name
sched.DEFAULT_GROUP, // job group (you can also specify
// 'null' to
// use the default group)
TestJob.class); // the java class to execute

jobDetail.getJobDataMap().put("jobSays", "Hello World!");
jobDetail.getJobDataMap().put("myFloatValue", 3.141f);
jobDetail.getJobDataMap().put("myStateData", new ArrayList());

//Trigger trigger = TriggerUtils.makeDailyTrigger(0, 1);
Trigger trigger = TriggerUtils.makeSecondlyTrigger(2);
trigger.setStartTime(new Date());
trigger.setName("myTrigger");

// Add the given JobDetail to the Scheduler,
// and associate the given Trigger with it.
sched.scheduleJob(jobDetail, trigger);
sched.start();
// sched.shutdown();

} catch (SchedulerException e) {
e.printStackTrace();
}
}
public void testQuartz() {
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

Scheduler sched;
try {

// sched = schedFact.getScheduler();
sched = StdSchedulerFactory.getDefaultScheduler();

JobDetail jobDetail = new JobDetail("myJob", // job name
sched.DEFAULT_GROUP, // job group (you can also specify
// 'null' to
// use the default group)
TestJob.class); // the java class to execute

jobDetail.getJobDataMap().put("jobSays", "Hello World!");
jobDetail.getJobDataMap().put("myFloatValue", 3.141f);
jobDetail.getJobDataMap().put("myStateData", new ArrayList());

// Trigger trigger = TriggerUtils.makeDailyTrigger(0, 1);
Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);
trigger.setStartTime(new Date());
trigger.setName("myTrigger");

// Add the given JobDetail to the Scheduler,
// and associate the given Trigger with it.
sched.scheduleJob(jobDetail, trigger);
sched.start();
// sched.shutdown();

} catch (SchedulerException e) {
e.printStackTrace();
}
}
}