本文主要记录:如何使用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的更多相关文章

  1. Spring Boot 定时任务 @Scheduled

    项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...

  2. spring cloud 定时任务

    项目中,因为使用了第三方支付(支付宝和微信支付),支付完毕后,第三方支付平台一般会采用异步回调通知的方式,通知商户支付结果,然后商户根据通知内容,变更商户项目支付订单的状态.一般来说,为了防止商户项目 ...

  3. spring 配置定时任务Scheduled

    一:在spring配置的xml文件添加3条命名空间 xmlns:task="http://www.springframework.org/schema/task" xsi:sche ...

  4. Spring中定时任务@Scheduled的一点小小研究

    最近做一个公众号项目,微信公众号会要求服务端找微信请求一个access_token,获取的过程: access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_tok ...

  5. Spring Task定时任务Scheduled

    Spring的任务调度,采用注解的形式 Spring中@Scheduled的用法. spring的配置文件如下,先扫描到任务的类,打开spirng任务的标签 <beans xmlns=" ...

  6. Spring的定时任务@Scheduled(cron = "0 0 1 * * *")

    指定某个方法在特定时间执行,如: cron="0 0 1 1 * ?" 即这个方法每月1号凌晨1点执行一次 关于这个注解的解释网上一大堆 但是今天遇到个问题,明明加了注解@Sche ...

  7. 【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 ...

  8. spring 定时任务@Scheduled

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  9. spring boot 学习(八)定时任务 @Scheduled

    SpringBoot 定时任务 @Scheduled 前言 有时候,我们有这样的需求,需要在每天的某个固定时间或者每隔一段时间让应用去执行某一个任务.一般情况下,可以使用多线程来实现这个功能:在 Sp ...

随机推荐

  1. Linux中常用的函数

    1.devm_kzalloc() 函数 devm_kzalloc() 和kzalloc()一样都是内核内存分配函数,但是devm_kzalloc()是跟设备(device)有关的,当设备(device ...

  2. Tensorflow & Python3 做神经网络(视频教程)

    Tensorflow 简介 1.1 科普: 人工神经网络 VS 生物神经网络 1.2 什么是神经网络 (Neural Network) 1.3 神经网络 梯度下降 1.4 科普: 神经网络的黑盒不黑 ...

  3. ActiveMQ集群方案

    集群方案主要为了解决系统架构中的两个关键问题:高可用和高性能.ActiveMQ服务的高可用性是指,在ActiveMQ服务性能不变.数据不丢失的前提下,确保当系统灾难出现时ActiveMQ能够持续提供消 ...

  4. Java中 @Override 的作用

    @Override是伪代码,表示重写(当然不写也可以),不过写上有如下好处: 可以当注释用,方便阅读: 编译器可以给你验证@Override下面的方法名是否是你父类中所有的,如果没有则报错.例如,你如 ...

  5. redis和memcached选择,对比分析

    memcache和redis是互联网分层架构中,最常用的KV缓存.不少同学在选型的时候会纠结,到底是选择memcache还是redis? memcache提供的功能是redis提供的功能的子集,不用想 ...

  6. Guid 几种格式化

    //32 位数字:00000000000000000000000000000000 Console.WriteLine(Guid.NewGuid().ToString("N")); ...

  7. BASIC-12_蓝桥杯_十六进制转八进制

    总结: 1.使用库函数可有效节省空间,但时间花费较多; 2.由于本题的输入数据较大,又限制时间,故要注意利用空间换时间; 3.使用顺序结构换取最小运行时间; 示例代码: #include <st ...

  8. Qt5布局管理(二)——QDockWidget停靠窗口类

    转载:LeeHDsniper 停靠窗口类QDockWidget 实例效果 如右图所示,左半部分MainWindow是该窗口的中心窗口,右边的最下面两个停靠窗口可以跳出该窗口: 但是第一个停靠窗口只能停 ...

  9. freemarker 宏嵌套nested 的使用

    转载来源:http://blog.sina.com.cn/s/blog_7e5699790100z59g.html 模板页: <#assign basePath = request.contex ...

  10. 小峰servlet/jsp(5)jsp自定义标签

    一.自定义标签helloworld: 二.自定义有属性的标签: HelloWorldTag.java:继承TagSupport: package com.java1234.tag; import ja ...