环境
  eclipse 4.7
  jdk 1.8
  Spring Boot 1.5.2
一、定时任务
1、启动类添加注解@EnableScheduling 用于开启定时任务

package com.wjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling //开启定时任务
public class APP { public static void main(String[] args) {
SpringApplication.run(APP.class, args);
} }

2、定义@Component定时组件类和@Scheduled定义执行周期

/**
*
*/
package com.wjy.scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* @Desc 定时任务
* @author wangjy15
*/
@Component
public class ScheduledTasks { //fixedRate 周期 频率
//失败后一直重试
//@Scheduled(fixedRate=1000)
@Scheduled(cron="0/1 * * * * ?")
public void task() {
System.out.println("每隔1秒钟打印...");
// 每隔1秒钟打印...
// 每隔1秒钟打印...
// 每隔1秒钟打印...
// 每隔1秒钟打印...
// 每隔1秒钟打印...
} }

两种方式:

(1)fixedDelay:
示例:@Scheduled(fixedRate=1000) 每隔1秒
(2)cron表达式:
示例:@Scheduled(cron = "0/10 * * * * ?")每隔1秒
Seconds Minutes Hours DayofMonth Month DayofWeek,注意:spring boot不支持年
每一个域可出现的字符如下:

Seconds: 可出现", - * /"四个字符,有效范围为0-59的整数
Minutes: 可出现", - * /"四个字符,有效范围为0-59的整数
Hours: 可出现", - * /"四个字符,有效范围为0-23的整数
DayofMonth :可出现", - * / ? L W C"八个字符,有效范围为0-31的整数
Month: 可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
DayofWeek: 可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
Year: 可出现", - * /"四个字符,有效范围为1970-2099年

每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:

(1) *:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。
(2) ?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和 DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
(3) -:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
(4) /:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
(5) ,:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
(6) L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
(7) W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一 到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份。
(8) LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
(9) #:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
举几个例子:
每隔5秒执行一次:"*/5 * * * * ?"
每隔1分钟执行一次:"0 */1 * * * ?"
每天23点执行一次:"0 0 23 * * ?"
每天凌晨1点执行一次:"0 0 1 * * ?"
每月1号凌晨1点执行一次:"0 0 1 1 * ?"
每月最后一天23点执行一次:"0 0 23 L * ?"
每周星期天凌晨1点实行一次:"0 0 1 ? * L"
在26分、29分、33分执行一次:"0 26,29,33 * * * ?"
每天的0点、13点、18点、21点都执行一次:"0 0 0,13,18,21 * * ?"
表示在每月的1日的凌晨2点调度任务:"0 0 2 1 * ? *"
表示周一到周五每天上午10:15执行作业:"0 15 10 ? * MON-FRI"
表示2002-2006年的每个月的最后一个星期五上午10:15执行:"0 15 10 ? 6L 2002-2006"
注意:由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?
cron表达式 参考:http://cron.qqe2.com/

3、Spring Boot整合Quzrtz

二、Spring Boot异步执行

类似开启了一个线程来执行,使用场景:发送短信、发送邮件、APP消息推送

1、添加@EnableAsync 开启异步

package com.wjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication
@EnableAsync
public class APP { public static void main(String[] args) {
SpringApplication.run(APP.class, args);
} }

2、定义@Component类和@Async方法

package com.wjy.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; @Component
public class AsyncTask { @Async
public String sendSms() {
System.out.println("###sendSms###3");
System.out.println("sendSms" );
System.out.println("###sendSms###4");
return "success";
} }

3、controller

package com.wjy.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.wjy.async.AsyncTask; @RestController
public class UserController { private static Logger log = Logger.getLogger(UserController.class); @RequestMapping("/sendSms")
public String sendSms() {
System.out.println("###sendMsg###1");
asyncTask.sendSms();
System.out.println("###sendMsg###2");
return "success";
} }

4、测试验证:http://localhost:8080/sendSms

不添加注解@Async:

###sendMsg###1
###sendSms###3
sendSms
###sendSms###4
###sendMsg###2

添加注解@Async:

###sendMsg###1
###sendMsg###2
###sendSms###3
sendSms
###sendSms###4

参考:

