Quartz 之 JobExecutionContext 解读

查看源码注释可以得知:

 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 。

 

 

 

 

 

Quartz 之 JobExecutionContext 解读Quartz 之 JobExecutionContext 解读 余-雷 博客专家 发布了205 篇原创文章 · 获赞 254 · 访问量 76万+ 他的留言板 关注
上一篇:Quartz.Net实现简单定时任务调度


下一篇:quartz Cron表达式