是否可以为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();
}
}
它们似乎都不起作用。欣赏任何想法或建议如何以适当的方式做到这一点?感谢
答案 0 :(得分:0)
我们无法从静态块中的数据库中获取cron表达式,因为在启动gorm之前加载了Job Classes。我们遇到了类似的用例,其中必须从数据库中读取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)
}
}