Quatrz简单入门

xiaoxiao2021-02-28  3

/**

任务需要实现Job接口的execute方法

*/

package com.nhb;

import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyTask implements Job { Logger log = LoggerFactory.getLogger(Job.class);   private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");   public MyTask() {   log.info(simpleDateFormat.format(new Date()) + ":");   }   public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("我执行了");   }

}

package com.nhb; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class QuatrzTest { public static void main(String[] nhb) throws Exception { //获取任务管理器 SchedulerFactory factory = new StdSchedulerFactory(); Scheduler scheduler = factory.getScheduler(); //获取jobDetail JobDetail jobDetail = JobBuilder.newJob(MyTask.class)//获取所执行任务的类 .withIdentity("job1", "group1").build();//设置任务名和组名  最后创建出对象 SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger() .withIdentity("trigger1","group1")//设置任务名和组名 .startNow() .withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(10,2))//配置重复次数和间隔时间 .build();//从当前设置开始  并创建对象 scheduler.scheduleJob(jobDetail, simpleTrigger); scheduler.start(); System.out.println("end"); } }

转载请注明原文地址: https://www.6miu.com/read-1600197.html

最新回复(0)