• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Quartz Job Scheduler – Example Code

February 21, 2013 //  by Krishna Srinivasan//  Leave a Comment

In this section we are going to develop a simple Quartz Scheduler application with the help of Quartz framework. That will display Hello World Quartz Scheduler :date & time on the console window after specified time schedule.

Before using Scheduler you have to instantiate it. For doing this some users may keep an instance of a factory serialized in a JNDI store, and some other users may find it just to instantiate and use a factory instance. Firstly create the instance of SchedulerFactory by getting the reference of org.Quartz.impl.StdSchedulerFactory Class. Invoke the getScheduler() method by this instance to instantiate the Scheduler.

Quartz Scheduler Application

We have published list of articles on latest Quartz scheduler.

  • Quartz Scheduler Articles and Tutorials
  • Quartz Scheduler Tutorial
  • Configure Quartz Scheduler Jobs using XML File

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.

SchedulerFactory: SchedulerFactory is a interface provides a mechanism for obtaining client-usable handles to Scheduler instances.

StdSchedulerFactory(): A Class StdSchedulerFactory is a class and it is implementation of SchedulerFactory interface. Here it just using for create an instance of SchedulerFactory instance.

Scheduler: Scheduler interface is the main interface (API) to this functionality. It provides some simple operations like scheduling jobs, unschedulingjobs, starting/stopping/pausing the scheduler.

getScheduler():SchedulerFactoy interface having the getScheduler() method that returns an instance of Scheduler.

start(): This method is used to starts the Scheduler’s threads that fire Triggers. At the first time when we create the Scheduler it is in stand-by mode, and will not fire triggers. The scheduler can also be send back into stand-by mode by invoking the standby() method.

JobDetail(String name, String group, Class jobclass): The JobDetail object is created at the time the Job is added to scheduler. It contains various property settings like job name, group name and job class name. It can be used to store state information for a given instance of job class.

SimpleTrigger(String name, String group, Date startTime, Date endTime, int repeatCount, long repeatInterval): Trigger objects are used to firing the execution of jobs. When you want to schedule the job, instantiate the trigger and set the properties to provide the scheduling.

DEFAULT_GROUP: It is a constant, specified that Job and Trigger instances are belongs to which group.

REPEAT_INDEFINITELY: It is a constant used to indicate the ‘repeat count‘ of the trigger is indefinite.

scheduleJob(JobDetail jd, SimpleTrigger st): This method is used to add the JobDetail to the Scheduler, and associate the Trigger with it.

Here is the code of Job Class:

1. Create a Job using Quartz’s Job Interface

HelloJob.java

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;public class HelloJob implements Job {
       public void execute(JobExecutionContext arg0) throws JobExecutionException{
       System.out.println(Hello World Quartz Scheduler:  + new Date());
   }
}

Here is the code of Scheduler Class:

2. Create a Schedule with Trigger

HelloSchedule.java

import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class HelloSchedule {
    public HelloSchedule()throws Exception{
       SchedulerFactory sf=new StdSchedulerFactory();
       Scheduler sched=sf.getScheduler();
       sched.start();
       JobDetail jd=new JobDetail("myjob",sched.DEFAULT_GROUP,HelloJob.class);
       SimpleTrigger st=new SimpleTrigger("mytrigger",sched.DEFAULT_GROUP,new Date(),
                  null,SimpleTrigger.REPEAT_INDEFINITELY,60L*1000L);
       sched.scheduleJob(jd, st);
    }
    public static void main(String args[]){
       try{
         new HelloSchedule();
       }catch(Exception e){}
    }
}

Category: JavaTag: Quartz Scheduler

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « What is load on startup element in web.xml file
Next Post: Example for a class implementing comparable interface in Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact