Quartz Scheduler(2.2.1) - Usage of JobDataMap

本文详细介绍了如何使用Quartz中的JobDataMap来存储和检索任务执行所需的数据。通过示例展示了如何在定义JobDetail时将数据放入JobDataMap,并在任务执行过程中获取这些数据。此外还介绍了通过设置setter方法简化数据访问的过程。

The JobDataMap can be used to hold any amount of (serializable) data objects which you wish to have made available to the job instance when it executes. JobDataMap is an implementation of the Java Map interface, and has some added convenience methods for storing and retrieving data of primitive types.

Here's some snippets of putting data into the JobDataMap while defining/building the JobDetail, prior to adding the job to the scheduler:

JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                                .withIdentity("helloJob", Scheduler.DEFAULT_GROUP)
                                .usingJobData("msg", "hello JobDataMap.")
                                .build(); 

Here is an example of getting data from the JobDataMap during the job's execution:

public class HelloJob implements Job {
    
    public void execute(JobExecutionContext context) throws JobExecutionException {
        String msg = context.getJobDetail().getJobDataMap().getString("msg");
        System.out.println("msg: " + msg);
    }
    
}

 

If you add setter methods to your job class that correspond to the names of keys in the JobDataMap (such as a setMsg(String msg) method for the data in the example above), then Quartz's default JobFactory implementation will automatically call those setters when the job is instantiated, thus preventing the need to explicitly get the values out of the map within your execute method.

public class HelloJob implements Job {
    
    @Setter
    private String msg;
    
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("msg: " + msg);
    }
    
}

 

转载于:https://www.cnblogs.com/huey/p/5106117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值