在grails作业groovy文件中执行sql以配置cron触发器的值

在grails作业groovy文件中执行sql以配置cron触发器的值,grails,groovy,cron,quartz-scheduler,Grails,Groovy,Cron,Quartz Scheduler,是否可以为cronExpression值分配一个属性,如下所示 RecursiveNotificationJob { def reminderService; def grailsApplication String cronValue = "0 0 8 * * ?" static triggers = { cron name: 'recursiveNotificationTrigger', cronExpression: cronValue

是否可以为cronExpression值分配一个属性,如下所示

RecursiveNotificationJob {
    def reminderService;
    def grailsApplication
    String cronValue = "0 0 8 * * ?"

    static triggers = {
        cron name: 'recursiveNotificationTrigger', cronExpression: cronValue
    }

    def execute() {
        println new Date().toString() +" Recursive Notification Job is working";
        reminderService.execute();
    }

}
事实上,我希望配置cronExpression值并从表中调用它,如下例所示

class RecursiveNotificationJob {
    def reminderService;
    def grailsApplication
    String cronValue = Parameter.executeQuery("select value from Parameter where name='RecursiveNotification'");

    static triggers = {
        cron name: 'recursiveNotificationTrigger', cronExpression: cronValue
    }

    def execute() {
        println new Date().toString() +" Recursive Notification Job is working";
        reminderService.execute();
    }

}

这两个似乎都不起作用。感谢您对如何以适当方式实现这一点的任何想法或建议?谢谢

我们无法从静态块中的数据库中获取cron表达式,因为在启动gorm之前已经加载了作业类。我们遇到了类似的用例,其中必须从数据库中读取cronExpression。下面是我们如何解决它的

不在作业级别定义触发器,意味着默认情况下不会安排作业

RecursiveNotificationJob {
    def reminderService;
    def grailsApplication

    def execute() {
        println new Date().toString() +" Recursive Notification Job is working";
        reminderService.execute();
    }
}
在Bootstrap.groovy中手动安排作业

import org.quartz.CronScheduleBuilder
import org.quartz.Trigger
import org.quartz.TriggerBuilder

class BootStrap {

    def grailsApplication

    def init = { servletContext ->
        this.scheduleJob();        
    }

    def destroy = {
    }

    def scheduleJob() {
        def triggerName = "RecursiveNotificationTrigger", 
            triggerGroup = "RecursiveNotification",
            cronExpression = Parameter.executeQuery("select value from Parameter where name='RecursiveNotification'");


        Trigger trigger = TriggerBuilder
            .newTrigger()
            .withIdentity(triggerName, triggerGroup)
            .withSchedule(
                CronScheduleBuilder.cronSchedule(cronExpression))
            .build();


        RecursiveNotificationJob.schedule(trigger)
    }
}

我们无法在静态块中从数据库获取cron表达式,因为在启动gorm之前已加载作业类。我们遇到了类似的用例,其中必须从数据库中读取cronExpression。下面是我们如何解决它的

不在作业级别定义触发器,意味着默认情况下不会安排作业

RecursiveNotificationJob {
    def reminderService;
    def grailsApplication

    def execute() {
        println new Date().toString() +" Recursive Notification Job is working";
        reminderService.execute();
    }
}
在Bootstrap.groovy中手动安排作业

import org.quartz.CronScheduleBuilder
import org.quartz.Trigger
import org.quartz.TriggerBuilder

class BootStrap {

    def grailsApplication

    def init = { servletContext ->
        this.scheduleJob();        
    }

    def destroy = {
    }

    def scheduleJob() {
        def triggerName = "RecursiveNotificationTrigger", 
            triggerGroup = "RecursiveNotification",
            cronExpression = Parameter.executeQuery("select value from Parameter where name='RecursiveNotification'");


        Trigger trigger = TriggerBuilder
            .newTrigger()
            .withIdentity(triggerName, triggerGroup)
            .withSchedule(
                CronScheduleBuilder.cronSchedule(cronExpression))
            .build();


        RecursiveNotificationJob.schedule(trigger)
    }
}

我发现groovy.lang.MissingPropertyException有错误:没有这样的属性:类:BootStrap的TriggerBuilder。我可以知道这个触发器是从哪个库调用的吗?你需要导入它们!用导入更新了代码..以防万一。我假设您使用的是quartz版本>2.0,我有一个错误“执行引导时出错:没有这样的属性:RecursiveNotificationJob for class:BootStrap”。。我尝试将该作业类导入此处,但也出现了错误。您的作业文件在哪里?你能提供完整的路径吗?是的。路径是grails app\jobs\org\fsl\RecursiveNotificationJob.groovy,错误为groovy.lang.MissingPropertyException:没有这样的属性:类:BootStrap的TriggerBuilder。我可以知道这个触发器是从哪个库调用的吗?你需要导入它们!用导入更新了代码..以防万一。我假设您使用的是quartz版本>2.0,我有一个错误“执行引导时出错:没有这样的属性:RecursiveNotificationJob for class:BootStrap”。。我尝试将该作业类导入此处,但也出现了错误。您的作业文件在哪里?你能提供完整的路径吗?是的。路径是grails app\jobs\org\fsl\RecursiveNotificationJob.groovy