springboot+quartz整合:
https://blog.csdn.net/tuesdayma/article/details/81538270
https://blog.csdn.net/tuesdayma/article/details/81563011
https://segmentfault.com/a/1190000016554033
https://www.jb51.net/article/148204.htm

Quartz简介:
https://www.cnblogs.com/zhanghaoliang/p/7886110.html
Quartz表+表字段含义:
https://blog.csdn.net/fly_captain/article/details/83058147

【Spring Boot学习之六】Spring Boot整合定时任务&异步调用的更多相关文章

  1. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  2. Spring Cloud 学习 之 Spring Cloud Eureka(搭建)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...

  3. Spring @async 方法上添加该注解实现异步调用的原理

    Spring @async 方法上添加该注解实现异步调用的原理 学习了:https://www.cnblogs.com/shangxiaofei/p/6211367.html 使用异步方法进行方法调用 ...

  4. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  5. Spring Boot学习笔记——Spring Boot与MyBatis的集成(项目示例)

    1.准备数据库环境 # 创建数据库 CREATE DATABASE IF NOT EXISTS zifeiydb DEFAULT CHARSET utf8 COLLATE utf8_general_c ...

  6. spring cloud学习(六)Spring Cloud Config

    Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...

  7. Spring 框架学习(1)--Spring、Spring MVC扫盲

    纸上得来终觉浅,绝知此事要躬行 文章大纲 什么是spring 传统Java web应用架构 更强的Java Web应用架构--MVC框架 Spring--粘合式框架 spring的内涵 spring核 ...

  8. Spring Cloud 学习 (九) Spring Security, OAuth2

    Spring Security Spring Security 是 Spring Resource 社区的一个安全组件.在安全方面,有两个主要的领域,一是"认证",即你是谁:二是& ...

  9. Spring框架学习03——Spring Bean 的详解

    1.Bean 的配置 Spring可以看做一个大型工厂,用于生产和管理Spring容器中的Bean,Spring框架支持XML和Properties两种格式的配置文件,在实际开发中常用XML格式的配置 ...

随机推荐

  1. For 32-bit BOOL is a signed char, whereas under 64-bit it is a bool.

    https://stackoverflow.com/questions/31267325/bool-with-64-bit-on-ios/31270249#31270249 Definition of ...

  2. React Core Features

    React Core Features Here is a summary of the core features. We will cover each feature in detail thr ...

  3. reduce要素与适用总结

    要素: 1.高阶函数:reduce: 2.处理函数:reducer: 3.数据:可以是具体数据.签名相同的普通函数.签名相同的高阶函数: reduce(reducer, datas(data or f ...

  4. 同步docker中的容器时间和宿主机相同

    同步docker中的容器时间和宿主机相同.cd /etc/ 在容器中修改下/etc/localtime文件的名称,避免冲突. mv localtime localtime_bak cp /usr/sh ...

  5. python语言(四)关键字参数、内置函数、导入第三方模块、OS模块、时间模块

    一.可变参数 定义函数时,有时候我们不确定调用的时候会传递多少个参数(不传参也可以).此时,可用包裹(packing)位置参数(*args),或者包裹关键字参数(**kwargs),来进行参数传递,会 ...

  6. 安装单机es

    1.安装JDK(1.8)2.上传解压Elasticsearch-5.4.33.创建一个普通用户,然后将对于的目录修改为普通用户的所属用户和所属组4.修改配置文件config/elasticsearch ...

  7. vue分组全选权限,CheckBoxGroup

    <template> <div class="comPower"> <div class="card_header" v-show ...

  8. MySQL的简单概念及软件安装

    数据库的简介 一.数据库的基本概念:数据.数据库.数据库管理系统.数据库系统 数据:数据(Data)是用来记录信息的可识别符号,是信息的具体表现形式. 数据库:(1)数据库(Database,DB)是 ...

  9. systemd socket activation golang demo

    service define rongapp.service [Unit] Description=rong Hello World HTTP Requires=network.target rong ...

  10. Zookeeper——一致性协议:Zab协议

    Reference: https://www.jianshu.com/p/2bceacd60b8a 什么是Zab协议 Zab 协议的作用 Zab 协议原理 Zab 协议核心 Zab 协议内容 原子广播 ...