SpringBoot之异步定时任务

如果每个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,如果加到类上,表明所有方法都异步执行】

扫描二维码关注公众号,回复: 8055982 查看本文章

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执行成功!");
}


}

猜你喜欢

转载自www.cnblogs.com/Koaler/p/11974469.html