How to execute job from controller in grails

Posted By : Summy Saurav | 28-Dec-2014

Sometimes we need to execute the job from controller, for example, you have to execute job and there in the job you have to do something with the requested params, So here in this case grails Quartz plugins provides the way to execute job from your controller so that you can provide the requested parameter to the job.

 

Look at the code written below to do so.

Quartz job in grails:

package com.retailerdev

class NameOfYourJob {
static triggers = {
 // leave this block empty
}

def execute(context)
{
 def id = context.mergedJobDataMap.get(‘id’)
}
}

Controller Calling The Job:

package com.retailerdev

class NameOfYourController {
def quartzScheduler

def someAction()
{
	def id = params.id
	NameOfYourJob .triggerNow([id:id])
}
}

To call the job from your controller you need to inject quartzScheduler then call the triggerNow function with reference of your job class like written above in the controller.

Note: Ensure that the package structure of your job class must be same as your controller class in order to call the job from controller.

 

Thanks

summy saurav

About Author

Author Image
Summy Saurav

Summy is a kind of conscientious type of personality always be stable and practical in making decision; quick learner and problem solving attitude within a team . He is Fond of drawing,reading and computer programming

Request for Proposal

Name is required

Comment is required

Sending message..