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 ...
随机推荐
- loopback v4 特性
loopback 是一个api 服务框架,挺方便的,同时也已经演进了好几代了v4 有一些新功能的 支持 新特性 基于typescript/es2017 开发 openapi 驱动的rest api 开 ...
- memcached自启动
# # Source function library. . /etc/rc.d/init.d/functions . /etc/sysconfig/network #[ ${NETWORKING} ...
- 【转】每天一个linux命令(17):whereis 命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/09/2761928.html whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数- ...
- meta viewport 理解
移动设备上的浏览器如果不指明 viewport 这个meta,当你从移动设备上浏览网页的时候,它假设(你浏览的是桌面版并且你想看到所有的内容),不只是一个左上角.因此,它会把viewport的宽度设置 ...
- linQ to sql 查询生成的sql语句
1. 如果是控制台应用,直接 db.Log = Console.Out; 2.其他应用则用如下语句: StringBuilder sql = new StringBuilder(); db.Log ...
- JSP 执行流程
一.jsp执行流程 1. 发送请求 ,请求访问jsp文件. 2. 服务器(Tomcat)提供的jsp parser 解析器解将jsp转化为java文件. jsp本质上是一个servlet. 3.ser ...
- go http 传递json数据
上篇博文中简单介绍了Go HTTP的Server 和Client.本文介绍如何在HTTP中传递json格式的数据. Server package main import ( "encodin ...
- JZ2440 裸机驱动 第13章 LCD控制器(1)
本章目标 了解LCD显示器的接口及时序: 掌握S3C2410/S3C2440 LCD控制器的使用方法: 了解帧缓冲区的概念,掌握如何设置帧缓冲区来显示图像: 13.1 LCD和LCD控制器 13.1 ...
- 关于新建android项目时 appcompat_v7报错问题的一点总结
说下我的解决方案: 1.确保 appcompat项目的 target版本 低于 实际项目的android版本(就像.net中 低版本的 framewrok项目不能引用高版本framework项目一样) ...
- wxWidgets:菜单
和菜单有关的类主要有两个:wxMenuItem和wxMenu.wxMenuItem用于表示一个菜单项,而wxMenu是wxMenuItem的弹出或下拉列表. 现在让我们看看如何给我们的框架类加上菜单: ...