springboot启用异步执行注解:
场景:对用户触发事件进行埋点记录分析
思路:
Controller进行token认证后讲请求信息转发给service异步执行,然后直接return结果(无需知道service执行结果) 前端也不关注controller执行结果
实现
将关键信息发给service后直接return结果
service异步执行,所以controller无需等待service执行完毕,可以直接返回结果(不知道我理解的对不对,欢迎指正)
异步执行注解配置类
package com.awservice.config;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
@Configuration
@EnableAsync
@Component
@ConfigurationProperties(prefix = "thread")
public class ExecutorConfig {
private int corePoolSize;
private int maxPoolSize;
private int maxQueue;
private String namePrefix;
private int keepAlive;
@Bean
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心线程数
executor.setCorePoolSize(corePoolSize);
//最大线程数
executor.setMaxPoolSize(maxPoolSize);
//队列大小
executor.setQueueCapacity(maxQueue);
//线程池中的线程的名称前缀
executor.setThreadNamePrefix(namePrefix);
//回收时间
executor.setKeepAliveSeconds(keepAlive);
// 当pool已经达到max size的时候
// 不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//执行初始化
executor.initialize();
return executor;
}
public int getCorePoolSize() {
return corePoolSize;
}
public void setCorePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
}
public int getMaxPoolSize() {
return maxPoolSize;
}
public void setMaxPoolSize(int maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public int getMaxQueue() {
return maxQueue;
}
public void setMaxQueue(int maxQueue) {
this.maxQueue = maxQueue;
}
public String getNamePrefix() {
return namePrefix;
}
public void setNamePrefix(String namePrefix) {
this.namePrefix = namePrefix;
}
public int getKeepAlive() {
return keepAlive;
}
public void setKeepAlive(int keepAlive) {
this.keepAlive = keepAlive;
}
}
配置抽离到properties
----分割线----
1.异步调用
异步调用就是在不阻塞主线程的情况下执行高耗时方法
(高频率同样也可以,比如我这个埋点的项目)
2.常规异步
通过开启新线程实现
3.在Springboot中启用异步方法
需要4个注解
@EnableAsync 开启异步
@Component 注册异步组件
@Autowired 注入异步组件
@Async 标注异步方法
4.进行一次异步调用
首先在一个Config类上标注开启异步
然后创建一个异步的组件类,就跟Service,Controller 一样一样的,用Component标注,Service也行
在类内创建一个异步方法,打上Async 标记。这个方法必须是实例方法。
然后就跟注入Service一样一样的了。
5.异步事务
在Async 方法上标注@Transactional是没用的。
在Async 方法调用的Service上标注@Transactional 有效。
6.异步方法的内部调用
异步方法不支持内部调用,也就是异步方法不能写在需要调用他的类的内部。
比如Class A 有a,b,c。b有Async标注。此时a对b的异步调用是失效的。
7.为什么异步方法必须是实例方法
因为static方法不能被Override。因为@Async 异步方法的实现原理是通过注入一个代理类到Bean中,这个代理继承这个Bean,需要覆写异步方法并执行。
然后这个东西,会被Spring放到自己维护的一个队列中。等待线程池读取并执行。
(尝试自己调优参数,核心线程数,最大线程数,队列容量,pool达到max时的策略,回收时间等等,默认的不一定是合适的)
这些解释参考自:https://blog.csdn.net/qq_15071263/article/details/80165680 (不想打字。。。。。)