如果每个Scheduled方法是同步执行的,万一有一个发生死锁,那么其他任务就没法执行,下面介绍异步定时任务

异步定时任务

Spring为任务调度与异步方法执行提供了注解支持,即通过在方法上设置@Async注解,可使得方法被异步调用。

异步调用的实现就是交给Spring的TaskExecutor来完成。

而这个TaskExecutor我们可以自定义

1、配置文件 application.properties

加上内容:

corePoolSize=10
maxPoolSize = 50
queueCapacity = 10
2、添加线程池配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
* 注解式配置文件
*
* @ClassName: AsyncConfig
* @Description: TODO
* @Author Tan
* @Date 2019/7/5
*/
@Configuration //表明该类是一个配置类
@EnableAsync //开启异步事件的支持
@PropertySource(value = "classpath:application.properties")
public class AsyncConfig {

/**
* 线程池维护线程的最少数量
*/
@Value("${corePoolSize}")
private int corePoolSize;

/**
* 线程池维护线程的最大数量
*/
@Value("${corePoolSize}")
private int maxPoolSize;

/**
* 缓存队列
*/
@Value("${corePoolSize}")
private int queueCapacity;

@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.initialize();
return executor;
}
}
3、然后跟之前一样
启动类【多了@EnableAsync】

@EnableAsync // 使Async生效
@EnableScheduling // 开启对定时任务的支持
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class WebApplication {

public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}

}
任务类【每个方法加上注解@Async,如果加到类上,表明所有方法都异步执行】

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* @ClassName: Schedule1
* @Description: 任务
* @Author Tan
* @Date 2019/7/5
*/
@Component
public class Schedule1 {

@Async
//corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份
@Scheduled(cron = "0/5 * * * * *")
public void calculate1() {
System.out.println("定时任务1执行开始!");

System.out.println("这里是定时任务1执行的内容。。。。。。");

System.out.println("定时任务1执行成功!");
}

@Async
@Scheduled(cron = "0/5 * * * * *")
public void calculate2() {
System.out.println("定时任务2执行开始!");

System.out.println("这里是定时任务2执行的内容。。。。。。");

System.out.println("定时任务2执行成功!");
}

}

SpringBoot之异步定时任务的更多相关文章

  1. 【java框架】SpringBoot(4)--SpringBoot实现异步、邮件、定时任务

    1.SpringBoot整合任务机制 1.1.SpringBoot实现异步方法 日常开发中涉及很多界面与后端的交互响应,都不是同步的,基于SpringBoot为我们提供了注解方式实现异步方法.使得前端 ...

  2. SpringBoot几种定时任务的实现方式

    定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...

  3. springboot整合@Scheduled定时任务的使用

    1.启动类里面添加注解@EnableScheduling ,例如: @SpringBootApplication@EnableScheduling@MapperScan("com.examp ...

  4. SpringBoot使用异步线程池实现生产环境批量数据推送

    前言 SpringBoot使用异步线程池: 1.编写线程池配置类,自定义一个线程池: 2.定义一个异步服务: 3.使用@Async注解指向定义的线程池: 这里以我工作中使用过的一个案例来做描述,我所在 ...

  5. SpringBoot中异步请求和异步调用(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...

  6. springBoot中的定时任务

    springBoot中的定时任务 1:在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 2:新建ScheduledTasks任务类 : package c ...

  7. SpringBoot中的定时任务与Quartz的整合

    SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...

  8. SpringBoot中执行定时任务

    一:在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. @SpringBootApplication @EnableSchedu ...

  9. SpringBoot系列——动态定时任务

    前言 定时器是我们项目中经常会用到的,SpringBoot使用@Scheduled注解可以快速启用一个简单的定时器(详情请看我们之前的博客<SpringBoot系列--定时器>),然而这种 ...

随机推荐

  1. 线性代数笔记24——微分方程和exp(At)

    原文:https://mp.weixin.qq.com/s/COpYKxQDMhqJRuMK2raMKQ 微分方程指含有未知函数及其导数的关系式,解微分方程就是找出未知函数.未知函数是一元函数的,叫常 ...

  2. acwing 49. 二叉搜索树与双向链表

    地址:https://www.acwing.com/problem/content/87/ 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表. 要求不能创建任何新的结点,只能调整树中结点指针 ...

  3. 《3D_Deep_Learning_for_Robot_Perception.pdf》

    https://github.com/PrincetonVision/marvin

  4. 《移动WEB前端高级开发实践@www.java1234.com.pdf》

    HTTP服务器: http-server 3.6.4 利用 Performance API 分析网站性能 页面加载生命周期 4. CSS3 伪类.伪元素, 看https://www.runoob.co ...

  5. Codeforces Round #594 (Div. 2) B. Grow The Tree 水题

    B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...

  6. php date获取当前时间

    结果: 结论: 本以为第一种方式最快,第三种方式竟超乎想象的快且稳定

  7. golang和swoole区别

    golang和swoole区别 开发效率 Go语言是本质上是静态语言,开发效率稍差,但性能更强,更适合底层软件的开发 Swoole使用PHP语言,动态脚本语言,开发效率最佳,更适合应用软件的开发 IO ...

  8. IT兄弟连 Java语法教程 流程控制语句 分支结构语句5

    5  switch-case条件语句 Java中的第二种分支控制语句时switch语句,switch语句提供了多路支持,因此可以使程序在多个选项中进行选择.尽管一系列嵌套if语句可以执行多路测试,然而 ...

  9. python多项式求解

    例如:p(x) = x3 - 3x+5 可以使用向量P=[1,0,-3,5]表示,向量长度减一表示多项式最高项次数. 从右到左分别是变量x的0次幂.1次幂.2次幂……n次幂. 这里可以使用numpy的 ...

  10. RST Methodology: “Responsible Tester”

    翻译另一篇James Bach的关于快速软件测试的文章,原文链接:http://www.satisfice.com/blog/archives/1364 在快速软件测试方法论中,我们区分出三种主要角色 ...