前言
在业务开发过程中,有时会遇到非核心的业务,如记录日志或发送短信等。有时也会遇到一次性要处理多个任务,再将多个任务处理结果进行返回。如果做同步处理时,会比较耗时,对于接口而言,响应速度慢一方面会影响用户体验,另一方面如果是服务间调用,可能会出现超时的情况。这种情况我们可以采用异步处理的方式,提高主流程的性能。
集成方法
1、springboot启动类添加@EnableAsync注解
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2、创建配置属性类AsyncProperties
@Component
@Data
@ConfigurationProperties(prefix = "async")
public class AsyncProperties {
private int corePoolSize;
private int maxPoolSize;
private int queueCapacity;
private int keepAlive;
}
3、添加异步配置
@Configuration
@Slf4j
public class AsyncConfig implements AsyncConfigurer {
@Autowired
private AsyncProperties asyncPropertie