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 可以作为一个触发源添加到一个方法中,例如,以下的方法将以一个固定延迟时间5秒钟调用一次执行,这个周期是以上一个调用任务的完成时间为基准,在上一个任务完成之后,5s后再次执行:

@Scheduled(fixedDelay = 5000)
public void doSomething() {
// something that should execute periodically
}

如果需要以固定速率执行,只要将注解中指定的属性名称改成fixedRate即可,以下方法将以一个固定速率5s来调用一次执行,这个周期是以上一个任务开始时间为基准,从上一任务开始执行后5s再次调用:

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

如果简单的定期调度不能满足,那么cron表达式提供了可能

package com.imwoniu.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @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 + "]秒");
}
}

  

按顺序依次为

秒(0~59)

分钟(0~59)

小时(0~23)

天(月)(0~31,但是你需要考虑你月的天数)

月(0~11)

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

7.年份(1970-2099)

Spring @SCHEDULED(CRON = "0 0 * * * ?")实现定时任务的更多相关文章

  1. Spring scheduled cron 表达式

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天( ...

  2. 使用轻量级Spring @Scheduled注解执行定时任务

    WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...

  3. Spring @Scheduled执行原理解析

    项目使用很多@Scheduled(cron=**) 注解来实现定时任务,既然要用就必须弄清楚的它的实现原理,于是乎翻了一下相关的源码. Spring 3.0之后增加了调度器功能,提供的@Schedul ...

  4. Spring的定时任务@Scheduled(cron = "0 0 1 * * *")

    指定某个方法在特定时间执行,如: cron="0 0 1 1 * ?" 即这个方法每月1号凌晨1点执行一次 关于这个注解的解释网上一大堆 但是今天遇到个问题,明明加了注解@Sche ...

  5. @Scheduled(cron = "0 0 * * * ?")实现定时任务

    //每一个小时执行一次 @Scheduled(cron = "0 0 * * * ?") public void saveDailyScoreScheduled() { try { ...

  6. Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 任务调度动态改变时间

    不需要重启应用就可以动态的改变Cron表达式的值 import java.util.Date; import java.util.concurrent.Executor; import java.ut ...

  7. Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 动态改变时间

    import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.Executors; ...

  8. spring注解 @Scheduled(cron = "0 0 1 * * *")实现定时的执行任务

    @Scheduled(cron = "0 0 1 * * *") 在使用该注解以前请做好以下准备工作,配置好相应的xm文件. 配置定时注解的步骤:http://blog.csdn. ...

  9. 摆脱Spring 定时任务的@Scheduled cron表达式的困扰

    一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...

随机推荐

  1. PowerDesigner软件的使用

    1. 报错:Could not Initialize JavaVM 的解决方案: powerDesigner不支持x64JDK,ok.安装32位. 仅仅是安装一下,不要做任何配置.......关闭po ...

  2. logger 的使用一 小结

    方式一 依赖: <!-- log start --> <dependency> <groupId>log4j</groupId> <artifac ...

  3. 27.OGNL与ValueStack(VS)-获取Stack Context中的信息

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 我们知道,除了可以从值栈中获取信息,还可以从Stack Context中获取 ...

  4. codeforces:Michael and Charging Stations分析和实现

    题目大意 迈克尔接下来n天里分别需要支付C[1], C[2], ... , C[n]费用,但是每次支付费用可以选择使用优惠或不使用优惠,每次使用价值X的优惠那么迈克尔所能使用的优惠余量将减少X并且当天 ...

  5. codeforces:Prefix Sums分析和实现

    题目大意: 给出一个函数P,P接受一个数组A作为参数,并返回一个新的数组B,且B.length = A.length + 1,B[i] = SUM(A[0], ..., A[i]).有一个无穷数组序列 ...

  6. 106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  7. PHP里的进制

    1.进制转换函数: <?php function decto_bin($datalist,$bin) { static $arr=array(0,1,2,3,4,5,6,7,8,9,'A','B ...

  8. [C#] Delegate, Multicase delegate, Event

    声明:这篇博客翻译自:https://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in- ...

  9. 479. Largest Palindrome Product

    class Solution { public: int largestPalindrome(int n) { vector<,,,,,,,}; ]; } }; 这里的穷举法,你不得不服

  10. BZOJ 1001 狼抓兔子 (最小割转化成最短路)

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 27715  Solved: 7134[Submit][ ...