Java Tomcat7中使用Quartz2.2实现定时任务项目代码实例(demo)

在 Java Web 项目中使用 Quartz 2.2 和 Tomcat 7 实现定时任务是一个常见的需求。在Java的Web网站中,并且在Tomcat7上,使用Quartz2.2实现定时任务。

1、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

2、介绍一下项目中文件内容

1)网站的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/javaeehttp://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>

2)Quartz的配置文件quartz.properties

 ----------------------------- Threads --------------------------- #
# 同一时运行的任务数
org.quartz.threadPool.threadCount=5
# ----------------------------- Plugins --------------------------- #
# 加载每个任务和触发器的配置数据
# 在本实例中,是通过xml文件配置
org.quartz.plugin.jobInitializer.class=org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin

3)Quartz的任务和执行时间的配置文件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/JobSchedulingDatahttp://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>

4)通过Quartz实现定时任务的代码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");
}
}

5)日志框架的配置文件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>

3、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

1)Maven项目中的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>

2)通过Quartz实现定时任务的代码TestJob.java

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

注意:上面代码中TriggerBuilder.newTrigger().withIdentity("simple")代码中,指定的标识'simple'是可选的,指定了就可以通过这个标识重新安排任务,例如,

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);

推荐阅读
cjavapy编程之路首页