Search code examples
grailsgroovyquartz-scheduler

Execute a Quartz Job with a Trigger from a Controller


I can run a cron from a static trigger from within the job folder and it will execute, but when I try to fire a trigger from my controller it just plain fails...What am I missing?

ERROR CODE: No signature of method: static com.example.TaskReminderJob.triggerNow() is applicable for argument types: (java.util.LinkedHashMap) values: [[params:[name:Frank, email:[email protected]]]]

Quartz Job in grails-app/jobs/example

  package com.example
  class TaskReminderJob {
     def reminderMailService 
     static triggers = { }

     def execute(context) {
         def email = context.mergedJobDataMap.get('email')
         def name = context.mergedJobDataMap.get('name')
         reminderMailService.remindMail1(name, email)  //send email via service       
     }
  }

CONTROLLER CALLING THE JOB

package example

class UserController {
    def quartzScheduler 
    ...
    //user is created
    ...                     
    TaskReminderJob.triggerNow([name:"frank",email:"[email protected]"] )
} 

Solution

  • Correct your package path and then you can trigger your job manually using triggerNow method. And if you need to pass any parameter to it you can pass it like this:

    Controller

    package com.example
    
    class UserController {
         def someAction(){
            ...
            TaskReminderJob.triggerNow([id:params.id])
         }
    }
    

    Job

    package com.example
    
    class TaskReminderJob {
        static triggers = {}
    
        def execute(context) {
            def id = context.mergedJobDataMap.get('id')
            ...
        }
    }