Java job scheduler- Quartz Scheduler

Quartz Scheduler in java
Quartz Scheduler

We know about cron job in unix. If we want to schedule any job in a particular day and time ,use corn scheduler in unix.

Being a java developer we always think if we can do the same  in java. If we want to schedule a job in java we can use “Quartz Scheduler”. java job scheduler is easy to configure.

To trigger a job we should use “cron expressions”, it tells when a job should be trigger. Below are the few examples of “cron expressions”

 

**Expression** **Meaning**
0 0 12 * * ? Fire at 12pm (noon) every day
0 15 10 ? * * Fire at 10:15am every day
0 15 10 * * ? Fire at 10:15am every day
0 15 10 * * ? * Fire at 10:15am every day
0 15 10 * * ? 2005 Fire at 10:15am every day during the year 2005
0 * 14 * * ? Fire every minute starting at 2pm and ending at 2:59pm, every day
0 0/5 14 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
0 0/5 14,18 * * ? Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
0 0-5 14 * * ? Fire every minute starting at 2pm and ending at 2:05pm, every day
0 10,44 14 ? 3 WED Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10 ? * MON-FRI Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 * ? Fire at 10:15am on the 15th day of every month
0 15 10 L * ? Fire at 10:15am on the last day of every month
0 15 10 L-2 * ? Fire at 10:15am on the 2nd-to-last last day of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L Fire at 10:15am on the last Friday of every month
0 15 10 ? * 6L 2002-2005 Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10 ? * 6#3 Fire at 10:15am on the third Friday of every month
0 0 12 1/5 * ? Fire at 12pm (noon) every 5 days every month, starting on the first day of the month.
0 11 11 11 11 ? Fire every November 11th at 11:11am.

For more detail please check http://quartz-scheduler.org/documentation/quartz-2.1.x/tutorials/crontrigger

 

Project structure

java job Scheduler
Quartz Scheduler in java

 

Please follow the below steps to schedule a job in java based project.

1. CronScheduler.java

package com.codewale.example; import java.util.Map; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class CronScheduler extends HttpServlet{ public void scheduleJob1() { String jobname = "This is job1"; String triggername = "Trigger 1"; String scheduletime = "0 0/2 * * * ?"; if(jobname!=null) { System.out.println("JOBNAME1="+jobname+" TRIGGERNAME1="+triggername+" JOBCRONEXPR1="+scheduletime); JobDetail job; job = JobBuilder.newJob(Job1.class).withIdentity(jobname).build(); Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggername).withSchedule(CronScheduleBuilder.cronSchedule(scheduletime)).build(); Scheduler scheduler=null; try { scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); } catch(SchedulerException e){ try { scheduler.shutdown(true); } catch (SchedulerException f) { f.printStackTrace(); } } } } public void scheduleJob2() { String jobname = "This is job2"; String triggername = "Trigger 2"; String scheduletime = "0 0/2 * * * ?"; if(jobname!=null) { System.out.println("JOBNAME2="+jobname+" TRIGGERNAME2="+triggername+" JOBCRONEXPR2="+scheduletime); JobDetail job; job = JobBuilder.newJob(Job2.class).withIdentity(jobname).build(); Trigger trigger = TriggerBuilder.newTrigger().withIdentity(triggername).withSchedule(CronScheduleBuilder.cronSchedule(scheduletime)).build(); Scheduler scheduler=null; try { scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); } catch(SchedulerException e){ try { scheduler.shutdown(true); } catch (SchedulerException f) { f.printStackTrace(); } } } } public void init(ServletConfig config) throws ServletException { super.init(config); System.out.println(" Initializing scheduled tasks from Job1............."); scheduleJob1(); System.out.println(" Initializing scheduled tasks from Job2............."); scheduleJob2(); } }

2.Job1.java

package com.codewale.example;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class Job1 implements Job{
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println("Job1 started ..........................");
}
}

3.Job2.java

package com.codewale.example;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class Job2 implements Job{
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// you can execute you job here
System.out.println("Job2 started .........................."); } }

4.Index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1">




Codewale Cron Scheduler Example


Please check the log /console, if jobs started.


5.Web.xml



 CronSchedulerInjava
 
 
  index.html
  index.htm
  index.jsp
  default.html
  default.htm
  default.jsp
 
 
 
 
  CronScheduler
  com.codewale.example.CronScheduler
  5
 

When server will start, web.xml will be loaded, it will load the CronScheduler class. CronScheduler will decide which job it will execute.

Don’t forget to  configure the web.xml 
Download Code

Profile photo of Nirmal Dhara
About Nirmal Dhara 28 Articles
Java Developer

5 Comments

  1. Hi,

    Can i use this to trigger a push notification for the following condition??This is my need:

    when a user save an account detail he will save a start date and an end date to the google cloud. i want to send notification to this user every 3rd month from this start date till the end date. like this there will be many accounts it can be from same user or different. I want to know whether this is possible??? if so how???

    • Yes you can use that. In fact i am using the same scheduler to send the google GCM. every day night my quartz scheduler send the GCM message to the registered user. if you need any help for GCM let me know i can do that.

  2. Hi,

    Iam new to quartz scheduler. I have to run batch file with jsp servlet and maintain logs everyday. I dont know how to log execution history and save the execution history into database. can you plz help me.

    • if i understand your problem correctly, you are running some application and that is generating log every day. and you want to put that log to data base . for that you want to run the scheduling job, which will inset the log to DB.

      solution – write a Quartz schedular and execute a job – in the job class write the code to read the data from log file and insert into db.

      • Thanks for your reply. But my exact question is with the quartz scheduler how can i get the execution history of jobs and i have it generate execution history logs. For example, how to get the scheduled time, nextfiretime, execution status whether the job is success or failure, how many time the job is fired etc., Is there any default functions? please suggest any idea.

        Thanks,
        suryaprakash

Leave a Reply

Your email address will not be published.


*