Spring boot中的定时任务(计划任务)
从Spring3.1开始,计划任务在Spring中实现变得异常的简单。首先通过配置类注解@EnableScheduling来开启对计划任务的支持,然后再要执行的计划任务的方法上注释@Scheduled,声明这是一个计划任务。
Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。
写个栗子来看看:
1)定时任务执行类:
package com.wj.test; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by wj on 2017/9/6.
*/
@Service
public class ScheduledTaskService {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); //每个2s执行一次任务
@Scheduled(fixedRate = 2000)
public void run(){
System.out.println(dateFormat.format(new Date()) + " | " + "每隔2s执行一次任务");
} // 每天15点29分执行该任务
@Scheduled(cron = "0 29 15 ? * *")
public void run1()
{
System.out.println(dateFormat.format(new Date()) + " | " + "每天在指定时间执行任务");
}
}
代码解释:
(1)通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行。
(2)使用cron属性可按照指定时间执行任务;cron是UNIX和Linux系统下的定时任务。
2)配置类:
package com.wj.test; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* Created by wj on 2017/9/6.
*/
@Configuration
@ComponentScan("com.wj.test")
@EnableScheduling
public class TaskSchedulerConfig { }
通过@EnableScheduling注解开启对计划任务的支持。
3)运行:
package com.wj.test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Created by wj on 2017/9/6.
*/
public class TestMain {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
}
}
结果如下图所示:

若在spring boot中使用@Scheduled定时任务,那么不需要写配置类,只需要在启动类application类中加入注解@EnableScheduling即可。
Spring boot中的定时任务(计划任务)的更多相关文章
- 【spring boot】spring boot中使用定时任务配置
spring boot中使用定时任务配置 =============================================================================== ...
- Spring Boot 中实现定时任务的两种方式
在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...
- Spring Boot中使用@Scheduled创建定时任务
我们在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. 创建定时任务 在Spring Boot中编写定时 ...
- 【定时任务】Spring Boot 中如何使用 Quartz
这篇文章将介绍如何在Spring Boot 中使用Quartz. 一.首先在 pom.xml 中添加 Quartz 依赖. <!-- quartz依赖 --> <dependency ...
- 如何在Spring Boot 中动态设定与执行定时任务
本篇文章的目的是记录并实现在Spring Boot中,动态设定与执行定时任务. 我的开发项目是 Maven 项目,所以首先需要在 pom.xml 文件中加入相关的依赖.依赖代码如下所示: <de ...
- 在Spring Boot中动态实现定时任务配置
原文路径:https://zhuanlan.zhihu.com/p/79644891 在日常的项目开发中,往往会涉及到一些需要做到定时执行的代码,例如自动将超过24小时的未付款的单改为取消状态,自动将 ...
- 【spring boot】使用定时任务@Scheduled 报错:Encountered invalid @Scheduled method 'dealShelf': Cron expression must consist of 6 fields (found 7 in "0 30 14 * * ? *")
在spring boot中使用使用定时任务@Scheduled 报错: org.springframework.beans.factory.BeanCreationException: Error c ...
- spring boot 基础篇 -- 定时任务
在日常项目中,常常会碰到定时监控项目中某个业务的变化,下面是spring boot 集成的定时任务具体配置: @Component public class IndexWarningScheduled ...
- Spring Boot中@Scheduled注解的使用方法
Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...
随机推荐
- [luoguP1586] 四方定理(DP 背包)
传送门 相当于背包, f[i][j] 表示当前数为 i,能分解成 j 个数的平方的和的数量 那么就是统计背包装物品的数量 ——代码 #include <cmath> #include &l ...
- Luogu P1257 平面上的最接近点对 暴力
这道题数据不大 两点距离用勾股定理求 #include<iostream> #include<cmath> using namespace std; struct node{ ...
- 轰炸II
题目背景 本题为轰炸数据加强版 题目描述 一个城市遭到了M次轰炸,每次都炸了一个每条边都与边界平行的矩形 在轰炸后,有N个关键点,指挥官想知道,它们有没有受到过轰炸,如果有,被炸了几次,最后一次是第几 ...
- 【BZOJ2081】Beads(哈希表)
题意: 翻转是指其中一段长度为k的子串全部翻转 n<=200000 a[i]<=n 思路:枚举k,直接哈希判充即可 时间复杂度是n/i求和,根据定理可得是O(n log n)级别的 单哈双 ...
- 测试出来了第一版代码--可以得到用户token啦
一版一版往前走啦... 先安装vs2010的学习版, 然后用codeblock来搞. 有一个msvcr100.dll这个文件需要和代码同级目录. 这样的好处是合规,然后,codeblock也可以用vs ...
- Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n
错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refre ...
- ArcGIS 10.1 for Server安装教程系列—— Linux下的单机安装
http://www.oschina.net/question/565065_81231 因为Linux具有稳定,功能强大等特性,因此常常被用来做为企业内部的服务器,我们的很多用户也是将Ar ...
- 错误代码: 1045 Access denied for user 'skyusers'@'%' (using password: YES)
1. 错误描写叙述 GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "."; 1 queries e ...
- C#邮件发送(含附件)
class SendEmail { static void Main(string[] args) { string from = "发件人@yingu.com"; string ...
- 【bzoj1015】[JSOI2008]星球大战starwar
给定一个无向图,求联通块个数,以及k次每次摧毁一个点后的联通块个数 将边和摧毁的点全记录下来,反着做即可 注意被摧毁的点不能算作联通块 #include<algorithm> #inclu ...