0. 我说网上spring.async相关的博文为啥比较少,原来是简单的缘故
先放代码,就一些简单、常见的异步配置的代码而已…
1. 自定义的异步配置的支持类
public class AsyncSupport {
protected static final String PREFIX_ALIAS_THREAD_NAME = "ANGEL-EXEC-";
protected static final String SUFFIX_ALIAS_THREAD_NAME = "-";
protected static final String DEFAULT_EXECUTOR_QUALIFIER = "angelDefaultExecutor";
protected AsyncSupport() {
}
protected Executor buildAngelExecutor(ExecutorProperties properties) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(properties.getCorePoolSize());
executor.setMaxPoolSize(properties.getMaxPoolSize());
executor.setQueueCapacity(properties.getQueueCapacity());
executor.setKeepAliveSeconds(properties.getKeepAliveSeconds());
executor.setThreadNamePrefix(StringUtils.join(
PREFIX_ALIAS_THREAD_NAME,
properties.getAlias(),
SUFFIX_ALIAS_THREAD_NAME));
return executor;
}
protected ExecutorProperties defaultExecutorProperties() {
return ExecutorProperties.builder()
.alias(DEFAULT_EXECUTOR_QUALIFIER)
.corePoolSize(8)
.maxPoolSize(16)
.queueCapacity(32)
.keepAliveSeconds(64)
.build();
}
}
2. 配置类也很简单,通过spring.async.configurer的方式参与到其工厂类的初始化中去
/**
* @author weng
* @since 2023/3/30
* <p>
* https://dandelioncloud.cn/article/details/1407362417307082753/
* https://blog.csdn.net/ludadan/article/details/116932178
*/
@Configuration
// 默认还是走 application.yml; 但还是要配置,因为不配的话,绝对不走 application-async.yml
@PropertySource(value = "classpath:application-async.yml", factory = YmlPropSrcFactory.class)
@Import(AsyncProperties.class)
@EnableAsync
@Slf4j
//@AutoConfigureAfter autoConfiguration才会生效(spring.factories)
public class AsyncConfig extends AsyncSupport implements AsyncConfigurer {
@Autowired
private MetaProperties metaProperties;
@Autowired
private Map<String, ExecutorProperties> executorProperties;
@Bean(DEFAULT_EXECUTOR_QUALIFIER)
@Primary
public Executor angelDefaultExecutor() {
ExecutorProperties defaultExecutorProperties = this.defaultExecutorProperties();
if (MapUtils.isNotEmpty(executorProperties)) {
ExecutorProperties properties = this.executorProperties.get(DEFAULT_EXECUTOR_QUALIFIER);
if (Objects.nonNull(executorProperties)) {
defaultExecutorProperties = properties;
}
}
return this.buildAngelExecutor(defaultExecutorProperties);
}
@Override
public Executor getAsyncExecutor() {
return this.angelDefaultExecutor();
}
// 处理 异步任务 中的异常
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (ex, method, params) -> {
log.error("ex.msg:[{}], method:[{}], params:[{}]",
ex.getMessage(), method.getName(), Jsons.NO_OP.stringify(params));
};
}
}
3. properties也放一下吧
/**
* @author weng
* @since 2023/3/30
* <p>
* https://blog.csdn.net/inexaustible/article/details/126886130
* https://blog.csdn.net/ludadan/article/details/116932178
* https://blog.csdn.net/pjw1217/article/details/114448798
* https://juejin.cn/post/6844904149448917006
* https://blog.csdn.net/qq_27579471/article/details/124473365
*/
public class AsyncProperties {
@Bean
@ConfigurationProperties("angel.async.meta")
public MetaProperties angelAsyncMetaProperties() {
return new MetaProperties();
}
@Bean
@ConfigurationProperties("angel.async.executors")
public Map<String, ExecutorProperties> angelAsyncExecutorProperties() {
return Maps.newHashMap();
}
}
4. application-async.yml
angel:
async:
meta:
enable: true
info: '我奋力撕破封闭的思绪,震开裂缝!!!'
executors:
# angelDefaultExecutor:
# alias: 'DEFAULT'
# core-pool-size: 8
# max-pool-size: 16
# queue-capacity: 32
# keep-alive-seconds: 60
# angelTestExecutor:
angelDefaultExecutor:
alias: 'TEST'
core-pool-size: 2
max-pool-size: 4
queue-capacity: 8
keep-alive-seconds: 5
# - {alias: 'DEFAULT', core-pool-size: 8, max-pool-size: 16, queue-capacity: 32, keep-alive-seconds: 60}
# - {alias: 'TEST', core-pool-size: 2, max-pool-size: 4, queue-capacity: 8, keep-alive-seconds: 5}