Quatrz + Spring

实例工程结构:

配置:

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>com.gdut.yh</groupId>
    <artifactId>QuatrzSpring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <springframework.version>3.0.6.RELEASE</springframework.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>1.8.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- define the project compile level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:*.xml
    </param-value>
  </context-param>
  
<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  </web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">  
  
    <!-- 任务1配置 -->  
    <bean name="job1" class="com.gdut.job.Job1Demo"/>  
    <bean id="jobDetail_1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <!-- 执行的类 -->  
        <property name="targetObject">  
            <ref bean="job1" />  
        </property>  
        <!-- 类中的方法 -->  
        <property name="targetMethod">  
            <value>sayHello</value>  
        </property>  
    </bean>  
    <bean id="cronTrigger_1" class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <property name="jobDetail">  
            <ref bean="jobDetail_1" />  
        </property>  
        <!-- 每一秒钟执行一次 -->  
        <property name="cronExpression">  
            <value>0/1 * * * * ?</value>  
        </property>  
    </bean>  
      
    <!-- 任务2配置 -->  
    <bean name="job2" class="com.gdut.job.Job2Demo"/>  
    <bean id="jobDetail_2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">  
            <ref bean="job2" />  
        </property>  
        <property name="targetMethod">  
            <value>sayHello</value>  
        </property>  
    </bean>  
    <bean id="cronTrigger_2" class="org.springframework.scheduling.quartz.CronTriggerBean">  
        <property name="jobDetail">  
            <ref bean="jobDetail_2" />  
        </property>  
        <property name="cronExpression">  
            <value>0/1 * * * * ?</value>  
        </property>  
    </bean>  
      
    <!-- 总配置 -->  
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <!-- 添加触发器 -->  
        <property name="triggers">  
            <list>  
                <ref bean="cronTrigger_1" />  
                <ref bean="cronTrigger_2" />  
            </list>  
        </property>  
    </bean>  
</beans> 

Job1Demo.java

package com.gdut.job;

import java.util.Date;

public class Job1Demo {
	public void sayHello() {  
        System.out.println(new Date() + " -> Hello, 我是任务 1");  
    }  
}

Job2Demo.java  

package com.gdut.job;

import java.util.Date;

public class Job2Demo {
	public void sayHello() {  
        System.out.println(new Date() + " -> Hello, 我是任务 2");  
    } 
}

 

部署到tomcat,启动如下:

 

posted @ 2019-08-24 23:07  myseries  阅读(263)  评论(0编辑  收藏  举报