Quartz2 CronTrigger表达式任务调度

package com.quartz.test3;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class UserQuartz implements Job{

	public UserQuartz() throws SchedulerException, InterruptedException{
	}
	
	@SuppressWarnings("all")
	public void run() throws SchedulerException, InterruptedException{
		
		// 1. 通过工厂得到 周期对象
		Scheduler scheduler = new StdSchedulerFactory().getScheduler();
		// 2. 创建触发器
		Trigger trigger = TriggerBuilder
						  .newTrigger()
						  .withSchedule(
								  //表达式: 每3秒执行一次
								  CronScheduleBuilder.cronSchedule("0/3 * * ? * * *")
						   )
						   .startNow()
						  .build();
		// 3. 创建 job 列表
		JobDetail detail = JobBuilder.newJob(UserQuartz.class).build(); 
		// 4. 注册 任务调度传入 detail, trigger
		scheduler.scheduleJob(detail, trigger);
		// 5.启动
		scheduler.start();
		Thread.sleep(600L*1000L);
		scheduler.shutdown(true);
	}
	public static void main(String[] args) throws SchedulerException, InterruptedException {
		 new UserQuartz().run();
	}

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("date:" + new SimpleDateFormat("HH:mm:ss").format(new Date()));
	}
	
}

 

你可能感兴趣的:(Quartz2 CronTrigger表达式任务调度)