是否可以将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)
}
}