Quartz scheduler JobListener Example


This example will show how to run JobListener for track status of Quartz job.


above image show project structure (this project used maven)

1. Add denpendency
add Quartz maven dependency in pom.xml

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.1.7</version>
        </dependency>

2. Create Job
create quartz job

package com.example.quartzlistener;

import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SimpleJob implements Job {

    public void execute(JobExecutionContext jec) throws JobExecutionException {
        System.out.println("doing simple job. " + new Date());
    }
}

3. Create JobListener
create joblistener by implement JobListener interface

package com.example.quartzlistener;

import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class SimpleJobListener implements JobListener{

    public String getName() {
        return "SimpleJobListener";
    }

    public void jobToBeExecuted(JobExecutionContext context) {
        System.out.println("jobToBeExecuted has execute. "+new Date());
    }

    public void jobExecutionVetoed(JobExecutionContext context) {
        System.out.println("jobExecutionVetoed has execute. "+new Date());
    }

    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        System.out.println("jobWasExecuted has execute. "+new Date()+"\r\n");
    }
    
}

4. Run Job
Create file for execute job

package com.example.quartzlistener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class SimpleScheduler {

    public static void main(String[] args) throws SchedulerException {
        JobDetail jobDetail = JobBuilder
                .newJob(SimpleJob.class)
                .withIdentity("SimpleJob")
                .build();

        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("SimpleJob")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.getListenerManager().addJobListener(new SimpleJobListener());
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);
    }
}

run SimpleScheduler we will get result

jobToBeExecuted has execute. Fri Mar 29 18:52:35 ICT 2013
doing simple job. Fri Mar 29 18:52:35 ICT 2013
jobWasExecuted has execute. Fri Mar 29 18:52:35 ICT 2013

jobToBeExecuted has execute. Fri Mar 29 18:52:40 ICT 2013
doing simple job. Fri Mar 29 18:52:40 ICT 2013
jobWasExecuted has execute. Fri Mar 29 18:52:40 ICT 2013

from out put show jobToBeExecuted befor do job and after job complete will show jobWasExecuted ,
not jobExecutionVetoed has execute.

ok, jobExecutionVetoed has execute when job has veto by TriggerListener, i will show code to veto job below.

Create TriggerListener

package com.example.quartzlistener;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;

public class SimpleTriggerListener implements TriggerListener{

    public String getName() {
        return "SimpleTriggerListener";
    }

    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        System.out.println("triggerFired ");
    }

    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        //return true when need veto job
        return true;
    }

    public void triggerMisfired(Trigger trigger) {
        System.out.println("triggerMisfired ");
    }

    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        System.out.println("triggerComplete ");
    }
    
}

after that edit SimpleScheduler.java by add TriggerListener to Scheduler

...
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.getListenerManager().addJobListener(new SimpleJobListener());
        scheduler.getListenerManager().addTriggerListener(new SimpleTriggerListener());
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);
...

we will get output

triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 18:59:50 ICT 2013
triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 18:59:55 ICT 2013
triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 19:00:00 ICT 2013
triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 19:00:05 ICT 2013

jobExecutionVetoed has execute and job has veto.

Download Source From : GitHub



me on google plus+Jirawong Wongdokpuang

Advertisements
Quartz scheduler JobListener Example

One thought on “Quartz scheduler JobListener Example

  1. konidalaswaroop says:

    hi thank you for your very simple and clean example, now i need to implement this in web application running on the server, will you be able to guide me further on using quartz scheduler

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s