1. 配置说明

    <task:annotation-driven scheduler="xxxScheduler" />

    <task:scheduler id="xxxScheduler" pool-size="10" />

    <context:component-scan base-package="xxx.tasks" />

  2. 任务代码

    import org.springframework.scheduling.annotation.Scheduled;

    import org.springframework.stereotype.Component;

       
     

    @Component

    public class DemoTask {

       
     

        @Scheduled(cron = "0/5 * * * * ? ") // 每5秒执行一次

        public void doTask() {

            System.out.println("begin run ...");

            try {

                Thread.sleep(10000);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            System.out.println("done.");

        }

    }

  3. 测试代码

    import java.io.IOException;

    import java.util.Scanner;

       
     

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

       
     

    public class DemoTaskApp {

       
     

        public static void main(String[] args) throws IOException {

            ApplicationContext context = new ClassPathXmlApplicationContext("spring-test.xml");

            Scanner input = new Scanner(System.in);

              
     

            new Scanner(System.in).nextLine();

        }

       
     

    }

  4. CRON表达式说明

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。

    1 秒(0~59)

    2 分钟(0~59)

    3 小时(0~23)

    4 天(0~31)

    5 月(0~11)

    6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

    7.年份(1970-2099)

  5. CRON表达式实例

    0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

    0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时

    0 0 12 ? * WED 表示每个星期三中午12点

    "0 0 12 * * ?" 每天中午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 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发

    "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

    "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

    "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发

    "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发

    "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发

    "0 15 10 15 * ?" 每月15日上午10:15触发

    "0 15 10 L * ?" 每月最后一日的上午10:15触发

    "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发

    "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

    "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

Spring Task ABC的更多相关文章

  1. 使用Spring Task轻松完成定时任务

    一.背景 最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用Spring Task进行简单配置完成该需求,本文介绍Spring3.0以后自定义开发的定时任务工具, spring ta ...

  2. Spring task定时任务

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  3. spring task 配置

    Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下.    对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...

  4. spring+task配置

    1.spring+task文件配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  5. Quartz Spring与Spring Task总结

    Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下.    对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...

  6. spring定时任务轮询(spring Task)

    定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次. 以下用spring注解配置定时任务.1.添加相应的schema xmlns:task=" xsi:schemaLocati ...

  7. java Quartz定时器任务与Spring task定时的几种实现,

    java Quartz定时器任务与Spring task定时的几种实现 基于java 的定时任务实现, Quartz 时间详细配置    请查阅   http://www.cnblogs.com/si ...

  8. uartz Spring与Spring Task总结

    Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下.    对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...

  9. Spring 使用介绍(十二)—— Spring Task

    一.概述 1.jdk的线程池和任务调用器分别由ExecutorService.ScheduledExecutorService定义,继承关系如下: ThreadPoolExecutor:Executo ...

随机推荐

  1. About getByClass

    不能获取class为多个的情况 function getByClass(parent,cls){ var res=[]; var ele=parent.getElementsByTagName(&qu ...

  2. ntp服务器同步时间详细配置

    部署NTP服务器进行时间同步   NTP服务端:linl_S    IP:10.0.0.15 NTP客户端:lin_C    IP:10.0.0.16 NTP服务概述 1.原理 NTP(Network ...

  3. P1955 [NOI2015]程序自动分析[离散化+并查集]

    大水题一道,不明白为什么你谷评了个蓝.一看就是离散化,先去满足相等的条件,相等即为两点联通,或者说在同一个集合内.再看不相等,只有两元素在同一集合才不满足.裸的disjoint-set直接上,常数巨大 ...

  4. 【Lintcode】011.Search Range in Binary Search Tree

    题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...

  5. std::ostringstream 转std::string

    http://www.cplusplus.com/reference/sstream/ostringstream/ https://en.cppreference.com/w/cpp/io/basic ...

  6. sass安装方法,绝对好用的方式

    系统重做了,很多东西都重装,sass也一样,结果在安装的过程中遇到了问题,这里记录下,也给同样遇到问题的朋友们一个思路.本方法是参照http://www.w3cplus.com/sassguide/i ...

  7. ThinkPHP隐藏index.php出现No input file specified的解决方法

    因为在Fastcgi模式下,php不支持rewrite的目标网址的PATH_INFO的解析 ThinkPHP运行在URL_MODEL=2时,会出现 No input file specified.的情 ...

  8. linux多线程加解锁

    1.动态方式使用互斥量,该类型的互斥量在定义时不进行初始化,需要在使用之前初始化,使用结束销毁 1.1.定义一个锁变量:         pthread_mutex_t g_mutex_Msg; 1. ...

  9. 天梯赛L3-001. 凑零钱(01背包记录物品)

    L3-001. 凑零钱 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现 ...

  10. Apt本地源制作

    方法一 缓存deb包 # apt install XXX # mkdir -p /usr/local/mydebs # find /var/cache/apt/archives/ -name *.de ...