Quartz定时任务SimpleTrigger:在调度器中传入和获取参数 - 副本

1.RemaindJob类(获取参数)

package job;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class RemaindJob implements Job{

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		System.out.println("执行任务啦啦啦阿阿联");
		JobDataMap jdMap=jobExecutionContext.getJobDetail().getJobDataMap();
		System.out.println(jdMap.get("user"));
	}

}

2.Test类(添加参数)

package test;

import java.util.Date;

import job.RemaindJob;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// 创建任务
		JobDetail jobDetail = new JobDetail("myjob1", RemaindJob.class);
		//添加参数
		jobDetail.getJobDataMap().put("user", "Tom");
		// 创建简单触发器,REPEAT_INDEFINITELY:无限重复
		SimpleTrigger simpleTri = new SimpleTrigger("trigger1",
				SimpleTrigger.REPEAT_INDEFINITELY, 500);
		// 设置触发器开始延迟时间
		simpleTri.setStartTime(new Date(System.currentTimeMillis() + 1000));
		// 创建调度工厂
		SchedulerFactory sf = new StdSchedulerFactory();
		// 获取调度器
		Scheduler scheduler;
		try {
			scheduler = sf.getScheduler();
			// 用调度器关联绑定触发器和任务
			scheduler.scheduleJob(jobDetail, simpleTri);
			// 启动
			scheduler.start();
			Thread.sleep(20000);
			// 停止
			scheduler.shutdown();

		} catch (SchedulerException e) {

			e.printStackTrace();
		} catch (InterruptedException e) {

			e.printStackTrace();
		}

	}

}

Quartz定时任务SimpleTrigger:在调度器中传入和获取参数 - 副本_第1张图片

你可能感兴趣的:(Quartz定时任务SimpleTrigger:在调度器中传入和获取参数 - 副本)