zoukankan      html  css  js  c++  java
  • spring定时器配置

    在此记录两种定时任务的配置:

    一种是quart定时器:

      <1>配置xml文件(定时任务配置)

      <!--定时任务 -->
        <bean id="txfwBomc" class="shmc.framework.scheduling.JobDetailFactoryBean">
            <property name="jobDataAsMap">
                <map>
                    <entry key="targetObject" value="bomcManager"/>  <!-- 定时任务所在类 -->
                    <entry key="targetMethod" value="bomcDataPropel"/>  <!-- 定时任务实现方法 -->
                </map>
            </property>
            <property name="concurrent" value="false" />
        </bean>
        
        <!--定义时间间隔触发器 -->
         <bean id="bomcTigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
          <property name="jobDetail" ref="txfwBomc"/>
          <property name="cronExpression" value="0 0 1 * * ?" />
        </bean> 

       <2>定义实现类的bean

    <bean id="bomcManager" class="com.test.service.BomcManager" parent="frameworkManager">
    </bean>

       <3>启动定时任务

      <!-- 集群定时器调度工厂  -->
        <bean id="clusterSchedule" class="shmc.framework.scheduling.SchedulerFactoryBean">
            <property name="applicationContextSchedulerContextKey">
                <value>applicationContext</value>
            </property>
            <property name="triggers">
                <list>
             <!-- BOMC推送 -->
                   <ref bean="bomcTigger"/>
                </list>
            </property>
        </bean>

      <4>业务层实现代码

      ………………

      

    一种利用注解:

      <1>配置xml文件,需要引用spring-task-3.1.xsd文档。

    <beans 
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="
            http://www.springframework.org/schema/task     //名称空间的名字
            http://www.springframework.org/schema/task/spring-task-3.1.xsd">  //模式文档的位置
        
        <!-- 扫描包路径 -->
        <context:component-scan base-package="com.src.xx" >
        </context:component-scan>
        <!--  开启定时器-->
        <task:annotation-driven/>
        
    </beans> 

    <注意:>上边开启定时任务时,没有设置线程池。而spring默认是单线程,多个任务执行起来时间会有问题(B任务会因为A任务执行起来需要20S而被延后20S执行)

    所以要配置线程池如下:

        <!--  开启定时器--> 
        <task:annotation-driven scheduler="taskScheduler"/>

      <!-- 配置定时任务的线程池 --> <task:scheduler id="taskScheduler" pool-size="5"/>

    <2>java代码,具体实现在业务层实现

    package com.src.xx.controller.api.timer;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import com.src.xx.service.timer.XianshiTimerService;
    
    
    @Component("taskXianshi")
    public class XianshiTimer {
        
        @Autowired
        private XianshiTimerService xianshiTimerService;
        
        /**
         * 定时任务
         */
        @Scheduled(cron = "0 26 10 * * ?") 
        public void xsActivityRemind(){
            xianshiTimerService.xsActivityRemind();
        }
           
    }

    ● spring定时任务cronExpression时间:<注意:每个时间点中间必须有空格分隔>
      一个cronExpression的表达式从左到右定义:
      秒(0-59)
      分钟(0-59)
      小时(0-23)
      月份中的日期(1-31)
      月份(1-12或JAN-DEC)
      星期中的日期(1-7或SUN-SAT)
      年份(1970-2099)

  • 相关阅读:
    ThinkPHP5跨控制器调用
    ThinkPHP5显示数据库字段内容
    使用ThinkPHP5连接数据库
    详解shell中>/dev/null 2>&1到底是什么
    算法
    设计模式简介
    python面试题
    python-基础介绍
    python第二天-linux权限管理
    Python全栈考试-部分试题(精选)
  • 原文地址:https://www.cnblogs.com/doufuquanjia/p/5810604.html
Copyright © 2011-2022 走看看