spring中配置quartz定时器

时间:2021-05-24 02:15:04

spring中配置quartz定时器

jar包说明

spring3.0+quartz1.0版本,这里如果使用quartz2.0的话,spring的jar包要3.2版本以上

spring中配置quartz定时器

下载地址:http://download.csdn.net/detail/dzy21/9582333

实体类的方法

定时器就来调用这个方法

package com.cloud.impl;

publicclass GetDataImpl {

   publicvoid getData(){

      System.out.println("测试定时器的数据拉取...");

   }

}

spring配置文件

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="

            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   <!-- 配置一个定时器 -->

   <beanid="getDataImpl"class="com.cloud.impl.GetDataImpl"></bean>

   <!-- 定时器:获取数据第一步 -->

   <beanid="getDataQuart"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

      <!--配置获取数据的service -->

      <propertyname="targetObject"ref="getDataImpl"/>

      <!--配置执行的method -->

      <propertyname="targetMethod"value="getData"/>

      <!--设置作业不并发调度 -->

      <propertyname="concurrent"value="false"/>

   </bean>

   <!-- 定时器:设置定时启动时间 -->

   <beanid="getDataTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">

      <propertyname="jobDetail"ref="getDataQuart"/>

      <!--每天凌晨开始,每30分钟拉取一次数据 -->

      <propertyname="cronExpression">

         <value>0 0-59/1 0-23 * * ?</value>

      </property>

   </bean>

   <!-- 定时器:启动定时器 -->

   <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean">

      <propertyname="triggers">

         <list>

            <!-- 这里可以设置多个要启动的定时器 -->

            <ref bean="getDataTrigger"/>

         </list>

      </property>

   </bean>

</beans>

web.xml配置文件

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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">

  <display-name></display-name>

 

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <!-- 配置Spring文件加载 -->

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