zoukankan      html  css  js  c++  java
  • spring定时任务

    spring定时任务(Quartz

    ----applicationContext.xml----

    <!-- 定时任务,关闭过期课程 -->
    <bean id="springBeanUtils" class="com.cdeledu.plat.signup.common.SpringBeanUtils"/>
    <bean id="job2"  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
        <property name="targetObject"> 
           <bean class="com.jxjyflat.course.action.CourseOperateAction" /> 
        </property> 
        <property name="targetMethod" value="closeDueCourse" /> 
        <property name="concurrent" value="false" /><!-- 作业不并发调度 --> 
    </bean> 
    <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
        <property name="jobDetail" ref="job2" /> 
        <!--每天02:00运行一次 --> 
        <property name="cronExpression" value="00 02 00 * * ?" /> 
    </bean> 
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
        <property name="triggers"> 
           <list> 
               <ref bean="cronTrigger2" /> 
           </list> 
        </property> 
    </bean> 

    ----访问过滤器---- 

    if (localStringBuffer.toString().equals("/course/operate.do")){
        String op = RequestHandler.getString(request, "op");
        if("closeDueCourse".equals(op)){
           bool = true;
           paramFilterChain.doFilter(paramServletRequest, paramServletResponse);
           return;
        }
    }

    ----action方法----

    public void closeDueCourse(){
        try{
           //获取facade
           courseFacade = (CourseFacade)SpringBeanUtils.getInstance().getBean("courseFacade");
           int dueCourseCount = courseFacade.getDueCourseCount();
            if(dueCourseCount > 0){
                courseFacade.closeDueCourse();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    知识点:

    每隔指定时间则触发一次,在Quartz中对应的触发器为:org.springframework.scheduling.quartz.SimpleTriggerBean

    每到指定时间则触发一次,在Quartz中对应的调度器为:org.springframework.scheduling.quartz.CronTriggerBean

    (java.util.TimerTask任务就只能使用第一种。Quartz、spring task支持两种)

    字段

    允许值

    允许的特殊字符

    0-59

    -*/

    0-59

    -*/

    小时

    0-23

    - * /

    日期

    1-31

    - * ? / L W C

    月份

    1-12 或者 JAN-DEC

    - * /

    星期

    1-7 或者 SUN-SAT

    - * ? / L C #

    年(可选)

    留空, 1970-2099

    - * /

    -

    区间

    *

    通配符

    ?

    你不想设置那个字段

    “*”

    字符代表所有可能的值

    “/”

    字符用来指定数值的增量

                 

    示例:

    "0 0 12 * * ?"                每天中午十二点触发 

    "0 15 10 ? * *"                 每天10:15触发 

    "0 15 10 * * ?"                 每天10:15触发 

    "0 15 10 * * ? *"            每天10:15触发 

    "0 15 10 * * ? 2005"      2005年的每天10:15触发 

    "0 * 14 * * ?"                每天14-14:59:每分钟一次触发 

    "0 0/5 14 * * ?"            每天14-14:55每5分钟一次触发 

    "0 0/5 14,18 * * ?"       每天14-14:55、18-18:55:内,每5分钟一次触发 

    "0 0-5 14 * * ?"            每天14:00至14:05每分钟一次触发 

    "0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 

    "0 15 10 ? * MON-FRI"  周一、周二、周三、周四、周五10:15触发 

    spring定时任务(@Scheduled)

    ----applactionContext.xml(spring配置文件)----

    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    ----springmvc-servlet.xml---- 

    //任务扫描注解
    <context:component-scan base-package="com.imwoniu.*" />
    <task:executor id="executor" pool-size="5" />  
    <task:scheduler id="scheduler" pool-size="10" />  
    <task:annotation-driven executor="executor" scheduler="scheduler" />

    ----action代码----

    package com.imwoniu.task;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    @Component
    public class TaskDemo {
        //@Scheduled(fixedRate = 5000)       //每隔5秒执行
        @Scheduled(cron = "0 0 2 * * ?")  //每天凌晨两点执行
            void doSomethingWith(){
                logger.info("定时任务开始......");
                long begin = System.currentTimeMillis();
                //执行数据库操作
                long end = System.currentTimeMillis();
                logger.info("任务耗时:[" + (end-begin) / 1000 + "]秒");
        }
    }




  • 相关阅读:
    mysql面试题
    Zookeeper与Kafka基础概念和原理
    Docker资源限制
    企业级仓库harbor搭建
    基于容器制作镜像
    docker基础学习(一)
    docker往阿里云推镜像和打包镜像
    Dockfile制作镜像
    算法Sedgewick第四版-第1章基础-006一封装输出(文件)
    算法Sedgewick第四版-第1章基础-005一封装输入(可以文件,jar包里的文件或网址)
  • 原文地址:https://www.cnblogs.com/whatarewords/p/10723795.html
Copyright © 2011-2022 走看看