简体   繁体   English

如何使用Grails Quartz2插件在指定时间设置一次性工作触发器

[英]How to set up a one-off job trigger at a specified time using Grails Quartz2 plugin

I am using Quartz2 Plugin , and am trying to dynamically trigger a very simple job. 我正在使用Quartz2 Plugin ,并试图动态触发一个非常简单的工作。 When the user performs a certain action, the job should be triggered some certain number of minutes in the future, and only run once. 当用户执行某项操作时,该作业应在将来某个特定的分钟数内触发,并且只能运行一次。

I have tried using the simple 'schedule' method that takes a date and job data: 我尝试使用简单的“计划”方法来获取日期和工作数据:

def sendTime = new Date()
use(groovy.time.TimeCategory) {
    sendTime = sendTime + (connectionInstance.timeout).minutes
    println "I will send the job at $sendTime"
}
ReportSmssyncTimeoutJob.schedule(sendTime, [connectionId:params.id])

In this setup, I find that the job actually triggers immediately instead of waiting until 'sendTime'. 在这种设置中,我发现作业实际上立即触发,而不是等到“ sendTime”。

My second attempt, after looking at the plugin source, was to use a SimpleTrigger 在查看插件源代码之后,我的第二次尝试是使用SimpleTrigger

def sendTime = new Date()
use(groovy.time.TimeCategory) {
    sendTime = sendTime + (connectionInstance.timeout).minutes
    println "I will send the job at $sendTime"
}
// arguments here are: jobKey='test', startTime=sendTime, repeatCount=0, repeatInterval=1 (zero not allowed), job-arguments)
def trigger = TriggerHelper.simpleTrigger(new JobKey("test"), sendTime, 0, 1, [connectionId:params.id])
ReportSmssyncTimeoutJob.schedule(trigger)

In this setup, the job also triggers immediately. 在此设置中,作业也会立即触发。 Is there something wrong with the SimpleTrigger implementation which prevents it from waiting until startDate? SimpleTrigger实现有什么问题,它可以阻止它等到startDate为止?

Unfortunately, switching to the main 'quartz' plugin (which now has support for Quartz 2) is not an option as I am working on a project that has loads of jobs set up to work with the quartz2 plugin. 不幸的是,切换到主要的“ quartz”插件(现在已支持Quartz 2)是不可行的,因为我正在从事一个项目,该项目设置了许多工作以与quartz2插件一起使用。

I asked this on the Grails mailing list and got the answer: this is a bug in the quartz2 plugin. 我在Grails邮件列表中询问了此问题,并得到了答案:这是quartz2插件中的错误。 It should be fixed in the next release (bug was noted in 0.2.3). 它应该在下一个版本中修复(错误已在0.2.3中指出)。

Update: tested this in v2.1.6.2 of the quartz2 plugin, and can confirm that both of the approaches in my question now work. 更新:在quartz2插件的v2.1.6.2中对此进行了测试,并且可以确认我问题中的两种方法现在都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM