定义作业类
package com.cn.task;import java.util.Date;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class HelloJob implements Job{public HelloJob(){}public void execute(JobExecutionContext arg0) throws JobExecutionException {// TODO Auto-generated method stubSystem.out.println(new Date().getTime());}}
定义测试类
package com.cn.task;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
public class HelloJobTest {
public void run() throws Exception {
// 通过SchedulerFactory获取一个调度器实例
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
//Date runTime = evenMinuteDate(new Date());
//
//// 通过过JobDetail封装HelloJob,同时指定Job在Scheduler中所属组及名称,这里,组名为group1,而名称为job1。
//JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1")
//.build();
//
//// 创建一个SimpleTrigger实例,指定该Trigger在Scheduler中所属组及名称。
//// 接着设置调度的时间规则.当前时间运行
//Trigger trigger = newTrigger().withIdentity("trigger1", "group1")
//.startAt(runTime).build();
// 注册并进行调度
//sched.scheduleJob(job, trigger);
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(
simpleSchedule().withIntervalInSeconds(40)
.repeatForever()).build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
// 启动调度器
sched.start();
try {
// 当前线程等待65秒
Thread.sleep(65L * 1000L);
} catch (Exception e) {
}
// 调度器停止运行
sched.shutdown(true);
}
public static void main(String[] args) throws Exception {
HelloJobTest example = new HelloJobTest();
example.run();
}
}