环境
  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. Ubuntu只读文件系统修复方法

    首先备份重要数据 fsck.ext4 -p /dev/sdb5 reboot

  2. Pychram中使用reduce()函数报错:Unresolved reference 'reduce'

    python3不能直接使用reduce()函数,因为reduce() 函数已经被从全局名字空间里移除了,它现在被放置在fucntools 模块里,所以要使用reduce函数得先饮用fucntools ...

  3. MySQL利用LOCATE,CONCAT查询

    SELECT * FROM table t WHERE 1=1 AND LOCATE(#{searchParams},CONCAT(t.account,IFNULL(t.id,''),IFNULL(t ...

  4. Docker报错“Dockerfile parse error line 1: FROM requires either one or three arguments”

    看官方文档Format: 以 '#' 开头一行被视为评论,出现在其他位置视为参数. 也就不难理解报错原因:将写在同一行的注释视为参数了. 原Dockerfile: 改为:

  5. PHP - assert()

    Find and exploit the vulnerability to read the file .passwd.-------------查找并利用此漏洞读取文件.passwd. assert ...

  6. JavaScript基础11——ES5和ES6

    ES5↓↓↓ bind方法/this关键字     bind()方法会创建一个新函数,称为绑定函数,当调用这个绑定函数时,绑定函数会以创建它时传入bind()方法的第一个参数作为this,传入 bin ...

  7. ubuntu及Cenos国内镜像下载

    打开:https://man.linuxde.net/download/ CentOS 7提供了三种ISO镜像文件的下载: DVD ISO 标准安装版,一般下载这个就可以了(推荐) Everythin ...

  8. word2vector的tensorflow代码实现

    import collections import math import os import random import zipfile import numpy as np import urll ...

  9. jmeter通过cookie获取图片验证码实现登录2

    在登录时有一张图片验证码,需要获取验证码用于后续登录,见图 1.找到图片验证码接口写入jmeter 2.正则表达式提取cookie 3.Fiddler抓取登录成功的响应cookie,并设置成全局 4. ...

  10. Shell 中eval的用法

    test.sh:pipe="|"eval ls $pipe wc -l 输出bogon:Desktop macname$ ./test.sh 45 test.sh:eval ech ...