spring Cloud 定时任务 @Scheduled
本文主要记录:如何使用spring的@Scheduled注解实现定时作业,基于spring cloud
1)pom.xml 文件引入相关依赖、spring-maven插件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.vip.qa</groupId>
<artifactId>springboot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)定时任务类
@Component:类注册成bean
@Scheduled:定时任务,可选固定时间、cron表达式等类型
cron表达式 每位的意义:Seconds Minutes Hours DayofMonth Month DayofWeek
package com.vip.qa.vop.service; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* Created by danny.yao on 2017/10/19.
*/
@Component
public class ScheduledTasks { private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Scheduled(fixedRate = 5000)
public void scheduledDemo(){
logger.info("scheduled - fixedRate - print time every 5 seconds:{}", formate.format(new Date()) );
} /**
"0/5 * * * * ?" 每5秒触发
"0 0 12 * * ?" 每天中午十二点触发
"0 15 10 ? * *" 每天早上10:15触发
"0 15 10 * * ?" 每天早上10:15触发
"0 15 10 * * ? *" 每天早上10:15触发
"0 15 10 * * ? 2005" 2005年的每天早上10:15触发
"0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟一次触发
"0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟一次触发
"0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
"0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发
"0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五的10:15触发
*/
@Scheduled(cron="0/10 * * * * ?")
public void scheduledCronDemo(){
logger.info("scheduled - cron - print time every 10 seconds:{}", formate.format(new Date()) );
}
}
3)应用程序入口类Application
@SpringBootApplication:等于原来的 (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan
@EnableScheduling:启用定时任务
package com.vip.qa.vop; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* Created by danny.yao on 2017/10/19.
*/
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
4)运行入口类,可以看到结果
spring Cloud 定时任务 @Scheduled的更多相关文章
- Spring Boot 定时任务 @Scheduled
项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...
- spring cloud 定时任务
项目中,因为使用了第三方支付(支付宝和微信支付),支付完毕后,第三方支付平台一般会采用异步回调通知的方式,通知商户支付结果,然后商户根据通知内容,变更商户项目支付订单的状态.一般来说,为了防止商户项目 ...
- spring 配置定时任务Scheduled
一:在spring配置的xml文件添加3条命名空间 xmlns:task="http://www.springframework.org/schema/task" xsi:sche ...
- Spring中定时任务@Scheduled的一点小小研究
最近做一个公众号项目,微信公众号会要求服务端找微信请求一个access_token,获取的过程: access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_tok ...
- Spring Task定时任务Scheduled
Spring的任务调度,采用注解的形式 Spring中@Scheduled的用法. spring的配置文件如下,先扫描到任务的类,打开spirng任务的标签 <beans xmlns=" ...
- Spring的定时任务@Scheduled(cron = "0 0 1 * * *")
指定某个方法在特定时间执行,如: cron="0 0 1 1 * ?" 即这个方法每月1号凌晨1点执行一次 关于这个注解的解释网上一大堆 但是今天遇到个问题,明明加了注解@Sche ...
- 【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 定时任务@Scheduled
1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- spring boot 学习(八)定时任务 @Scheduled
SpringBoot 定时任务 @Scheduled 前言 有时候,我们有这样的需求,需要在每天的某个固定时间或者每隔一段时间让应用去执行某一个任务.一般情况下,可以使用多线程来实现这个功能:在 Sp ...
随机推荐
- apache airflow docker 运行简单试用
airflow 是一个编排.调度和监控workflow的平台,由Airbnb开源,现在在Apache Software Foundation 孵化. airflow 将workflow编排为tasks ...
- 理解git
为了真正了解git,我们从底部.底层开始,了解git核心,知其然并知其所以然. 为什么要进行版本控制呢? 因为编写文件不可能一次到位,文件总是有不同的状态需要保存下来,方便以后出错回滚. git 是目 ...
- Javascript Array 方法整理
Javascript Array 方法整理 Javascript 数组相关方法 说明 大多数其它编程语言不允许改变数组大小,越界访问索引会报错,但是 javascript不会报错,不过不建议直接修改a ...
- tomcat源码阅读之容器(Container)
一. 实现容器的接口是Container接口,Tomcat中共有四种类型的容器: 1.Engine:表示整个Catalina Servlet引擎: 2.Host:表示含有一个或者多个Context容器 ...
- bzoj 2784 [JLOI2012]时间流逝——树上高斯消元
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2784 一个状态可以加很多个能量圈,但减少能量圈的情况只有一种.所以可以用树来刻画. 然后就变 ...
- datacolumn 表达式 除数为0
dt.columns.add("avgp",typeof(decimal),"IIF(qty=0,0,price/qty)")
- visual leak detector用法
百度vld和windbg安装 配置symbol路径 配置环境变量 _NT_SYMBOL_PATH SRV*E:\symbols*http://msdl.microsoft.com/download/s ...
- php的zend引擎执行过程 一
1. Zend引擎主要包含两个核心部分:编译.执行: 执行阶段主要用到的数据结构: opcode: php代码编译产生的zend虚拟机可识别的指令,php7有173个opcode,定义在 zend_v ...
- 微信公众号获取粉丝openid系统
做为一名开发人员,在测试当中也经常需要用到openid,但是微信公众号获取openid的方法也是特别麻烦!网页授权是最常见的方式, 但是网页授权的流程太复杂,不仅要开发,还要在公众号后台设置回调域名( ...
- Web(click and script) 与 Web(HTTP/HTML)协议区别
先从最简单的说明上来看, Web(HTTP/HTML): Emulation of communication between a browser and Web Serve ...