Files
notes_estom/Quartz/5 JobDataMap.md
2022-11-24 11:11:33 +08:00

36 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
JobDataMap
https://blog.csdn.net/qq_30859353/article/details/120533838
JobDataMap中可以包含不限量的序列化的数据对象在job实例执行的时候可以使用其中的数据JobDataMap是Java Map接口的一个实现额外增加了一些便于存取基本类型的数据的方法。
将job加入到scheduler之前在构建JobDetail时可以将数据放入JobDataMap如下示例
```
// define the job and tie it to our DumbJob class
JobDetail job = newJob(DumbJob.class)
.withIdentity("myJob", "group1") // name "myJob", group "group1"
.usingJobData("jobSays", "Hello World!")
.usingJobData("myFloatValue", 3.141f)
.build();
在job的执行过程中可以从JobDataMap中取出数据如下示例
public class DumbJob implements Job {
public DumbJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
JobKey key = context.getJobDetail().getKey();
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String jobSays = dataMap.getString("jobSays");
float myFloatValue = dataMap.getFloat("myFloatValue");
System.err.println("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue);
}
}
```