zoukankan      html  css  js  c++  java
  • spring

     使用Spring的@Scheduled实现定时任务

    <?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:task="http://www.springframework.org/schema/task"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.platform.freight"/>
    <task:annotation-driven/>
    </beans>


    Spring配置文件xmlns加入
    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.0.xsd"

    Spring扫描注解的配置
    <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" />



    代码实现
    @Scheduled(fixedRate = 5000)
    public void doSomething() {
        // something that should execute periodically
    }


    如果简单的定期调度不能满足,那么cron表达式提供了可能
    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。
    其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.


    @Component
    public class TaskDemo {
        
        @Scheduled(cron = "0 0 2 * * ?")  //每天凌晨两点执行
            void doSomethingWith(){
                logger.info("定时任务开始......");
                long begin = System.currentTimeMillis();
            
                //执行数据库操作了哦...
            
                long end = System.currentTimeMillis();
                logger.info("定时任务结束,共耗时:[" + (end-begin) / 1000 + "]秒");
        }
    }


    =====================
    package com.platform.freight.task.run;

    import com.platform.freight.common.interfaces.sys.JobService;
    import com.platform.freight.common.vo.BasisVo;
    import com.platform.util.LogUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;

    /**
     * Created by ASUS on 2017/10/12.
     */
    @Component
    public class DoTaskRun {

        @Autowired
        private JobService jobService;

        @Scheduled(cron = "0 0/1 * * * ?")
        public void runTaskJob(){
            LogUtils.info (">>>>>>>>>>定时计划开始<<<<<<<<<<<<");
            BasisVo basisVo=jobService.doRunTask();
            LogUtils.info

    (">>>>>>>>>>定时计划结束,返回信息:"+basisVo.getResultMsg()+"<<<<<<<<<<<<");
        }

    }

  • 相关阅读:
    cegui 编译过程详解(cegui-0.8.2)
    ogre3D学习基础17 --- 如何手动创建ogre程序
    ogre3D学习基础16 -- 手动创建实体(ManualObject)
    ogre3D学习基础15 -- 创建BSP Scene Manager
    ogre3D,cegui配置问题
    ogre3D学习基础14 -- 雾化效果与天空面,天空盒,天空穹
    ogre3D学习基础13 -- 键盘控制网格动画mesh
    ogre3D学习基础12 --- 让机器人动起来(移动模型动画)
    引用其他头文件时出现这种错误,莫名其妙,error C2065: “ColorMatrix”: 未声明的标识符
    STL学习笔记3--deque
  • 原文地址:https://www.cnblogs.com/createyuan/p/7736287.html
Copyright © 2011-2022 走看看