Quartz 2のJobListenerの例

このチュートリアルでは、ジョブが終了したときなど、実行中のジョブの状態を追跡するために `JobListener`を作成する方法を説明します。

P.Sこの例はQuartz 2.1.5 でテストされています

1.クォーツ・ジョブ

単純なメッセージを出力し、テストのために `JobExecutionException`をスローします。

File:HelloJob.java

package com.mkyong.quartz;

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

public class HelloJob implements Job
{
    public void execute(JobExecutionContext context)
    throws JobExecutionException {

        System.out.println("Hello Quartz! 123");

       //Throw exception for testing
        throw new JobExecutionException("Testing Exception");
    }

}

2. JobListener

JobListenerを作成するには、単に `JobListener`インターフェースを実装し、すべてのインターフェースのメソッドをオーバーライドします。

File:HelloJobListener.java

package com.mkyong.quartz.listener;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class HelloJobListener implements JobListener {

    public static final String LISTENER__NAME = "dummyJobListenerName";

    @Override
    public String getName() {
        return LISTENER__NAME;//must return a name
    }

   //Run this if job is about to be executed.
    @Override
    public void jobToBeExecuted(JobExecutionContext context) {

        String jobName = context.getJobDetail().getKey().toString();
        System.out.println("jobToBeExecuted");
        System.out.println("Job : " + jobName + " is going to start...");

    }

   //No idea when will run this?
    @Override
    public void jobExecutionVetoed(JobExecutionContext context) {
        System.out.println("jobExecutionVetoed");
    }

   //Run this after job has been executed
    @Override
    public void jobWasExecuted(JobExecutionContext context,
            JobExecutionException jobException) {
        System.out.println("jobWasExecuted");

        String jobName = context.getJobDetail().getKey().toString();
        System.out.println("Job : " + jobName + " is finished...");

        if (!jobException.getMessage().equals("")) {
            System.out.println("Exception thrown by: " + jobName
                + " Exception: " + jobException.getMessage());
        }

    }

}

3. CronTrigger

`HelloJobListener`をスケジューラーに添付し、ジョブの状態をモニターする例。

File:CronTriggerExample.java

package com.mkyong.quartz;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;

import com.mkyong.quartz.listener.HelloJobListener;

public class CronTriggerExample {
    public static void main( String[]args ) throws Exception
    {

    JobKey jobKey = new JobKey("dummyJobName", "group1");
        JobDetail job = JobBuilder.newJob(HelloJob.class)
        .withIdentity(jobKey).build();

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

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();

       //Listener attached to jobKey
        scheduler.getListenerManager().addJobListener(
            new HelloJobListener(), KeyMatcher.keyEquals(jobKey)
        );

       //Listener attached to group named "group 1" only.
       //scheduler.getListenerManager().addJobListener(
       // new HelloJobListener(), GroupMatcher.jobGroupEquals("group1")
       //);

        scheduler.start();
        scheduler.scheduleJob(job, trigger);

    }
}

`CronTriggerExample.java`を実行します。ここに出力があります。

jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Quartz! 123
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception

jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Quartz! 123
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception

ソースコードをダウンロードする

ダウンロードする - リンク://wp-content/uploads/2012/07/Quartz2-JobListener-Example.zip[Quartz2-JobListener-Example.zip](13 KB)

参考文献

2 JobListenerドキュメント]。リンク://java/quartz-2-scheduler-tutorial/[Quartz 2 hello worldの例]