springboot(八)-定时任务
在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容。
如果我们不用springboot开发的话,我们写定时任务需要写那些配置呢?
我们需要在application.xml文件中添加以下配置:
1.在<beans .. />中添加
xmlns:tx="http://www.springframework.org/schema/tx"
还有xsi:schemaLocation =“...”中添加
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd
这就算引进task任务的功能了,接着,我们要开启task任务,配置
<task:annotation-driven />
然后在使用定时任务的类名上面添加注解@Component交由spring来管理,对不对。
最后在方法名上面添加注解@Scheduled(cron="*/6 * * * * ?") 或者@Scheduled(fixedRate = 6000),这样基本就完成了。
这样做并不算很复杂和繁琐。
那现在用springboot开发,我们没有xml配置文件了。我们怎么做?
springboot默认已经帮我们实现了xml文件中的一套配置,只需要添加相应的注解就可以实现。
pom.xml
首先在pom里面添加包含定时任务的依赖包。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
application.java
在启动类上面加上@EnableScheduling
即可开启定时。
@SpringBootApplication
@EnableScheduling
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建定时任务实现类
@Component
public class SchedulerTask { private int count=0; @Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("this is scheduler task runing "+(count++));
} }
或者
@Component
public class Scheduler2Task { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 6000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
} }
参数说明
@Scheduled
参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?"
,一种是 fixedRate = 6000
,两种都表示每隔六秒打印一下内容。
fixedRate 说明
@Scheduled(fixedRate = 6000)
:上一次开始执行时间点之后6秒再执行@Scheduled(fixedDelay = 6000)
:上一次执行完毕时间点之后6秒再执行@Scheduled(initialDelay=1000, fixedRate=6000)
:第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
springboot(八)-定时任务的更多相关文章
- 玩转SpringBoot之定时任务详解
序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- SpringBoot 配置定时任务
SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...
- SpringBoot - 添加定时任务
SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...
- springboot之定时任务
定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...
- SpringBoot整合定时任务和异步任务处理 3节课
1.SpringBoot定时任务schedule讲解 定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- 十三、springboot集成定时任务(Scheduling Tasks)
定时任务(Scheduling Tasks) 在springboot创建定时任务比较简单,只需2步: 1.在程序的入口加上@EnableScheduling注解. 2.在定时方法上加@Schedule ...
- SpringBoot创建定时任务
之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单. (1 ...
- springboot开启定时任务 添加定时任务 推送
最近在自学Java的springboot框架,要用到定时推送消息.参考了网上的教程,自己调试,终于调好了.下面将网上的教程归纳下,总结复习下. springboot开启定时任务 在SpringBo ...
- (入门SpringBoot)SpringBoot结合定时任务task(十)
SpringBoot整合定时任务task 使用注解EnableScheduling在启动类上. 定义@Component作为组件被容器扫描. 表达式生成地址:http://cron.qqe2.com ...
随机推荐
- 34-n的pi次方
链接:https://www.nowcoder.com/acm/contest/118/B来源:牛客网 题目描述 喜爱ACM的PBY同学遇到了一道数学难题,已知底数n,请你帮他准确的计算出结果a = ...
- Mybatis中resultType和resultMap
一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...
- Django--登录实例
1.准备工作 创建必要的目录和文件,导入js,css,bootstrap等,目录结构如下: 2.配置文件添加static路径 settings.py 1 2 3 4 STATIC_URL = '/st ...
- Luogu 4284 [SHOI2014]概率充电器
BZOJ 3566 树形$dp$ + 概率期望. 每一个点的贡献都是$1$,在本题中期望就等于概率. 发现每一个点要通电会在下面三件事中至少发生一件: 1.它自己通电了. 2.它的父亲给它通电了. 3 ...
- Django框架 之 querySet详解
Django框架 之 querySet详解 浏览目录 可切片 可迭代 惰性查询 缓存机制 exists()与iterator()方法 QuerySet 可切片 使用Python 的切片语法来限制查询集 ...
- elasticsearch plugin
bin/plugin -install de.spinscale/elasticsearch-plugin-suggest/0.90.5-0.9 plugin -install mobz/elasti ...
- angular学习文章
https://www.jianshu.com/p/86c6249a2069 angular.cn https://segmentfault.com/a/1190000008754631
- Windows中与系统关联自己开发的程序(默认打开方式、图标、右击菜单等)
1. 默认打开方式 1.1. 代码支持 在Windows下,某个特定后缀名类型的文件,如果要双击时默认用某个程序(比如自己开发的WinForm程序)打开,代码中首先肯定要支持直接根据这个文件进行下一步 ...
- webrowser卡死解决方案
webrowser 是由于有道词典造成 解决方案,关闭有道或卸载:
- tomcat 安装与使用!
$安装:安装方式为zip解压. 打开tomcat官网点此链接:https://tomcat.apache.org/download-80.cgi 选择你想要使用的版本,点击相应位数选择zip解压包版, ...