Quartz[1] - Passing of task parameters

Pass parameters using JobDataMap

If you want to return some information about this Job in the implementation of the task's execute() method, or pass some parameters, you can use JobDataMap.

JobDetail job = JobBuilder.newJob(DemoJob.class)
    .withIdentity("myJob", "group1")
    .usingJobData("strData", "Tom")
    .usingJobData("intData", 15)
    .build();

When building the JobDetail, use the various overloaded forms provided by the usingJobData(key, value) method to add some custom parameters. These parameters can be obtained in the execute(JobExecutionContext jobExecutionContext) method, as shown in the following example:

@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    JobKey key = jobExecutionContext.getJobDetail().getKey();
    JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
    String strData = dataMap.getString("strData");
    int intData = dataMap.getInt("intData");
    System.out.println("key : " + key);
    System.out.println("strData : " + strData);
    System.out.println("intData : " + intData);
}

operation result:

key : group1.myJob
strData : Tom
intData : 15



Author: JohnShen
Link: https://www.jianshu.com/p/35a1e931c414
Source : Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326256630&siteId=291194637