Grails Quartz job set up manually with start and stop

Quartz plug-in allows your Grails application to schedule jobs to be executed using a specified interval or cron expression. Usually we create the job using “grails create-job” right ?. Here I an going to create the job with the help of service. Which can programmatically start and stop the scheduler job.

First install plug-in
grails install-plugin quartz

Then create a service
grails create-service com.grails.job.JobScheduler

Modify the content like following

package com.grails.job;

import org.apache.commons.logging.LogFactory
import org.quartz.Job
import org.quartz.JobExecutionContext

* SchedulerJobService : this class implements the Quartz Job * This picks the cron expression defined through the controller *

* @author Praveen * */ class SchedulerJobService implements Job { private static final log = LogFactory.getLog(SchedulerJobService.class) boolean transactional = false /** *

execute : standard execution method for Quarts Job

* * @param JobExecutionContext * @return void */ public void execute (JobExecutionContext jobExecutionContext) { "SchedulerJobService has been triggered to run " + new Date() //You can call your service method from here }

The create a controller and make the content like following

package com.grails.controller

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
import com.grails.job.SchedulerJobService;


* QuartzController : is the controller class for the scheduled job *

* @author praveen * */ class QuartzController { static allowedMethods = [save: "POST", update: "POST", delete: "POST"] /** * startScheduler : start the Quarts Job * * @return void */ def startScheduler(){ Scheduler scheduler = new StdSchedulerFactory().getScheduler(); JobDetail job = JobBuilder.newJob(SchedulerJobService.class) .withIdentity("myJob","DB") .build(); // specify the running period of the job ("10/20 * * * * ?") CronTrigger trigger = TriggerBuilder.newTrigger() .withIdentity("crontrigger","DB") .withSchedule(CronScheduleBuilder.cronSchedule("10/20 * * * * ?")) .build(); scheduler.start(); scheduler.scheduleJob(job, trigger); println "The sceduler initiated" redirect(action: "view", params: params) } /** *

* stopScheduler : this method is for stop *

* @return */ def stopScheduler(){ Scheduler scheduler = new StdSchedulerFactory().getScheduler(); TriggerKey triggerKey = TriggerKey.triggerKey("crontrigger","DB"); scheduler.unscheduleJob(triggerKey); println "The sceduler stoped" redirect(action: "view", params: params) } }

Please see how the cron expression works

cronExpression: "s m h D M W Y"
                 | | | | | | `- Year [optional]
                 | | | | | `- Day of Week, 1-7 or SUN-SAT, ?
                 | | | | `- Month, 1-12 or JAN-DEC
                 | | | `- Day of Month, 1-31, ?

About Praveen Orathel

I am a java web application developer.
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Grails Quartz job set up manually with start and stop

  1. karthi says:

    very nice explaination .can u explain how to use ajax plugins in grails as simple example

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s