zoukankan      html  css  js  c++  java
  • springmvc+quartz简单实现定时调度

    一、简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.3.0。

    二、因为定时调度,在很多业务上面都会涉及,想要根据自己的规则来生成自己想要的达到的目的。所以利用quartz来时间定时任务的触发。是非常有必要的。

    三、springmvc下需要的jar包

       <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.quartz-scheduler</groupId>
          <artifactId>quartz</artifactId>
          <version>2.2.3</version>
        </dependency>

    说明:quartz是基础包。spring-context-support包是调度设置的依赖包。spring-context可以不用添加。

    四、spring-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:context="http://www.springframework.org/schema/context" 
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
           
        <!-- 加入需要执行的类 -->
        <bean id="timingSchedule" class="com.troy.jpa.schedule.TimingSchedule"/>
        <!-- 加入定时执行的方法 -->
        <bean id="timingScheduleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 定时执行的类 -->
            <property name="targetObject" ref="timingSchedule"/>
            <!-- 具体的方法 -->
            <property name="targetMethod" value="execute"/>
        </bean>
        <!-- 调度触发器,设置自己想要的时间规则 -->
        <bean id="timingScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <!-- 加入相关的执行类和方法 -->
            <property name="jobDetail" ref="timingScheduleJobDetail"/>
            <!-- 设置时间规则 (为了方便测试,设置成一分钟一次。具体的规则见详情)-->
            <property name="cronExpression" value="00 * * * * ?"/>    
        </bean>
        <!-- 加入调度工厂 ,设置调度触发器即可-->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="timingScheduleTrigger"/>
                </list>
            </property>
        </bean>
    </beans>

    五、需要执行的类和方法

    package com.troy.jpa.schedule;
    
    import java.util.Date;
    
    public class TimingSchedule {
        
        //定时执行的方法
        public void execute(){
            System.out.println("执行时间"+ new Date());
        }
    }

    六、执行效果

    执行时间Wed Jul 12 21:01:00 CST 2017
    执行时间Wed Jul 12 21:02:00 CST 2017
    执行时间Wed Jul 12 21:03:00 CST 2017
    执行时间Wed Jul 12 21:04:00 CST 2017

    七、时间设置规则

    1  秒  是  0-59    , - * / 
    2  分  是  0-59   , - * / 
    3 小时  是  0-23   , - * / 
    4  日  是  1-31   , - * ? / L W 
    5  月  是  1-12 or JAN-DEC   , - * / 
    6  周  是  1-7 or SUN-SAT   , - * ? / L # 
    7  年  否  empty 或 1970-2099  , - * / 
  • 相关阅读:
    使用java.util.Properties类读写配置文件
    maven配置文件setting.xml字段注释
    使用Nexus搭建Maven代理仓库
    Memcached 内存管理详解
    Memcached常用命令及使用说明
    使用NTP协议服务器时间同步
    Eclipse打JAR包的使用
    Eclipse插件的各种安装方法
    Java中代理对象的使用小结
    tp5框架成功、失败提示模板修改
  • 原文地址:https://www.cnblogs.com/ll409546297/p/7157702.html
Copyright © 2011-2022 走看看