使用Quartz的动态作业数据 [英] Dynamic Job Data using Quartz

查看:140
本文介绍了使用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");
}






JobDataMap上的文档教程。

这篇关于使用Quartz的动态作业数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