zoukankan      html  css  js  c++  java
  • SpringQtz 时间任务调度

    1、配置所需要maven jar包

    <!-- 任务调度需要的jar包-->
    <dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>1.8.5</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.2.4.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>3.2.4.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>3.2.4.RELEASE</version>
    </dependency>
    <!-- 任务调度需要的jar包-->

    2、配置文件spring-timer.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <!-- 配置调度程序quartz ,其中配置JobDetail有两种方式-->
    <!--方式一:使用JobDetailBean,任务类必须实现Job接口 -->
    <!-- <bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean"> -->
    <!-- <property name="name" value="exampleJob"></property> -->
    <!-- <property name="jobClass" value="com.zbb.cn.filter.SpringQtz"></property> -->
    <!-- <property name="jobDataAsMap"> -->
    <!-- <map> -->
    <!-- <entry key="service"> -->
    <!-- <value>simple is the beat</value> -->
    <!-- </entry> -->
    <!-- </map> -->
    <!-- </property> -->
    <!-- </bean> -->
    <!--运行时请将方式一注释掉! -->
    <!--
    方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法
    -->
    <!-- 定义目标bean和bean中的方法 -->
    <bean id="SpringQtzJob" class="com.cc.hkjc.controller.backstage.interceptor.SpringQtz">

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

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

    <!-- ======================== 调度工厂 ======================== -->
    <bean id="SpringJobSchedulerFactoryBean"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
    <list>
    <ref bean="CronTriggerBean" />
    </list>
    </property>
    </bean>

    </beans>

    package com.cc.hkjc.controller.backstage.interceptor;

    import java.util.Calendar;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import javax.annotation.Resource;

    import org.apache.cxf.common.util.StringUtils;

    import com.cc.hkjc.domain.HcNum;
    import com.cc.hkjc.domain.Zstj;
    import com.cc.hkjc.service.PhoneService;
    import com.cc.hkjc.service.ZstjService;

    public class SpringQtz {
    private static int count=0;
    @Resource
    private ZstjService zstjService;

    @Resource
    private PhoneService phoneService;

    public void execute() {
      
    }
    }

    4、在web.xml配置spring-timer.xml

  • 相关阅读:
    暑假学习笔记(一)——初识Neo4j和APICloud入门
    置信规则库学习记录——1
    博客整理——宣传文案
    博客整理——软件工程实践总结
    博客整理——Alpha版冲刺
    博客整理——事后诸葛亮
    面试被企业拒绝后还有必要再申请吗?
    MapReduce分布式算法
    水题系列二:PhoneNumbers
    水题系列一:Circle
  • 原文地址:https://www.cnblogs.com/swite/p/5734271.html
Copyright © 2011-2022 走看看