Quartz定时调度器02

时间:2022-03-11 07:59:10
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sf.format(new Date()));
System.out.println("Hello Job!");
}
package com.demo.quartz.quartz01;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.SimpleScheduleBuilder;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;public class HelloScheduler{public static void main(String[] args) throws SchedulerException{SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 创建JobDetail实例,将该实力和HelloJob绑定JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myjob", "group1").build();// 创建Trigger实例,定义该job立即执行,并且每隔两秒执行一次,直到永远Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();//创建Scheduler实例SchedulerFactory schedulerFactory=new StdSchedulerFactory();Scheduler scheduler=schedulerFactory.getScheduler();scheduler.start();//打印当前时间System.out.println(sf.format(new Date()));scheduler.scheduleJob(jobDetail, trigger);}}