Schedule Quartz CronTrigger Example

Quartz is a java enterprise ready Job Scheduler. In the previous tutorial we saw how you can create a simple Quartz trigger Scheduler. In this tutorial we will show you how you can create a Schedule Quartz CronTrigger.

Maven Dependencies

First add the necessary libraries to your project. We need both org.quartz-scheduler.quartz and code>org.quartz-scheduler.quartz-jobs.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.quartz</groupId>
    <artifactId>simple-trigger</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>QUARTZ - ${project.artifactId}</name>

    <dependencies>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
    </dependencies>

</project>

Create a Quartz Job

We can create a Quartz job by implementing the org.quartz.Job interface which obligates us to override the execute() method which is fired when the job gets executed. The JobExecutionContext allows you to retrieve meta information about the current executing job.

package com.memorynotfound.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class BakeWaffles implements Job {

    @Override
    public void execute(JobExecutionContext ctx) throws JobExecutionException {
        System.out.println("baking waffles at: " + ctx.getFireTime());
    }
}

Schedule Quartz CronTrigger Example

Next lets put all the pieces together. First we create a job using the JobBuilder we give the job an identity name waffleJob with a group name foodGroup. We can use this later on if we want to register a JobListener or TriggerListener to listen only to certain events. But this is for the next tutorial. Next we create a CronTrigger again we give the trigger an identity and we schedule the cron trigger using a cron-job string notation. The string 0 0/1 * * * ? means that we schedule the job every minute starting now. Finally we create a scheduler using the StdSchedulerFactory and schedule the job using the scheduleJob() method.

package com.memorynotfound.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

public class SimpleTriggerProgram {

    public static void main(String... args) throws SchedulerException {

        // create job
        JobDetail job = JobBuilder.newJob(BakeWaffles.class)
                .withIdentity("waffleJob", "foodGroup")
                .build();

        // create trigger
        // Build a trigger that will fire every minute starting now
        CronTrigger trigger = newTrigger()
                .withIdentity("waffleTrigger", "foodGroup")
                .withSchedule(cronSchedule("0 0/1 * * * ?"))
                .build();

        // schedule it
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
    }
}

Output

baking waffles at: Wed Jun 17 19:47:00 CEST 2015
baking waffles at: Wed Jun 17 19:48:00 CEST 2015
baking waffles at: Wed Jun 17 19:49:00 CEST 2015
baking waffles at: Wed Jun 17 19:50:00 CEST 2015
baking waffles at: Wed Jun 17 19:51:00 CEST 2015
baking waffles at: Wed Jun 17 19:52:00 CEST 2015

References

Download

You may also like...