springBoot 定时器任务
1、新建一个计划任务类(只能和主类平级或在主类的下级)
import java.text.SimpleDateFormat;
import java.util.Date; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class ScheduledTasks {
private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dataFromat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 1000)
public void reportCurrent(){
logger.info("现在时间:{}",dataFromat.format(new Date()));
} }
本示例中使用的是 fixedRate函数,它指定的是从调用开始时间到指定时间之后,单位毫秒。还有 fixedDelay指定从
完成任务测量的时间间隔。还可以指定具体时间,使用 Scheduled(cron="... ")
cron参数说明: 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午12点触发
其中 按顺序依次为:
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
2、启用定时任务
当上面一切被设置好之后,还需要在主类中加入 @EnableScheduling 注解来启动任务,否则定时任务不会被执行
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling //必须加此注解
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3、启动,当一切编译完毕时,可以启动来测试了,在类中右键-->RunAs-->SpringBoot App,一切正常就会看到如下结果
springBoot 定时器任务的更多相关文章
- 补习系列(9)-springboot 定时器,你用对了吗
目录 简介 一.应用启动任务 二.JDK 自带调度线程池 三.@Scheduled 定制 @Scheduled 线程池 四.@Async 定制 @Async 线程池 小结 简介 大多数的应用程序都离不 ...
- springBoot 定时器
程序入口类中加入注解 @EnableScheduling 配置定时任务为并行 @Slf4j @Configuration public class ScheduledConfig implements ...
- springboot 定时器 Schdule
定时器:定时启动任务,执行代码 1.在启动类中加入注解: 2.创建一个类,并且在这个类上加入注解:@Component 3.定义一个方法,在方法上加入注解:@Scheduled(cron=" ...
- SpringBoot定时器任务
Spring Boot使用@Scheduled定时器任务 摘要: Spring Boot之使用@Scheduled定时器任务 假设我们已经搭建好了一个基于Spring Boot项目,首先我们要在A ...
- SpringBoot定时器
使用Component注解注解一个类,这个类就变成了一个组件.组件可以有很多不同的特性,比如Scheduled注解为组件的某个函数添加了定时的特性. @Component public class M ...
- 关于springboot的定时器配置
关于springboot的定时器: 比较重要的两个注解: @EnableScheduling:标注启动定时任务. @Scheduled(fixedRate = 1000 * 30) 定义某个定时任务 ...
- SpringBoot系列——EnableScheduling,对定时器的支持
前言 定时器功能在项目里面往往会用到,比如定时发送邮件.定时释放数据库资源:这里记录一下springboot对定时器的支持的简单实例 cron表达式 开始之前要先介绍一下cron表达式,这里当一下百度 ...
- Springboot+websocket+定时器实现消息推送
由于最近有个需求,产品即将到期(不同时间段到期)时给后台用户按角色推送,功能完成之后在此做个小结 1. 在启动类中添加注解@EnableScheduling package com.hsfw.back ...
- 关于给springboot添加定时器的两种方式
原文:https://blog.csdn.net/liboyang71/article/details/72781526 首先,搭建好一个springboot项目,可使用maven或者gradle或者 ...
随机推荐
- RStudio 断点调试 进入for循环语句调试
参考: http://www.rstudio.com/ide/docs/debugging/overview 1.进入调试模式 全选代码,点击source即可进入调试模式. 2.进入for 调试 在F ...
- 9、IPA通路分析相关网页教程
IPA FAQ: http://ingenuity.force.com/ipa/IPATutorials# ####有各种相关教程和帮助文件. IPA 分析结果展示: http://www.lucid ...
- p1197&bzoj1015 星球大战
传送门(洛谷) 传送门(bzoj) 题目 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的 ...
- ASP.NET jquery 获取服务器控件ID
一般方法: jQuery("#txtUserName").val(); 如果页面加载了母版页或者自定义控件:该页面的ID有可能会被篡改(可能是因为避免控件ID冲突的机制),因此强烈 ...
- 利用Fitnesse测试外部jar包
1. 下载Fitnesse官方jar http://www.fitnesse.org/FitNesseDownload 2. 下载后,创建下面目录,其中FitnesseRoot目录,不需要创建,Fit ...
- Codeforces Round #520 (Div. 2)B(贪心,数学)
#include<bits/stdc++.h>using namespace std;int mi[100007];int main(){ int cnt=0; int flag=0; i ...
- 2018ICPC徐州区域赛网络赛G(VECTOR+SET,模拟)
#include<bits/stdc++.h>using namespace std;int x,y;vector<int>v1,v2;long long solve(vect ...
- 《OD学hadoop》20160904某旅游网项目实战
一.ETL操作 抽取数据 日志格式: 分割符号:^A IP地址 服务器时间 二.Java工程 1. 创建项目 copy代码及配置文件 2. 改配置 core-site.xml hbase-site.x ...
- cf785D(组合数学)
题目链接: http://codeforces.com/problemset/problem/785/D 题意: 左边全为 '(' 右边全为 ')' 且两者数量想等的字符串称为 RSBS. 给出一个由 ...
- 基础篇 - pg_isready
pg_isready 发起一个到指定 PostgreSQL数据库的连接检查. 使用方法: pg_isready [选项]... 选项: -d, --dbname=DBNAME 数据库名 -q, --q ...