Spring @SCHEDULED(CRON = "0 0 * * * ?")实现定时任务
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 * * * ?")实现定时任务的更多相关文章
- Spring scheduled cron 表达式
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天( ...
- 使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...
- Spring @Scheduled执行原理解析
项目使用很多@Scheduled(cron=**) 注解来实现定时任务,既然要用就必须弄清楚的它的实现原理,于是乎翻了一下相关的源码. Spring 3.0之后增加了调度器功能,提供的@Schedul ...
- Spring的定时任务@Scheduled(cron = "0 0 1 * * *")
指定某个方法在特定时间执行,如: cron="0 0 1 1 * ?" 即这个方法每月1号凌晨1点执行一次 关于这个注解的解释网上一大堆 但是今天遇到个问题,明明加了注解@Sche ...
- @Scheduled(cron = "0 0 * * * ?")实现定时任务
//每一个小时执行一次 @Scheduled(cron = "0 0 * * * ?") public void saveDailyScoreScheduled() { try { ...
- Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 任务调度动态改变时间
不需要重启应用就可以动态的改变Cron表达式的值 import java.util.Date; import java.util.concurrent.Executor; import java.ut ...
- Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 动态改变时间
import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.Executors; ...
- spring注解 @Scheduled(cron = "0 0 1 * * *")实现定时的执行任务
@Scheduled(cron = "0 0 1 * * *") 在使用该注解以前请做好以下准备工作,配置好相应的xm文件. 配置定时注解的步骤:http://blog.csdn. ...
- 摆脱Spring 定时任务的@Scheduled cron表达式的困扰
一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...
随机推荐
- django-form.errors和前端上传文件
一.上传文件: 在相应的模型里面定义`FileField`或者是`ImageField`类型的字段,并且1.设置好`upload_to`参数来指定上传的路径. class User(models.Mo ...
- DBNavigator中把insert变为append
procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);begin if Button = nbIns ...
- 3 MySQL数据库--初识sql语句
1.初识sql语句 服务端软件 mysqld SQL语句:后面的分号mysql -uroot -p123 操作文件夹(库) 增 create database db1 charset utf8; 查 ...
- vs启动项目提示Web 服务器被配置为不列出此目录的内容。
解决方法 确认网站或应用程序配置文件中的 configuration/system.webServer/directoryBrowse@enabled 属性已设置为 true. 配置一下web.con ...
- dubbo 提供者 ip不对
1.服务器多网卡绑定,导致服务起来后程序自己选择的ip不对. 2.提供服务的机器开启了vpn. 3.dubbo配置文件中写死了host. 以下为转载:转自http://www.ithao123.cn/ ...
- Liunx下如何使用kettle
在windows下完成所有操作, 把xxx.ktr上传到liunx, Pan.sh xxx.ktr 就完成了
- CentOS错误
centos下yum lock的解决办法 Another app is currently holding the yum lock; waiting for it to exit... 解决办法: ...
- 74款安卓和IOS app源码地址
知乎专栏App https://github.com/bxbxbai/ZhuanLan WeChat高仿微信 项目地址: https://github.com/motianhuo/wechat Gan ...
- Java程序设计19——类的加载和反射-Part-B
接下来可以随意提供一个简单的主类,该主类无须编译就可使用上面的CompileClassLoader来运行它. package chapter18; public class Hello { publi ...
- C++中static修饰的静态成员函数、静态数据成员
1.静态成员函数.静态数据成员 在类中,用static修饰的成员函数被称为静态成员函数,而用static修饰的数据成员就称为静态数据成员:与普通的成员函数和数据成员不同, 静态成员函数和静态数据成员有 ...