DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Quartz Code Snippets for Beginners

Spring Quartz Code Snippets for Beginners

Vineet Manohar user avatar by
Vineet Manohar
·
Oct. 12, 10 · Interview
Like (0)
Save
Tweet
Share
15.78K Views

Join the DZone community and get the full member experience.

Join For Free

Some applications need to run periodic jobs in the background. On Linux/Unix systems, background processes can be run using the “cronjob” scheduling service. In Java web applications, you can add scheduling to any app in 4 simple steps using Spring and Quartz. This article outlines the steps needed for scheduling, with code snippets required for each step.

Step 1: web.xml

To initialize Spring in your webapp, add this snippet to your web.xml file. If you are already using Spring, you probably already have this in your web.xml.

<!-- Initialize Spring -->
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Step 2: applicationContext.xml snippet

There are 3 concepts when configuring your scheduled jobs:

1) The Job: the java code that you want to run in the background periodically

2) A Trigger: a trigger adds scheduling information to your job. There are different types of triggers (e.g. SimpleTrigger, CronTrigger). They differ in the way you specify scheduling information. For example, CronTrigger takes a cronexpression, whereas SimpleTrigger takes a repeatInterval.

3) The Scheduler: this is the actual service which runs in the background and invokes ‘triggers’. You can register multiple triggers with this service.

Add this snippet to your Spring xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

 <!-- The 'Scheduler': quartz scheduler -->
 <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <!-- list of triggers registered with the scheduler -->
  <property name="triggers">
   <list>
    <!-- Trigger1: JobExample using CronTrigger -->
    <bean id="jobDetailTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
     <!-- job class -->
     <property name="jobDetail">
      <bean class="org.springframework.scheduling.quartz.JobDetailBean">
       <!-- CHANGE THIS to the actual implementation class (see step 4)  -->
       <property name="jobClass" value="com.vineetmanohar.example.quartz.JobExample" />
      </bean>
     </property>

     <!-- job schedule: every 5 minutes: CHANGE THIS -->
     <property name="cronExpression" value="0 0/30 * * * ?" />
    </bean>

    <!-- Trigger2: JobExample2 using SimpleTrigger -->
    <bean class="org.springframework.scheduling.quartz.SimpleTriggerBean">
     <property name="jobDetail">
      <bean>
       <!-- CHANGE THIS to the actual implementation class (see step 4)  -->
       <property name="jobClass" value="com.vineetmanohar.example.quartz.JobExample2" />
      </bean>
     </property>

     <!-- wait 4 minutes after startup -->
     <property name="startDelay" value="240000" />

     <!-- every 1 min (60 sec) -->
     <property name="repeatInterval" value="60000" />
    </bean>
   </list>
  </property>
 </bean>

</beans>

Step 3: pom.xml snippet

If you are using Maven, add this to your pom.xml to add Spring and Quartz dependency to your project.

<project>
 <dependencies>
  <dependency>
   <groupId>org.opensymphony.quartz</groupId>
   <artifactId>quartz</artifactId>
   <version>1.6.1</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>2.5</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>2.5</version>
  </dependency>
 </dependencies>
</project>

Step 4: Java code snippet

Finally, write the actual implementation. Make sure that job class you specified in step 2 points to this file.

package com.vineetmanohar.example.quartz.JobExample;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class JobExample extends QuartzJobBean {
  protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
   System.out.println("Job invoked at : " + new java.util.Date());

   // TODO: implement the job here
 }
}

Cron expression

The format of cron expression is <Seconds> <Minutes> <Hours> <Day-of-Month> <Month> <Day-of-Week> <Year (optional field)>. For example:

Every 30 minutes

0 0/30 * * * ? 

See full reference documentation here.

From http://www.vineetmanohar.com/2010/10/spring-quartz-code-snippets-for-beginners

Snippet (programming) Spring Framework Quartz (scheduler) job scheduling

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Path From APIs to Containers
  • How to Submit a Post to DZone
  • HTTP vs Messaging for Microservices Communications
  • Full Lifecycle API Management Is Dead

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: