如何在Grails中配置Quartz触发器以允许不同的测试和生产时间表 [英] How to configure Quartz triggers in Grails to allow different schedules for testing and production

查看:139
本文介绍了如何在Grails中配置Quartz触发器以允许不同的测试和生产时间表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Grails 2.2.4中采用Quartz插件(:quartz:1.0.1),并试图弄清楚如何允许开发和测试使用与生产所需的时间表不同的时间表,而不必更改部署到每个代码.

I am trying to adopt the Quartz plugin (:quartz:1.0.1) in Grails 2.2.4 and am trying to figure out how to allow development and testing to use a different schedule than what is desired in production without having to change the code deployed to each.

这是我的经验.

我也在使用JDBC JobStore和Quartz Monitor插件(:quartz-monitor:1.0).所以我有这样定义的工作:

I am using a JDBC JobStore and the Quartz Monitor plugin (:quartz-monitor:1.0) too. So I have a job defined like this:

class TestJob {
    static triggers = {
        cron name: 'testTrigger', startDelay: 1000, cronExpression: '0 0/1 * * * ?'
    }
    ...
}

当我运行Grails应用程序时,将为该作业设置触发器,并将其存储在数据库中并开始执行.如果我通过Quartz Monitor故意对cron表达式进行更改,它将反映在数据库和执行计划中.

When I run the Grails application the trigger is set up for the job, stored in the database, and starts executing. If I go in and make an intentional change to the cron expression through the Quartz Monitor it is reflected in the database and the execution schedule.

如果我现在重新启动应用程序,则将触发器更改回作业中定义的状态.因此,使用这种方法时,我几乎无法完成工作的触发器块中的任何内容.如果我从代码中完全删除了触发器块,那么数据库定义将保持不变,并控制计划,这似乎是一个改进.

If I now restart the application the trigger is changed back to what is defined in the job. So using this approach I am pretty much stuck with whatever is in the triggers block for the job. If I remove the triggers block from the code altogether then the database definitions go unaltered and control the schedule which seems like an improvement.

因此,我认为在Job中不定义任何触发器是有道理的,但这使我试图弄清楚如何首先以不覆盖有意更改的方式加载触发器.似乎在配置中添加一个块(可能在外部配置文件中)以定义触发器是很有意义的.据我所知,我将需要编写一些内容以在启动时(在BootStrap中?)进行解析,然后通过Quartz API应用它.

So I think it makes sense to not define any triggers in the Job, but that leaves me trying to figure out how to load the triggers up in the first place and in a way that does not overwrite intentional changes. It seems like adding a block to the config (probably in an external config file) for defining triggers would make sense. As far as I can tell I would need to write something to parse this at start-up (in BootStrap?) and apply it through the Quartz API.

在我所有的阅读和谷歌搜索文档中都缺少这样的东西吗?或者,也许我的想法更根本了.

Does something like this already exist that am missing in all of my documentation reading and googling? Or maybe I am wrongheaded in a more fundamental way.

更新一些实施细节

Hans给了我一些想法,以了解适合我的情况的情况.

Hans gave me some ideas about what should work for my situation.

我最终关闭了JDBC作业存储,因为我认为配置应该是触发权限.我将作业/触发信息放入一个看起来像这样的外部配置文件中.

I ended up turning off the JDBC job store because I decided the config should be the authority for triggering. I put the job/trigger information into an external config file that looks like this.

quartzJobs: [
    'TestJob': [
        cronTriggers: [
            cronExpression: '0 0 7 ? * 2-6'
        ]
    ]
]

然后我在BootStrap中调用了一些看起来像这样的代码.

Then I have some code called in BootStrap that looks like this.

def jobs = grailsApplication.config.quartzJobs
if (jobs) {
    jobs.each { job, details ->
        List triggers = (details?.cronTriggers instanceof Map) ? [details.cronTriggers]: details.cronTriggers
        if (triggers) {
            def j = grailsApplication.mainContext.getBean(job)
            triggers.each { trigger ->
                String cronExpression = trigger.cronExpression ?: '1 1 1 1 1 ? 2099'
                j.schedule(cronExpression)
            }
        }
    }
}

推荐答案

您可以将配置放入Config.groovy或从grails.config.locations中读取的属性文件中.

You can put the configuration in your Config.groovy or in a properties file that gets read from grails.config.locations.

然后在您的BootStrap.groovy中,您可以执行以下操作:

And then in you BootStrap.groovy you can do:

TestJob.schedule(grailsApplication.config.cronExpression)

在此cronExpression中是您在属性文件中选择的属性的名称.

In this cronExpression is the name of the property you chose in the properties file.

请参见 http://grails-plugins.github.io/grails -quartz/guide/triggers.html 表示可用的不同Job.schedule()方法.

See http://grails-plugins.github.io/grails-quartz/guide/triggers.html for the different Job.schedule() methods that are available.

这篇关于如何在Grails中配置Quartz触发器以允许不同的测试和生产时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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