如果每个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. MVC、MTV、FBV、CBV、母版和继承:

    cookie session cookie的定义: 保存在浏览器上的一组组键值对 (请求头) 为什么要有? http协议是无状态,每次的请求之间是相互独立的,没有办法保存状态. Django中操作co ...

  2. windows golang安装golang.org/x/net text grpc

    使用git # 吧$GOPATH替换成自己的GOPATH git clone https://github.com/golang/net.git $GOPATH\src\golang.org\x\ne ...

  3. 2019CSP-J/S受虐记

    emmmm...... 今年noip很波折,我从7月开始准备 但CCF居然停了noip,这搞得我很迷茫,CCF你在干什么! 然后又恢复了,这有搞得我很懵逼?(还改名叫csp了) 就换了个名,CCF你搞 ...

  4. OpenDaylight开发hello-world项目之功能实现

    OpenDaylight开发hello-world项目之开发环境搭建 OpenDaylight开发hello-world项目之开发工具安装 OpenDaylight开发hello-world项目之代码 ...

  5. golang数据结构之冒泡排序

    //BubbleSort 冒泡排序 func BubbleSort(arr *[]int) { ; i >= ; i-- { ; j-- { if (*arr)[j] > (*arr)[i ...

  6. Linux 小工具

    1. 截图工具 shutter 安装 sudo add-apt-repository ppa:shutter/ppa sudo apt-get update sudo apt-get install ...

  7. saltstack--史上最细致安装攻略!亲测无坑

    准备一台虚拟机node1: [root@linux-node1 pillar]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAS ...

  8. MySQL变量介绍和用法简介

    目录 一.用户变量 1.1.用户变量定义 1.2.用户变量用法 二.系统变量 2.1 系统变量简单介绍 2.2 系统变量用法简介 本博客介绍一下MySQL中变量的用法和注意细节 @ 一.用户变量 1. ...

  9. 屏幕输入转换为int//方法大注释

    可以使用两种方法: using System; namespace 方法测试 { class Program { static void Main(string[] args) { Console.W ...

  10. ASP.NET Core快速入门--学习笔记系列文章索引目录

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 抓住国庆假期的尾巴完成了此系列课程的学习笔记输出! ASP.NET Core快 ...