1
votes

Is it possible to assign a property for cronExpression value as below class

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();
    }

}

In fact I would like to have the cronExpression value to be configured and called from a table as my example below

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();
    }

}

Both of them doesn't work seem. Appreciate any idea or suggestion how this can be done in proper way? Thanks

1

1 Answers

0
votes

We can't fetch the cron expression from database in static block as the Job Classes are loaded before the gorm is initiated. We ran into similar usecase where the cronExpression had to be read from database. Here is how we solved it.

Don't define the triggers at Job Level, Means that the job will not be scheduled by default.

RecursiveNotificationJob {
    def reminderService;
    def grailsApplication

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

Schedule the Job Manually in 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)
    }
}