Quartzで複数のジョブを実行する例

この例では、Quartz API、Quartz XML、およびSpringを介して複数のQuartzジョブを宣言する方法を示します。クォーツスケジューラフレームワークでは、各ジョブは一意のトリガに接続され、スケジューラによって実行されます。

P.S Quartzでは、複数のジョブに対して1つのトリガーが使用できません。 (これが間違っていれば私を訂正してください)

1. Quartz API

3つのQuartzのジョブ、JobS、JobS、JobSを作成します。

package com.mkyong.quartz;

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

public class JobA implements Job {

    @Override
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        System.out.println("Job A is runing");
    }

}
package com.mkyong.quartz;

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

public class JobB implements Job {

    @Override
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        System.out.println("Job B is runing");
    }

}
package com.mkyong.quartz;

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

public class JobC implements Job {

    @Override
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        System.out.println("Job C is runing");
    }

}

Quartz APIを使用して、上記3つのジョブを宣言し、3つの特定のトリガーに割り当て、スケジュールします。

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;

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

    JobKey jobKeyA = new JobKey("jobA", "group1");
        JobDetail jobA = JobBuilder.newJob(JobA.class)
        .withIdentity(jobKeyA).build();

        JobKey jobKeyB = new JobKey("jobB", "group1");
        JobDetail jobB = JobBuilder.newJob(JobB.class)
        .withIdentity(jobKeyB).build();

        JobKey jobKeyC = new JobKey("jobC", "group1");
        JobDetail jobC = JobBuilder.newJob(JobC.class)
        .withIdentity(jobKeyC).build();


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

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

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

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

        scheduler.start();
        scheduler.scheduleJob(jobA, trigger1);
        scheduler.scheduleJob(jobB, trigger2);
        scheduler.scheduleJob(jobC, trigger3);

    }
}

出力

Job A is runing//every 5 seconds
Job B is runing
Job C is runing
Job A is runing//every 5 seconds
Job B is runing
Job C is runing

2. Quartz XMLの例

XMLファイルの同等のバージョン。 "quartz.properties"と "quartz-config.xml"がプロジェクトのクラスパスにあることを確認してください。

File - quartz.properties

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true

File - quartz-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
    xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData
        http://www.quartz-scheduler.org/xml/job__scheduling__data__1__8.xsd"
    version="1.8">

    <schedule>
        <job>
            <name>JobA</name>
            <group>GroupDummy</group>
            <description>This is Job A</description>
            <job-class>com.mkyong.quartz.JobA</job-class>
        </job>

        <trigger>
            <cron>
                <name>dummyTriggerNameA</name>
                <job-name>JobA</job-name>
                <job-group>GroupDummy</job-group>
                <!-- It will run every 5 seconds -->
                <cron-expression>0/5 **  **  **  **  ?</cron-expression>
            </cron>
        </trigger>
    </schedule>

    <schedule>
        <job>
            <name>JobB</name>
            <group>GroupDummy</group>
            <description>This is Job B</description>
            <job-class>com.mkyong.quartz.JobB</job-class>
        </job>

        <trigger>
            <cron>
                <name>dummyTriggerNameB</name>
                <job-name>JobB</job-name>
                <job-group>GroupDummy</job-group>
                <!-- It will run every 5 seconds -->
                <cron-expression>0/5 **  **  **  **  ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>

ファイル:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
<listener>
    <listener-class>
       org.quartz.ee.servlet.QuartzInitializerListener
   </listener-class>
</listener>
</web-app>

3. Springの例

Springの同等のバージョン。

package com.mkyong.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class JobA extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext arg0)
        throws JobExecutionException {
        System.out.println("Job A is runing");
    }

}
package com.mkyong.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class JobB extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext arg0)
        throws JobExecutionException {
        System.out.println("Job B is runing");

    }

}
package com.mkyong.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class JobC extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext arg0)
        throws JobExecutionException {
        System.out.println("Job C is runing");

    }

}

File:Spring-Quartz.xml - Spring XML Bean設定ファイルでジョブとトリガを宣言します。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="jobA" class="com.mkyong.job.JobA"/>
    <bean id="jobB" class="com.mkyong.job.JobB"/>
    <bean id="jobC" class="com.mkyong.job.JobC"/>

    <!-- Quartz Job -->
    <bean name="JobA" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.mkyong.job.JobA"/>
    </bean>

    <bean name="JobB" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.mkyong.job.JobB"/>
    </bean>

    <bean name="JobC" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.mkyong.job.JobC"/>
    </bean>

    <!-- Cron Trigger, run every 5 seconds -->
    <bean id="cronTriggerJobA"
                class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="JobA"/>
        <property name="cronExpression" value="0/5 **  **  **  **  ?"/>
    </bean>

    <bean id="cronTriggerJobB"
                class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="JobB"/>
        <property name="cronExpression" value="0/5 **  **  **  **  ?"/>
    </bean>

    <bean id="cronTriggerJobC"
                class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="JobC"/>
        <property name="cronExpression" value="0/5 **  **  **  **  ?"/>
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerJobA"/>
                <ref bean="cronTriggerJobB"/>
                <ref bean="cronTriggerJobC"/>
            </list>
        </property>
    </bean>

</beans>

それを実行します

package com.mkyong.common;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[]args) throws Exception {
        new ClassPathXmlApplicationContext("Spring-Quartz.xml");

    }
}

出力。

INFO: Starting beans in phase 2147483647
Jul 30, 2012 10:38:13 PM org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
INFO: Starting Quartz Scheduler now
Job A is runing
Job B is runing
Job C is runing
Job A is runing
Job B is runing
Job C is runing

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

それをダウンロードする - Multiple-Jobs-in-Quartz-Spring-Example.zip (25kb)