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

Quartz scheduler Example


This example show how to run Quartz with SimpleSchedule and CronSchedule. Quartz can download from official website

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 by implement Job

package com.example.quartz;

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.");
    }
}

3. Create JobDetail
Create SimpleJob.java with detail

        JobDetail job = JobBuilder
                .newJob(SimpleJob.class)
                .withIdentity("SimpleJob")
                .build();

4. Create Trigger
Quartz has provide 2 type of trigger

  • SimpleTrigger (use SimpleScheduleBuilder)
  • CronTrigger (use CronScheduleBuilder)

Create SimpleSchedule

        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("SimpleJob")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
                .build();

Create CronSchedule

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

5. Start Job
Job Start use Scheduler execute

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);

Full source code

package com.example.quartz;

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

public class SimpleSchedule 
{
    public static void main( String[] args ) throws SchedulerException
    {
        JobDetail job = JobBuilder
                .newJob(SimpleJob.class)
                .withIdentity("SimpleJob")
                .build();
        
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("SimpleJob")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
                .build();
        
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job,trigger);
    }
}

Download Source From : Github



me on google plus+Jirawong Wongdokpuang