使用Quartz的动态作业数据
当我的进程收到消息时,它需要启动一个计时器并在X秒内执行一些逻辑。这些工作需要存储在JDBC存储中,据我所知,这可能与此问题无关。
When my process gets a message, it needs to start a timer and execute some logic in X seconds. These jobs need to be stored in a JDBC store, which as far as I can tell may be irrelevant to this question.
根据我所读到的内容,我应该能够为类似属性的JobDataMap分配不同的JobDataMap到单个Job类,但是我找不到任何文档或示例来支持这个用例。也许我的Google-fu很弱。
Based on what I've read, I should be able to assign a JobDataMap with different values for similar properties to a single Job class, but I'm unable to find any documentation or examples to back this use-case. Perhaps my Google-fu is weak.
这有意义吗?有一个Job类,并以某种方式存储JobDataMap来填充该Job类并在每个消息的基础上运行它?
Does that make sense? Have one Job class and somehow store a JobDataMap to populate that Job class and run it on a per-message basis?
org.quartz.Trigger
同时具有 getJobDataMap()
(这将 new
必要时加一个)和 setJobDataMap()
来获取触发器 JobDataMap 。
org.quartz.Trigger
has both getJobDataMap()
(which will new
up one if necessary) and setJobDataMap()
to get the trigger's JobDataMap.
最简单的用法是:
Trigger t = new SimpleTrigger(...);
t.getJobDataMap().put("foo", "bar");
使用现有的值映射进行初始化:
To init with an existing Map of values:
Map data = new HashMap();
data.put("foo", "bar");
t.setJobDataMap(new JobDataMap(data));
在工作执行时获取数据
public void execute(JobExecutionContext context) throws JobExecutionException
{
String fooValue = context.getMergedJobDataMap().get("foo");
}