package com.yh.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author lq
* 创建时间 2019/4/17 0:00
**/
public class HelloJob implements Job {
/**
* 验证每次调度器运行时,它在调用execute方法前会创建一个新的job实例,当调用完成后,关联的job对象实例会被释放,释放的实例会被垃圾回收机制回收。
* ps,使用了反射
*/
public HelloJob(){
System.out.println("HelloJob init "+new Date());
}
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
System.out.println("正在进行数据备份");
}
}
package com.yh.scheduler;
import com.yh.job.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
/**
* @author lq
* 创建时间 2019/4/17 0:03
**/
public class HelloSchedulerDemo {
public static void main(String[] args) throws SchedulerException {
//JOb
JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("hello1", "group1").build();
//Trigger
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();
//Scheduler
Scheduler defaultScheduler = StdSchedulerFactory.getDefaultScheduler();
//调度关联任务和触发器
defaultScheduler.scheduleJob(detail,trigger);
defaultScheduler.start();
}
}