您的位置:首页 > 编程语言 > Java开发

spring中配置quartz定时器

2016-07-21 18:37 666 查看

spring中配置quartz定时器

jar包说明

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



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

实体类的方法

定时器就来调用这个方法

package com.cloud.impl;
public
class
GetDataImpl {
   public
void

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

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: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">
   <!--
配置一个定时器 -->
   <bean
id="getDataImpl"
class="com.cloud.impl.GetDataImpl"></bean>
   <!--
定时器:获取数据第一步 -->
   <bean
id="getDataQuart"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <!--
配置获取数据的service -->
      <property
name="targetObject"
ref="getDataImpl"/>
      <!--
配置执行的method -->
      <property
name="targetMethod"
value="getData"/>
      <!--
设置作业不并发调度 -->
      <property
name="concurrent"
value="false"
/>
   </bean>
   <!--
定时器:设置定时启动时间 -->
   <bean
id="getDataTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property
name="jobDetail"
ref="getDataQuart"/>
      <!--
每天凌晨开始,每30分钟拉取一次数据 -->
      <property
name="cronExpression">
         <value>0 0-59/1 0-23 * * ?</value>
      </property>
   </bean>
   <!--
定时器:启动定时器 -->
   <bean
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property
name="triggers">
         <list>
           
<!-- 这里可以设置多个要启动的定时器 -->
           
<ref bean="getDataTrigger"/>
         </list>
      </property>
   </bean>
</beans>

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: