spring-Scheduler
作者:纯洁的微笑
出处:http://www.ityouknow.com/
版权所有,欢迎保留原文链接进行转载:)
在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现
1、pom包配置
pom包里面只需要引入springboot starter包即可
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2、启动类启用定时
在启动类上面加上@EnableScheduling即可开启定时
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3、创建定时任务实现类
定时任务1:
@Component
public class SchedulerTask {
private int count=0;
@Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("this is scheduler task runing "+(count++));
}
}
定时任务2:
@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()));
}
}
结果如下:
this is scheduler task runing 0
现在时间:09:44:17
this is scheduler task runing 1
现在时间:09:44:23
this is scheduler task runing 2
现在时间:09:44:29
this is scheduler task runing 3
现在时间:09:44:35
参数说明
@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。
fixedRate 说明
@Scheduled(fixedRate = 6000):上一次开始执行时间点之后6秒再执行@Scheduled(fixedDelay = 6000):上一次执行完毕时间点之后6秒再执行@Scheduled(initialDelay=1000, fixedRate=6000):第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
spring-Scheduler的更多相关文章
- Spring Scheduler定时任务 + Quartz
原文地址: https://blog.csdn.net/revitalizing/article/details/61420556 版权声明:本文为博主原创文章,未经博主允许不得转载. https:/ ...
- Spring @Scheduler使用cron时的执行问题
主要想弄清使用Spring @Scheduler cron表达式时的两个问题: 同一定时任务,第二次触发时间到了,第一次还没有执行完成时会执行吗? 不同的定时任务,相互之间是否有影响? 结论写在前面: ...
- spring scheduler相同时间内执行两次的问题
在网上找了大片文章,有的说是上下文被夹在两次的问题,可我配置scheduler的上下文着实没被夹在多次 之后才在网上找到,虽然还不明白原理,但是还是贴出来分享下
- Spring + JDK Timer Scheduler Example--reference
http://www.mkyong.com/spring/spring-jdk-timer-scheduler-example/ In this example, you will use Sprin ...
- [Spring]支持注解的Spring调度器
概述 如果想在Spring中使用任务调度功能,除了集成调度框架Quartz这种方式,也可以使用Spring自己的调度任务框架. 使用Spring的调度框架,优点是:支持注解(@Scheduler),可 ...
- 【译】Spring 4 基于TaskScheduler实现定时任务(注解)
前言 译文链接:http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotati ...
- Quartz与Spring Boot集成使用
上次自己搭建Quartz已经是几年前的事了,这次项目中需要定时任务,需要支持集群部署,想到比较轻量级的定时任务框架就是Quartz,于是来一波. 版本说明 通过搜索引擎很容易找到其官网,来到Docum ...
- 下雨天,适合学「Spring Boot」
北方的闷热,让不少小伙伴盼着下雨,前几天北京下了场大雨,杭州也紧跟这下了场雨,就在昨天原本还很闷热的天,突然就飘泼大雨了.今天也断断续续的下着小雨,一觉醒来已经是10点了.有句话说:懒惰是人的天性 ...
- Spring Batch(4): Job具体解释
第四章 配置作业Job 4.1 基本配置 Job的配置有3个必须的属性.name,jobRepository,steps.一个简单的Job配置例如以下: <job id="footba ...
- Spring Batch(4): Job详解
Spring Batch(4): Job详解 2016-03-26 18:46 870人阅读 评论(1) 收藏 举报 分类: Spring(6) 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
随机推荐
- java多线程死锁
进程(线程)同步的基本概念 进程之间的制约关系 1. 直接制约关系(进程同步) 这个关系主要源于进程合作,例如,有一个输入进程A通过单缓冲向进程B提供数据,当该缓冲空时,进程B因为不能获得所需数据而被 ...
- 动手写一个简单版的谷歌TPU-指令集
系列目录 谷歌TPU概述和简化 基本单元-矩阵乘法阵列 基本单元-归一化和池化(待发布) TPU中的指令集 SimpleTPU实例: (计划中) 拓展 TPU的边界(规划中) 重新审视深度神经网络中的 ...
- AJAX异步提交form表单
记录: 网上有说怎么做,没说怎么接收,打印了一下数据,记录一下取值: 比如说有如下form: <form id="form1" name="form1" ...
- valet环境PHPstorm+xdebug调试
1.安装xdebug 2.配置xdebug zend_extension="/usr/local/Cellar/php@7.2/7.2.18/pecl/20170718/xdebug.so& ...
- Python文件中将print的输出内容重定向到变量中
有时候需要用到别人的代码, 但是又不想修改别人的文件, 想拿到输出的结果, 这时候就需要使用sys模块, 将print输出的内容重定向到变量中. Python调用sys模块中的sys.stdout, ...
- 学习 GitHub 有什么好处?
layout: post title: "学习 GitHub 有什么好处?" date: 2018-04-15 19:20:20 +0800 --- 鸣谢:王顶 老师(河北经贸大学 ...
- asyncio系列之sleep()实现
先来看个例子,自己实现的模拟耗时操作 例1 import types import select import time import socket import functools class Fu ...
- CentOS7 使用 kubeadm 搭建 k8s 集群
一 安装Docker-CE 前言 Docker 使用越来越多,安装也很简单,本次记录一下基本的步骤. Docker 目前支持 CentOS 7 及以后的版本,内核要求至少为 3.10. Docker ...
- 那些有实力进入 BAT 的本科生,都做对了什么事?
作者:黄小斜 文章来源:微信公众号[黄小斜] 最近这段时间,我们部门来了几个年纪轻轻的本科生,最小的比我们小五岁左,这对于我来说还是比较有冲击力的. 想想我也是九0出头的老腊肉了,想当年我上大学的时候 ...
- kuangbin专题 专题一 简单搜索 迷宫问题 POJ - 3984
题目链接:https://vjudge.net/problem/POJ-3984 这个题目,emm,上代码,看的估计应该是刚开始接触搜索的,我带点注释,你能慢慢理解. #include <ios ...