spring实现定时任务quartz

spring实现定时任务quartz

1.创建相应的web项目,导入对应的jar

2.编写applicationContext.xml(spring的配置文件)

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!-- 启用spring注解支持 -->
 <context:annotation-config />
    
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.test" />

</beans>

3.编写定时任务调度文件applicationContext-quartz.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:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    default-lazy-init="true">
    
<!-- ======================== 任务调度工厂 ======================== -->  
<bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    <property name="triggers">  
        <list>  
            <ref bean="CronTriggerBean"/>  
        </list>  
    </property>  
</bean>

<!-- ======================== 调度触发器 ======================== -->  
<bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
    <property name="jobDetail" ref="SpringQtzJobMethod"></property>  
    <property name="cronExpression" value="0/5 * * * * ?"></property>  
</bean>

<!-- 调用目标任务 -->  
<bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
    <property name="targetObject">  
        <ref bean="SpringQtzJob"/>  
    </property>  
    <property name="targetMethod">  <!-- 要执行的方法名称 -->  
        <value>execute</value>  
    </property>  
</bean>
 
<!-- 方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->   
<!-- 定义目标bean和bean中的方法 -->  
<bean id="SpringQtzJob" class="com.pypua.jobs.SpringQtz"/>

  
  
 
  
    

</beans>

4.编写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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>spring_quartz</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext*.xml
        </param-value>
</context-param>

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

5.定时任务执行的文件SpringQtz.java

package com.pypua.jobs;

import java.util.Date;

public class SpringQtz {

    private static int counter = 0;  
        protected void execute()  {  
            long ms = System.currentTimeMillis();  
            System.out.println("\t\t" + new Date(ms));  
            System.out.println("(" + counter++ + ")");  
        }  

}

执行结果如下

posted @ 2017-08-17 17:54  十月围城小童鞋  阅读(108)  评论(0编辑  收藏  举报