import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzExample {
public static void main(String[] args) throws SchedulerException {
// 创建调度器
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
// 定义任务
JobDetail job = JobBuilder.newJob(SampleJob.class)
.withIdentity("job1", "group1")
.build();
// 触发器(每 5 秒执行一次)
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(5)
.repeatForever())
.build();
// 绑定任务和触发器
scheduler.scheduleJob(job, trigger);
}
}
public class SampleJob implements Job {
@Override
public void execute(JobExecutionContext context) {
System.out.println("Job Executed: " + new Date());
}
}