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
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
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Maintenance
  4. Exploring Different Scheduling Types with Quartz 2

Exploring Different Scheduling Types with Quartz 2

Zemian Deng user avatar by
Zemian Deng
·
Nov. 03, 12 · Interview
Like (1)
Save
Tweet
Share
10.46K Views

Join the DZone community and get the full member experience.

Join For Free

We often think of Cron when we want to schedule a job. Cron is very flexible in expressing an repeating occurance of an event/job in a very compact expression. However it's not answer for everything, as I often see people are asking for help in the Quartz user forum. Did you know that the popular Quartz 2 library provide many other schedule types (called Trigger) besides cron? I will show you each of the Quartz 2 built-in schedule types here within a complete, standalone Groovy script that you can run and test it out. Let's start with a simple one.

@Grab('org.quartz-scheduler:quartz:2.1.6')
@Grab('org.slf4j:slf4j-simple:1.7.1')
import org.quartz.*
import org.quartz.impl.*
import org.quartz.jobs.*

import static org.quartz.DateBuilder.*
import static org.quartz.JobBuilder.*
import static org.quartz.TriggerBuilder.*
import static org.quartz.SimpleScheduleBuilder.*

def trigger = newTrigger()
    .withSchedule(
        simpleSchedule()
        .withIntervalInSeconds(3)
        .repeatForever())
    .startNow()
    .build()
dates = TriggerUtils.computeFireTimes(trigger, null, 20)
dates.each{ println it }

 

This is the Quartz's SimpleTrigger, and it allows you to create a fixed rate repeating job. You can even limit to certain number of count if you like. I have imported all the nessary classes the script needs, and I use the latest Quartz 2.x builder API to create an instance of the trigger.

The quickest way to explore and test out whether a scheduling fits your need is to print out its future execution times. Hence you see me using TriggerUtils.computeFireTimes in the script. Run the above and you should get the datetimes as scheduled to be run, in this case every 3 seconds.

bash> $ groovy simpleTrigger.groovy
    Tue Oct 23 20:28:01 EDT 2012
    Tue Oct 23 20:28:04 EDT 2012
    Tue Oct 23 20:28:07 EDT 2012
    Tue Oct 23 20:28:10 EDT 2012
    Tue Oct 23 20:28:13 EDT 2012
    Tue Oct 23 20:28:16 EDT 2012
    Tue Oct 23 20:28:19 EDT 2012
    Tue Oct 23 20:28:22 EDT 2012
    Tue Oct 23 20:28:25 EDT 2012
    Tue Oct 23 20:28:28 EDT 2012
    Tue Oct 23 20:28:31 EDT 2012
    Tue Oct 23 20:28:34 EDT 2012
    Tue Oct 23 20:28:37 EDT 2012
    Tue Oct 23 20:28:40 EDT 2012
    Tue Oct 23 20:28:43 EDT 2012
    Tue Oct 23 20:28:46 EDT 2012
    Tue Oct 23 20:28:49 EDT 2012
    Tue Oct 23 20:28:52 EDT 2012
    Tue Oct 23 20:28:55 EDT 2012
    Tue Oct 23 20:28:58 EDT 2012

 

The most frequent used scheduling type is the CronTrigger, and you can test it out in similar way.

@Grab('org.quartz-scheduler:quartz:2.1.6')
@Grab('org.slf4j:slf4j-simple:1.7.1')
import org.quartz.*
import org.quartz.impl.*
import org.quartz.jobs.*

import static org.quartz.DateBuilder.*
import static org.quartz.JobBuilder.*
import static org.quartz.TriggerBuilder.*
import static org.quartz.CronScheduleBuilder.*

def trigger = newTrigger()
    .withSchedule(cronSchedule("0 30 08 * * ?"))
    .startNow()
    .build()
dates = TriggerUtils.computeFireTimes(trigger, null, 20)
dates.each{ println it }

 

The javadoc for CronExpression is very good and you should definately read it throughly to use it effectively. With the script, you can explore all the combination you want easily and verify future fire times before your job is invoked.

Now, if you have some odd scheduling needs such as run a job every 30 mins from MON to FRI and only between 8:00AM to 10:00AM, then don't try to cramp all that into the Cron expression. The Quartz 2.x has a dedicated trigger type just for this use, and it's called DailyTimeIntervalTrigger! Check this out:

@Grab('org.quartz-scheduler:quartz:2.1.6')
@Grab('org.slf4j:slf4j-simple:1.7.1')
import org.quartz.*
import org.quartz.impl.*
import org.quartz.jobs.*

import static org.quartz.DateBuilder.*
import static org.quartz.JobBuilder.*
import static org.quartz.TriggerBuilder.*
import static org.quartz.DailyTimeIntervalScheduleBuilder.*
import static java.util.Calendar.*

def trigger = newTrigger()
    .withSchedule(
        dailyTimeIntervalSchedule()
        .startingDailyAt(TimeOfDay.hourMinuteAndSecondOfDay(8, 0, 0))
        .endingDailyAt(TimeOfDay.hourMinuteAndSecondOfDay(10, 0, 0))
        .onDaysOfTheWeek(MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY)
        .withInterval(10, IntervalUnit.MINUTE))
    .startNow()
    .build()
dates = TriggerUtils.computeFireTimes(trigger, null, 20)
dates.each{ println it }

 

Another hidden Trigger type from Quartz is CalendarIntervalTrigger, and you would use this if you need to repeat job that's in every interval of a calendar period, such as every year or month etc, where the interval is not fixed, but calendar specific. Here is a test script for that.

@Grab('org.quartz-scheduler:quartz:2.1.6')
@Grab('org.slf4j:slf4j-simple:1.7.1')
import org.quartz.*
import org.quartz.impl.*
import org.quartz.jobs.*

import static org.quartz.DateBuilder.*
import static org.quartz.JobBuilder.*
import static org.quartz.TriggerBuilder.*
import static org.quartz.CalendarIntervalScheduleBuilder.*
import static java.util.Calendar.*

def trigger = newTrigger()
    .withSchedule(
        calendarIntervalSchedule()
        .withInterval(2, IntervalUnit.MONTH))
    .startAt(futureDate(10, IntervalUnit.MINUTE))
    .build()
dates = TriggerUtils.computeFireTimes(trigger, null, 20)
dates.each{ println it }

 

I hope these will help you get started on most of your scheduling need with Quartz 2. Try these out and see your future fire times before even scheduling a job into the scheduler should save you some times and troubles.

job scheduling Quartz (scheduler)

Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Express Hibernate Queries as Type-Safe Java Streams
  • How To Validate Three Common Document Types in Python
  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • Using the PostgreSQL Pager With MariaDB Xpand

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: