zoukankan      html  css  js  c++  java
  • Quartz的简单使用 luoye

    1:pom的坐标:

       <dependency>
             <groupId>org.quartz-scheduler</groupId>
             <artifactId>quartz</artifactId>
             <version>2.2.3</version>
        </dependency>
        <dependency>
              <groupId>org.quartz-scheduler</groupId>
              <artifactId>quartz-jobs</artifactId>
              <version>2.2.3</version>
       </dependency>

      注意:解决与shiro的冲突

    <dependency>
       <groupId>org.apache.shiro</groupId>
       <artifactId>shiro-quartz</artifactId>
       <version>${shiro.version}</version>
       <exclusions>
          <exclusion>
             <groupId>org.opensymphony.quartz</groupId>
             <artifactId>quartz</artifactId>
          </exclusion>
       </exclusions>
    </dependency>
    <dependency>
       <groupId>org.quartz-scheduler</groupId>
       <artifactId>quartz</artifactId>
    </dependency>

    2:配置文件的代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
        <!-- ==============================================spring定时器============================================= -->
        
        <!-- 每个定义的任务都要在这里进行引用才能运行 -->
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                              <ref local="MyTimerTrigger" />
                             <ref local="My2Trigger" />
                </list>
            </property>
        </bean>
    
        <!-- ===============任务一====================== -->
        <bean id="MyTimer" class="com.project.business.service.timer.MyTimer"/> 
        <!-- 引用,配置要运行的方法 -->
        <bean id="MyTimerDetail"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                <ref bean="MyTimer" />
            </property>
            <property name="concurrent" value="false" />
            <property name="targetMethod" value="execute" /><!--执行的方法-->
        </bean>
        <!-- 引用,定制调用间隔,具体时间配置的正则,请阅读timerReadme.txt -->
        <bean id="MyTimerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="MyTimerDetail" />
            </property>
            <property name="cronExpression">
                <!-- <value>0 30 * * * ?</value> -->
                <value>0 0/2 09 * * ? </value>
            </property>
        </bean>
    
        <!-- ==============任务二=========================  -->
        <bean id="My2Time" class="com.project.business.service.timer.My2Time">
            <property name="clsMap"> 
                <map>
                      <entry key="OrganizationService"><value></value></entry>
                      <entry key="PersonnelService"><value></value></entry>
                      <entry key="PlantService"><value></value></entry>
                      <entry key="FunctionLocationService"><value></value></entry>
                      <entry key="MasterEquipmentService"><value></value></entry>
                </map> 
            </property>
        </bean>
        <!-- 引用,配置要运行的方法 -->
        <bean id="My2Detail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                <ref bean="My2Time" />
            </property>
            <property name="concurrent" value="false" />
            <property name="targetMethod" value="execute" /><!--执行的方法-->
        </bean>
        <!-- 引用,定制调用间隔,具体时间配置的正则,请阅读timerReadme.txt -->
        <bean id="My2Trigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="My2Detail" />
            </property>
            <property name="cronExpression">
                <!-- <value>0 0/1 * * * ?</value> -->
                <!-- <value> 0 0 0 * * ?</value> -->
                <value>0 0/5 * * * ?</value>
            </property>
        </bean>
    </beans>

      在web.xml中的引用

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            classpath*:spring-mybatis.xml,
            classpath*:spring-cxf.xml,
            classpath*:timerConfig.xml
            </param-value>
        </context-param>

    3:需要执行的方法与代码:

    package com.mec.technology.basic.timetask.impl;
    
    public class Mytest {
    
        public void execute(){
            System.out.println("我要执行的代码");
        }
    }

      需要执行的方法2:

    @Service
    @SuppressWarnings("rawtypes")
    public class My2Time{
        private BaseDAO dao = null;
        
        private final List<Class> timerList = new ArrayList<Class>();
        
        private Map<String,String> clsMap = new HashMap<String, String>();
        
        private ApplicationContext context;
        
        public My2Time(){
    //        timerList.add(OrganizationService.class); //部门
    //        timerList.add(PersonnelService.class); //人员
    //        timerList.add(PlantService.class);  //工厂
    //        timerList.add(FunctionLocationService.class); //功能位置
    //        timerList.add(MasterEquipmentService.class);//设备主数据
    //        timerList.add(ShipInformationService.class);//船舶信息
        }
        
        /**
         * 定时器执行方法
         */
        public void execute() { 
    
            
        }
    }

     4:Quartz的Cron表达式

    1.        Seconds
    2.        Minutes
    3.        Hours
    4.        Day-of-Month
    5.        Month
    6.        Day-of-Week
    7.        Year (可选字段)
    
    例  "0 0 12 ? * WED" 在每星期三下午12:00 执行,
    
    个别子表达式可以包含范围, 例如,在前面的例子里("WED")可以替换成 "MON-FRI", "MON, WED, FRI"甚至"MON-WED,SAT".
    
    “*” 代表整个时间段.
    
    每一个字段都有一套可以指定有效值,如
    
    Seconds (秒)         :可以用数字0-59 表示,
    
    Minutes(分)          :可以用数字0-59 表示,
    
    Hours(时)             :可以用数字0-23表示,
    
    Day-of-Month(天) :可以用数字1-31 中的任一一个值,但要注意一些特别的月份
    
    Month(月)            :可以用0-11 或用字符串  “JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC” 表示
    
    Day-of-Week(每周):可以用数字1-7表示(1 = 星期日)或用字符口串“SUN, MON, TUE, WED, THU, FRI and SAT”表示
    
    “/”:为特别单位,表示为“每”如“0/15”表示每隔15分钟执行一次,“0”表示为从“0”分开始, “3/20”表示表示每隔20分钟执行一次,“3”表示从第3分钟开始执行
    
    “?”:表示每月的某一天,或第周的某一天
    
    “L”:用于每月,或每周,表示为每月的最后一天,或每个月的最后星期几如“6L”表示“每月的最后一个星期五”
    
    “W”:表示为最近工作日,如“15W”放在每月(day-of-month)字段上表示为“到本月15日最近的工作日”
    
    ““#”:是用来指定“的”每月第n个工作日,例 在每周(day-of-week)这个字段中内容为"6#3" or "FRI#3" 则表示“每月第三个星期五”
    
    
    1)Cron表达式的格式:秒 分 时 日 月 周 年(可选)。
    
                   字段名                 允许的值                        允许的特殊字符  
                   秒                         0-59                               , - * /0-59                               , - * /  
                   小时                     0-23                               , - * /1-31                               , - * ? / L W C  
                   月                         1-12 or JAN-DEC         , - * /  
                   周几                     1-7 or SUN-SAT           , - * ? / L C #  
                   年 (可选字段)     empty, 1970-2099      , - * /?”字符:表示不确定的值
    
                   “,”字符:指定数个值
    
                   “-”字符:指定一个值的范围
    
                   “/”字符:指定一个值的增加幅度。n/m表示从n开始,每次增加m
    
                   “L”字符:用在日表示一个月中的最后一天,用在周表示该月最后一个星期X
    
                   “W”字符:指定离给定日期最近的工作日(周一到周五)
    
                   “#”字符:表示该月第几个周X。6#3表示该月第3个周五
    
             2)Cron表达式范例:
    
                     每隔5秒执行一次:*/5 * * * * ?
    
                     每隔1分钟执行一次:0 */1 * * * ?
    
                     每天23点执行一次:0 0 23 * * ?
    
                     每天凌晨1点执行一次:0 0 1 * * ?
    
                     每月1号凌晨1点执行一次:0 0 1 1 * ?
    
                     每月最后一天23点执行一次:0 0 23 L * ?
    
                     每周星期天凌晨1点实行一次:0 0 1 ? * L
    
                     在26分、29分、33分执行一次:0 26,29,33 * * * ?
    
                     每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
  • 相关阅读:
    java后台设计简单的json数据接口,设置可跨域访问,前端ajax获取json数据
    java 多线程研究:锁的概念
    PHP 学习笔记之一:thinkPHP的volist标签
    Flask 学习篇二:学习Flask过程中的记录
    Docker 学习应用篇之三: Docker的简单实用
    CodeForces 832B Petya and Exam
    CodeForce 832A Sasha and Sticks
    Docker 学习应用篇之二: Docker的介绍和安装
    ASP.NET中AJAX的异步加载(Demo演示)
    Java实现 蓝桥杯VIP 算法训练 连通分块(并查集)
  • 原文地址:https://www.cnblogs.com/luoyetl/p/10224959.html
Copyright © 2011-2022 走看看