web-dev-qa-db-ja.com

Quartz 2.2およびTomcat 7の簡単な例

Java動的Webアプリケーション。このタスクは初めてです。Webのすべてのチュートリアルを試してみました。スケジューラを初期化するコンテキストリスナーメソッドを試しました。 hello worldプログラムは、一般的にのみ動作しますJava application。Webアプリケーションの場合、見た目が複雑です。

pom.xml:

    <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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.ananth</groupId>
<artifactId>test-app</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-app Maven Webapp</name>
<url>http://maven.Apache.org</url>
<dependencies>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.Apache.Tomcat</groupId>
        <artifactId>servlet-api</artifactId>
        <version>6.0.30</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>

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

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.6</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalName>test-app</finalName>
</build>

quartz.properties:

#org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

# Main Quartz configuration
org.quartz.scheduler.skipUpdateCheck = true
org.quartz.scheduler.instanceName = MyQuartzScheduler
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
#org.quartz.threadPool.threadCount = 5

HelloJob.Java:

    package com.test;

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

    public class HelloJob implements Job {

public HelloJob() {
}

public void execute(JobExecutionContext context)
        throws JobExecutionException {

    System.out.println("Hello!  HelloJob is executing.");
}
    }

servlet.Javaパッケージcom.test;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class MyServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1567185871113714035L;

    public void init(ServletConfig cfg) {
        String key = "org.quartz.impl.StdSchedulerFactory.KEY";
        ServletContext servletContext = cfg.getServletContext();
        StdSchedulerFactory factory = (StdSchedulerFactory) servletContext
                .getAttribute(key);
        // Scheduler quartzScheduler = factory.getScheduler("MyQuartzScheduler");
        Scheduler sched;
        try {
            sched = factory.getScheduler("MyQuartzScheduler");
            //sched = factory.getScheduler();//MyQuartzScheduler
            sched.start();

            // define the job and tie it to our HelloJob class
            JobDetail job = newJob(HelloJob.class).withIdentity("myJob",
                    "group1").build();

            // Trigger the job to run now, and then every 40 seconds
            Trigger trigger = newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .startNow()
                    .withSchedule(
                            simpleSchedule().withIntervalInSeconds(4)
                                    .repeatForever()).build();

            // Tell quartz to schedule the job using our trigger
            sched.scheduleJob(job, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }
}

Web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://Java.Sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <context-param>
        <context-param>
            <param-name>quartz:shutdown-on-unload</param-name>
            <param-value>false</param-value>
        </context-param>
        <context-param>
            <param-name>quartz:wait-on-shutdown</param-name>
            <param-value>true</param-value>
        </context-param>
        <context-param>
            <param-name>quartz:start-scheduler-on-load</param-name>
            <param-value>true</param-value>
        </context-param>
        <context-param>
            <param-name>quartz:config-file</param-name>
            <param-value>/WEB-INF/quartz.properties</param-value>
        </context-param>

        <listener>
            <listener-class>
                org.quartz.ee.servlet.QuartzInitializerListener
            </listener-class>
        </listener>

        <listener>
            <listener-class>com.test.ApplicationStartup</listener-class>
        </listener>
        <display-name>Archetype Created Web Application</display-name>
</web-app>

私はMaven Webアプリのarchtypeを使用しています。

35
Poornan

内容

  • Eclipseプロジェクト
  • Mavenを使用
  • XMLレス

Eclipseプロジェクト

Eclipseで典型的なプロジェクトを使用している場合、最も基本的な例の構造は次のようになります。

_C:.
|
+---src
|   |   log4j.dtd
|   |   log4j.xml
|   |   quartz.properties
|   |   quartz_data.xml
|   |
|   \---org
|       \---paulvargas
|           \---test
|               \---quartz
|                       TestJob.Java
|
\---WebContent
    \---WEB-INF
        |   web.xml
        |
        \---lib
                jta-1.1.jar
                log4j-1.2.17.jar
                quartz-2.1.5.jar
                slf4j-api-1.6.5.jar
                slf4j-log4j12-1.6.5.jar
_

各ファイルのコードは次のとおりです。

_web.xml_

_<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://Java.Sun.com/xml/ns/javaee" xmlns:web="http://Java.Sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://Java.Sun.com/xml/ns/javaee http://Java.Sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <listener>
        <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
    </listener>

</web-app>
_

_quartz.properties_

_# ----------------------------- Threads --------------------------- #
# How many jobs can run at the same time?
org.quartz.threadPool.threadCount=5

# ----------------------------- Plugins --------------------------- #
# Class to load the configuration data for each job and trigger.
# In this example, the data is in an XML file.
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
_

_quartz_data.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_2_0.xsd"
    version="1.8">

    <schedule>
        <job>
            <name>TestJob</name>
            <job-class>org.paulvargas.test.quartz.TestJob</job-class>
        </job>
        <trigger>
            <cron>
                <name>TestJob</name>
                <job-name>TestJob</job-name>
                <cron-expression>0 0/5 * 1/1 * ? *</cron-expression>
            </cron>
        </trigger>
    </schedule>

</job-scheduling-data>
_

ジョブは5分ごとに実行されます(_0 0/5 * 1/1 * ? *_タグの式_cron-expression_を参照)。別の式が必要な場合は、 _http://www.cronmaker.com/_ で作成できます

_TestJob.Java_

_package org.paulvargas.test.quartz;

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

public class TestJob implements Job {

    @Override
    public void execute(final JobExecutionContext ctx)
            throws JobExecutionException {

        System.out.println("Executing Job");

    }

}
_

_log4j.xml_

_<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd" >
<log4j:configuration>

    <appender name="STDOUT" class="org.Apache.log4j.ConsoleAppender">
        <layout class="org.Apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p (%c.Java:%L).%M - %m%n"/>
        </layout>
    </appender>

    <root>
        <priority value="TRACE" />
        <appender-ref ref="STDOUT"/>
    </root>

</log4j:configuration>
_

Mavenを使用

Mavenを使用している場合、同じプロジェクトの構造は次のとおりです。

_C:.
|   pom.xml
|
\---src
    \---main
        +---Java
        |   \---org
        |       \---paulvargas
        |           \---test
        |               \---quartz
        |                       TestJob.Java
        |
        +---resources
        |       log4j.dtd
        |       log4j.xml
        |       quartz.properties
        |       quartz_data.xml
        |
        \---webapp
            |   index.jsp
            |
            \---WEB-INF
                    web.xml
_

ファイル_pom.xml_:

_<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>test</groupId>
    <artifactId>BasicQuartz</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>BasicQuartz</name>
    <url>http://maven.Apache.org</url>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <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>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.Apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
_

XMLレス

*これにはServet 3.0以降が必要です(Tomcat 7以降、Glassfish 3以降、JBoss AS 7)

必要なファイルは、前の例の_TestJob.Java_と次のリスナーの2つだけです。

_import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;

@WebListener
public class QuartzListener extends QuartzInitializerListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        super.contextInitialized(sce);
        ServletContext ctx = sce.getServletContext();
        StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
        try {
            Scheduler scheduler = factory.getScheduler();
            JobDetail jobDetail = JobBuilder.newJob(TestJob.class).build();
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
                    CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).startNow().build();
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
        } catch (Exception e) {
            ctx.log("There was an error scheduling the job.", e);
        }
    }

}
_

競合を避けるために、_web.xml_にデフォルトのリスナーを同時に設定しないでください。この最後の例では、デフォルトのスレッド数は10です。スケジューラーはスタンバイモードで起動したため、scheduler.start();を呼び出す必要があります。 _"simple"_ IDはオプションですが、ジョブのスケジュールを変更するために使用できます(素晴らしい!)。例えば。:

_ServletContext ctx = request.getServletContext();
StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QuartzListener.QUARTZ_FACTORY_KEY);
Scheduler scheduler = factory.getScheduler();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
        CronScheduleBuilder.cronSchedule(newCronExpression)).startNow().build();
Date date = scheduler.rescheduleJob(new TriggerKey("simple"), trigger);
_
73
Paul Vargas