使用自定义触发器和参数安排作业

发布于 2024-12-26 23:50:26 字数 975 浏览 2 评论 0原文

我正在使用 Grails Quartz 插件,并希望使用以编程方式创建的触发器来安排我的作业。我事先不知道执行间隔是多少。我希望这项工作无限期地执行。

文档给出了一些如何安排/触发作业的示例

==动态作业调度==

从 0.4.1 版本开始,您可以动态安排作业执行。

这些方法可用:

  • MyJob.schedule(String cronExpression, Map params?) 创建 cron 触发器;
  • MyJob.schedule(LongrepeatInterval, IntegerrepeatCount?, Map params?) 创建简单触发器:重复作业repeatCount+1次,延迟repeatInterval毫秒;
  • MyJob.schedule(Date ScheduleDate, Map params?) 将一项作业安排到特定日期执行;
  • MyJob.schedule(Trigger trigger) 使用自定义触发器安排作业的执行;
  • MyJob.triggerNow(Map params?) 强制立即执行作业。

    每个方法(自定义触发器除外)都采用可选的“params”参数。您可以使用它将一些数据传递到您的作业,然后从作业中访问它。

Grails 版本 1.3.7 Quartz 插件版本 0.4.2

那么,为什么 MyJob.schedule(Trigger trigger) 不接受参数?而且,如何使用自定义触发器和作业的映射或参数来实现我想要的目标?

I am using the Grails Quartz plugin and want to schedule my jobs with a programmatically-created Trigger. I do not know beforehand what the execution interval will be. I want the job to execute indefinitely.

The docs give some examples of how to schedule/trigger jobs:

== Dynamic Jobs Scheduling ==

Starting from 0.4.1 version you have the ability to schedule job executions dynamically.

These methods are available:

  • MyJob.schedule(String cronExpression, Map params?) creates cron trigger;
  • MyJob.schedule(Long repeatInterval, Integer repeatCount?, Map params?) creates simple trigger: repeats job repeatCount+1 times with delay of repeatInterval milliseconds;
  • MyJob.schedule(Date scheduleDate, Map params?) schedules one job execution to the specific date;
  • MyJob.schedule(Trigger trigger) schedules job's execution with a custom trigger;
  • MyJob.triggerNow(Map params?) force immediate execution of the job.

    Each method (except the one for custom trigger) takes optional 'params' argument. You can use it to pass some data to your job and then access it from the job.

Grails Version 1.3.7
Quartz Plugin version 0.4.2

So, why does the MyJob.schedule(Trigger trigger) not take params? And, how can I achieve what I want, using a custom trigger and a map or params for the Job?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼痛 2025-01-02 23:50:26

如果您查看 这些方法的定义位置 a href="https://github.com/nebolsin/grails-quartz" rel="nofollow">Quartz插件源码,可以看到所有需要的函数Map params 是创建触发器 的包装器,然后将其触发到调度程序。

MyJob.schedule(Trigger trigger) 方法只是触发您传递给它的触发器,因此您需要在之前将参数添加到 Triggers jobDataMap 属性中调用这个方法,即:

trigger.jobDataMap.putAll [foo:"It Works!"]
MyJob.schedule( trigger )

If you look where these methods are defined in the Quartz plugin sourcecode, you can see that all of the functions that take Map params are wrappers which create a Trigger and then fire it off to the scheduler.

The MyJob.schedule(Trigger trigger) method, just fires off the trigger you pass it, so it is up to you to add your params into the Triggers jobDataMap property before you call this method, ie:

trigger.jobDataMap.putAll [foo:"It Works!"]
MyJob.schedule( trigger )
谜兔 2025-01-02 23:50:26

蒂姆的回答是正确的,这里有一点更新。

Trigger trigger = TriggerBuilder
                .newTrigger()
                .startNow()
                .withIdentity("triggerName", "groupName")
                .withSchedule(
                SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(5000).repeatForever()
                ).build();
        trigger.jobDataMap.putAll([foo:"bar"])
        MyJob.schedule(trigger)

tim answer is correct, here is little bit updation.

Trigger trigger = TriggerBuilder
                .newTrigger()
                .startNow()
                .withIdentity("triggerName", "groupName")
                .withSchedule(
                SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(5000).repeatForever()
                ).build();
        trigger.jobDataMap.putAll([foo:"bar"])
        MyJob.schedule(trigger)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文