QUARTZ FRAMEWORK IN JAVA

Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application – from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may executed virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering. Quartz is free, licensed under the Apache 2.0 license.
Quartz.NET is a pure .NET library written in C# which currently targets Framework version 1.1 and above. Quartz.NET is feature-wise equal to Quartz Java 1.6 excluding Java specifics. Quartz.NET is already running in production systems and has received good feedback.
Features
Quartz comprises from small java library (.jar file) that contains the core Quartz functionality and scheduler interface is the main interface (API) to this functionality. It needs little configuration to be set in the system. Simple scheduling process like scheduling jobs, un scheduling jobs, starting/stopping/pausing the scheduler can be done through it.
Example. –
For running any job we need to make few component.
Scheduler Task– It is simply a java class which we want to schedule.
Trigger– are used to define when Quartz will run your declared scheduled jobs.
It is of two type.https://www.techartifact.com/blogs/wp-admin/post-new.php#post_name
• SimpleTrigger – allows to set start time, end time, repeat interval to run yout job.
• CronTrigger – allows Unix cron expression to specify the dates and times to run your job.

We need to create a task which we want to schedule.

package com.vinay.jobs;
public class PrintMyNameTask
{
public void printMe() {
System.out.println(“My name is vinay” + new date());
}
}

After creating the task ,we need to make job so that we can schedule.for that we need to implement the Quartz Job interface, and also the execute() method. Get the scheduler task from Job Details via “task name”, and specify which schedule task (method) to run.
execute(): Any software components you want to schedule then you must implement the Job interface and override it execute() method.
JobExecutionContext: The JobExecutionContext object that is passed to execute() method provides the job instance with information about its “run-time” environment – a handle to the Scheduler that executed it, a handle to the Trigger that triggered the execution, the job’s JobDetail object, and a few other items.

package com.vinay.jobs;
import java.util.Map;
import org.quartz.Job;
public class NamePrintJob implements Job
{
public void execute(JobExecutionContext context)
{
Map dataMap = context.getJobDetail().getJobDataMap();
PrintMyNameTask task = (PrintMyNameJob)dataMap.get("printMyNametask");
task. printMe();
}
}

Initialize a JobDetail object, link your scheduler job with setJobClass(NamePrintJob.class); method, define a task name and put it into Job data map, dataMap.put(“printMyNameTask”, task);. This “task name” is the only link between your Job and JobDetail.
Note : job.setName(“printMyNameTaskJob”) has no special method or function,it is just a descriptive name, you can put anything here.
We will be doing scheduling using cron trigger

package com.vinay.jobs;
import java.util.Map;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

public class VinayQuartzExample
{
public static void main( String[] args ) throws Exception
{
PrintMyNameTask task = new PrintMyNameTask();

JobDetail job = new JobDetail();
job.setName("namePrintJob ");
job.setJobClass(NamePrintJob.class);

Map dataMap = job.getJobDataMap();
dataMap.put("printMyNameTask ", task);

CronTrigger trigger = new CronTrigger();
trigger.setName("vinayjobSchedule");
trigger.setCronExpression("0 15 12 ? * * ");
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}

This task will run every 12:15 am everyday.
Crond Expression- cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of cron.
CronTrigger uses “cron expressions”, which are able to create firing schedules such as: “At 8:00am every Monday through Friday” or “At 1:30am every last Friday of the month”.
Example-
0 0 12 * * ? Fire at 12pm (noon) 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 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

If you want to build cron expression for you. Just use http://www.cronmaker.com/

Reference – http://www.mkyong.com/java/quartz-scheduler-example/
http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html

Vinay

I am an Oracle ACE in Oracle ADF/Webcenter. Sr Java Consultant-working on Java/J2EE/Oracle ADF/Webcenter Portal/ content and Hibernate for several years. I'm an active member of the OTN JDeveloper/Webcenter forum. Passionate about learning new technologies. I am here to share my knowledge. Give your views and suggestion on [email protected] .

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusYouTube