Home Java/J2EE Quartz Scheduler with Multiple Jobs in Play Framework
formats

Quartz Scheduler with Multiple Jobs in Play Framework

Published on May 13, 2014, by in Java/J2EE.

Hi Play Framework Experts,

Quartz Scheduler is a powerful and advance scheduler framework, to help Java Open Source Developer to schedule a job to run at a specified date and time.

Follow the given below steps to implement the Quartz Scheduler with multiple job in the Play Framework:

1 Create  Project in the Play Framework (more details click here) .

2  After creating the project structure should look like:

projectStructure

 

3  Create one Global class in the app folder and extend the GlobalSettings, Global class extends the onStart method of the GlobalSettings, onStart method is called automatically when we start our play project, so in the onStart method we will call the our Scheduler logic

import com.scheduler.Scheduler;
import play.GlobalSettings;

public class Global extends GlobalSettings {
public void onStart(play.Application application) {
Scheduler.callMultipleScheduler();
}
}

4  Now You will Create a class in which you have written the logic of the job. so using scheduler each and every time the job runs, now i have created a class JobA which  implements Job,  we have implemented the execute method of Job class and all the logic of the job will be written in the execute method.

5 In the multiple Job Scheduler you need to create more then one job class where we need to  implement the logic of the every job, code of job class is given below,

package com.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class JobA implements Job {

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// you have to write the logic of the job
}

}

6 Now create one class call it as ” Scheduler”  in which you will configure the scheduler and job

package com.scheduler;

import java.util.Date;

import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

import com.job.JobA;
import com.job.JobB;

public class Scheduler {
public static void callMultipleScheduler() {
// Set the job defination
JobDetailImpl firstJob = new JobDetailImpl();
firstJob.setName(“First Job”);
firstJob.setJobClass(JobA.class);

JobDetailImpl secondJob = new JobDetailImpl();
secondJob.setName(“Second Job”);
secondJob.setJobClass(JobB.class);

// Creating schedule time with trigger
SimpleTriggerImpl firstTrigger = new SimpleTriggerImpl();
firstTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
firstTrigger.setRepeatInterval(30000);
firstTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
firstTrigger.setName(“FirstTrigger”);

SimpleTriggerImpl secondTrigger = new SimpleTriggerImpl();
secondTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
secondTrigger.setRepeatInterval(30000);
secondTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
secondTrigger.setName(“secondTrigger”);
try {
org.quartz.Scheduler schedulerMain = new StdSchedulerFactory().getScheduler();
schedulerMain.start();
schedulerMain.scheduleJob(firstJob, firstTrigger);
schedulerMain.scheduleJob(secondJob, secondTrigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}

7  So when you run the project onStart method of the Global class first runs and onStart method calls callMultipleScheduler method of the Scheduler class. 

8  So this way you have  implemented the Quartz Scheduler in the play Framework.

9 Note : for implementing this you need to use the given below JAR file

quartz-2.2.1.jar

quartz-jobs-2.2.1.jar

slf4j-api-1.6.6.jar

10 You can download the source code of Quartz Scheduler with Multiple job in play Framework from: scheduler

Share!Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterFlattr the authorDigg thisPin on PinterestEmail this to someoneShare on StumbleUponShare on RedditShare on TumblrBuffer this pagePrint this page
Tags:
Copyright © 2017 - Scalsys. All Rights Reserved