Quartz 之 JobExecutionContext 解读

左心房为你撑大大i 提交于 2020-01-27 15:50:38

查看源码注释可以得知:

 JobExecutionContext 是一个包含了各种上下文信息的句柄,指向执行中的JobDetail 实例 和 执行完成的Trigger实例.

 JobExecutionContext中有一个简便的方法可以获取执行上下文中的JobDataMap , 它是JobDetail 中的JobDataMap 和 Trigger 中的JobDataMap 的合并,后者的JobDataMap 会覆盖前者相同名称的值.

注意: 不要期望通过这个返回的JobDataMap 的set 方法设置 某个值 持久化到作业自身的JobDataMap ,即使 有@PersistJobDataAfterExecution .

试图更改此映射的内容通常会导致IllegalStateException 异常.

 public JobDataMap getMergedJobDataMap();

我们看下如下例子.

package com.example04;

        import org.quartz.*;
        import org.quartz.impl.StdSchedulerFactory;

/**
 * 1442554244
 * JobExecutionContext例子
 **/

public class CronTriggerExample {

    public void run () throws SchedulerException {

        //实例化调度器
        Scheduler scheduler  =StdSchedulerFactory.getDefaultScheduler();

        //实例化job
        JobDetail job1 = JobBuilder.newJob(SimpleJob.class)
                .withIdentity("job1","group1")
                //设置jobDetail值
                .usingJobData("name","tom")
                .usingJobData("email","123@qq.com")
                // 当job 没有绑定trigger时,继续保存在调度器中
                .storeDurably()
                .build();

        //若有相同名称的job ,是否替换,否则抛出异常, 使用这个方法,必须保证 isDurable=true
        scheduler.addJob(job1,false);

        //定义Contrigger
        CronTrigger cronTrigger1 = TriggerBuilder.newTrigger()
                .withIdentity("trigger1","group1")
                //设置JobDatail值
                .usingJobData("email","456@qq.com")
                .withSchedule(CronScheduleBuilder.cronSchedule(" 0/5 * * * * ? "))
                .forJob(job1)
                .build();

        scheduler.scheduleJob(cronTrigger1);

        scheduler.start();

    }

    public static void main(String[] args) throws SchedulerException {
        CronTriggerExample example = new CronTriggerExample();
        example.run();

    }
}

package com.example04;

import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @author  yulei
 */

public class SimpleJob implements Job {

    private static Logger log = LoggerFactory.getLogger(SimpleJob.class);

    public SimpleJob() {

    }

    public void execute(JobExecutionContext context) throws JobExecutionException {

        JobKey jobKey = context.getJobDetail().getKey();

        JobDataMap jobDataMap = context.getMergedJobDataMap();

        log.info("name: "+jobDataMap.getString("name")+", email: "+jobDataMap.getString("email"));
        log.info("SimpleJob says: " + jobKey + " executing at " + new Date());
    }

}

打印内容如下:

INFO] 27 一月 11:52:32.084 上午 main [org.quartz.impl.StdSchedulerFactory]
Using default implementation for ThreadExecutor

[INFO] 27 一月 11:52:32.109 上午 main [org.quartz.core.SchedulerSignalerImpl]
Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

[INFO] 27 一月 11:52:32.109 上午 main [org.quartz.core.QuartzScheduler]
Quartz Scheduler v.2.3.0 created.

[INFO] 27 一月 11:52:32.110 上午 main [org.quartz.simpl.RAMJobStore]
RAMJobStore initialized.

[INFO] 27 一月 11:52:32.111 上午 main [org.quartz.core.QuartzScheduler]
Scheduler meta-data: Quartz Scheduler (v2.3.0) 'MyScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 20 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.


[INFO] 27 一月 11:52:32.112 上午 main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler 'MyScheduler' initialized from default resource file in Quartz package: 'quartz.properties'

[INFO] 27 一月 11:52:32.112 上午 main [org.quartz.impl.StdSchedulerFactory]
Quartz scheduler version: 2.3.0

[INFO] 27 一月 11:52:32.135 上午 main [org.quartz.core.QuartzScheduler]
Scheduler MyScheduler_$_NON_CLUSTERED started.

[INFO] 27 一月 11:52:35.007 上午 MyScheduler_Worker-1 [com.example04.SimpleJob]
name: tom, email: 456@qq.com

[INFO] 27 一月 11:52:35.011 上午 MyScheduler_Worker-1 [com.example04.SimpleJob]
SimpleJob says: group1.job1 executing at Mon Jan 27 11:52:35 CST 2020

由次可以看出,触发器中的JobDataMap 会覆盖JobDetail 中相同名称的key 的value 。

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!