如果每个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. 使用docker-compose安装wordpress

    一.建立应用的目录 mkdir my_wordpress cd my_wordpress 二.创建 docker-compose.yml touch docker-compose.yml;vi doc ...

  2. driver.find_element_by_xpath.clear()无法清空输入框默认值

    输入框带默认值,想删除默认值,填写新内容,使用clear()再send_keys(), 发现这种方式无法清除,只会在默认值后面追加新的内容. 上网搜了一下,有两种解决方案,如下: 方法一: 先双击,后 ...

  3. 【poj2661】Factstone Benchmark(斯特林公式)

    传送门 题意: 给出\(x,x\leq 12\),求最大的\(n\),满足\(n!\leq 2^{2^x}\). 思路: 通过斯特林公式: \[ n!\approx \sqrt{2\pi n}\cdo ...

  4. IDEA如何打包可运行jar,外部引用jar包版

    背景: 有时候,我们会用IDEA来开发一些小工具,需要打成可运行的JAR包:或者某些项目不是WEB应用,纯粹是后台应用,发布时,也需要打成可运行的JAR包.并且,如果依赖第三方jar时,又不希望第三方 ...

  5. 解决VirtualBox虚拟机中PM3总是自动断开的问题

    一.问题 运行环境: 虚拟机软件:VirtualBox 6.0.8 r130520 宿主机:Win10 1803 客户机:Ubuntu 19.04 问题: 当插入PM3并连入虚拟机后,PM3过几十秒会 ...

  6. promise 和 setTimeout 在任务队列的执行顺序

    setTimeout(() => { console.log() }); const a = new Promise((resolve,reject)=>{ console.log(); ...

  7. WPF 精修篇 DataGrid 筛选

    原文:WPF 精修篇 DataGrid 筛选 DataGrid也可以分组 但是用的地方不多 就没写 筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选 贴码 <DataGrid x: ...

  8. HTML连载42-清空默认边距、文字行高

    一.            webstorm取色技巧:webstorm内置了颜色取色器,我们对某种颜色未知的时候,可以利用下图中的取色器,进行颜色识别. 二.系统会默认给body添加外边距,因此我们对 ...

  9. MySQL下载和安装教程

    1.下载MySQL数据库可以访问官方网站:https://www.mysql.com/ 2.点击DOWNLOADS模块下的Community模块下的MySQL Community Server进行下载 ...

  10. SpringMVC方法的返回值类型和自动装配

    1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest& ...