SpringBoot 实现异步处理多个并行任务

在现代Web应用开发中,异步处理和多任务并行处理对于提高系统的响应性和吞吐量至关重要。Spring Boot 提供了多种机制来实现异步任务处理,本文将介绍如何利用这些机制来优化您的应用程序性能。

1. 引言

在高负载情况下,如果所有的请求都采用同步处理的方式,则可能会导致系统响应缓慢甚至超时。为了避免这种情况,我们可以使用异步处理技术来提高应用程序的效率。Spring Boot 提供了简单而强大的工具来支持异步任务处理。

2. 环境准备

确保您的开发环境已安装以下软件:

  • Java 11 或更高版本

  • Maven 3.6 或更高版本

  • Spring Boot 2.x 或更高版本

3. 创建 Spring Boot 项目

首先,我们需要创建一个新的 Spring Boot 项目。可以通过 Spring Initializr 快速生成基本的项目骨架。

3.1 添加依赖

在 pom.xml 文件中添加必要的依赖,如 spring-boot-starter-web 和 spring-boot-starter-aop(用于支持 @Async 注解)。

复制代码
1<dependencies>
2    <dependency>
3        <groupId>org.springframework.boot</groupId>
4        <artifactId>spring-boot-starter-web</artifactId>
5    </dependency>
6    <dependency>
7        <groupId>org.springframework.boot</groupId>
8        <artifactId>spring-boot-starter-aop</artifactId>
9    </dependency>
10</dependencies>
复制代码

3.2 启用异步支持

在主类上添加 @EnableAsync 注解,以启用异步支持。

复制代码
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.scheduling.annotation.EnableAsync;
4
5@SpringBootApplication
6@EnableAsync
7public class AsyncApplication {
8    public static void main(String[] args) {
9        SpringApplication.run(AsyncApplication.class, args);
10    }
11}
复制代码

4. 配置线程池

为了更好地控制异步任务的执行,我们通常会自定义一个线程池。

复制代码
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
4
5@Configuration
6public class AsyncConfig {
7
8    @Bean("asyncExecutor")
9    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
10        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
11        executor.setCorePoolSize(2); // 核心线程数
12        executor.setMaxPoolSize(5); // 最大线程数
13        executor.setQueueCapacity(10); // 队列大小
14        executor.setThreadNamePrefix("AsyncExecutor-"); // 线程名称前缀
15        executor.initialize();
16        return executor;
17    }
18}
复制代码

5. 创建异步服务

接下来,我们将创建一些异步服务方法,并使用 @Async 注解标记它们。

复制代码
1import org.springframework.scheduling.annotation.Async;
2import org.springframework.stereotype.Service;
3
4import java.util.concurrent.CompletableFuture;
5
6@Service
7public class AsyncService {
8
9    @Async("asyncExecutor")
10    public CompletableFuture<Void> performTaskOne() {
11        System.out.println("Executing task one in " + Thread.currentThread().getName());
12        try {
13            Thread.sleep(2000);
14        } catch (InterruptedException e) {
15            throw new IllegalStateException(e);
16        }
17        return CompletableFuture.completedFuture(null);
18    }
19
20    @Async("asyncExecutor")
21    public CompletableFuture<Void> performTaskTwo() {
22        System.out.println("Executing task two in " + Thread.currentThread().getName());
23        try {
24            Thread.sleep(3000);
25        } catch (InterruptedException e) {
26            throw new IllegalStateException(e);
27        }
28        return CompletableFuture.completedFuture(null);
29    }
30
31    @Async("asyncExecutor")
32    public CompletableFuture<Void> performTaskThree() {
33        System.out.println("Executing task three in " + Thread.currentThread().getName());
34        try {
35            Thread.sleep(1000);
36        } catch (InterruptedException e) {
37            throw new IllegalStateException(e);
38        }
39        return CompletableFuture.completedFuture(null);
40    }
41}
复制代码

6. 控制器

在控制器中,我们将调用这些异步服务方法,并等待它们完成。

复制代码
1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.web.bind.annotation.GetMapping;
3import org.springframework.web.bind.annotation.RestController;
4import org.springframework.web.context.request.async.DeferredResult;
5
6@RestController
7public class AsyncController {
8
9    private final AsyncService asyncService;
10
11    @Autowired
12    public AsyncController(AsyncService asyncService) {
13        this.asyncService = asyncService;
14    }
15
16    @GetMapping("/execute-tasks")
17    public DeferredResult<Void> executeTasks() {
18        DeferredResult<Void> deferredResult = new DeferredResult<>();
19        CompletableFuture<Void> taskOneFuture = asyncService.performTaskOne();
20        CompletableFuture<Void> taskTwoFuture = asyncService.performTaskTwo();
21        CompletableFuture<Void> taskThreeFuture = asyncService.performTaskThree();
22
23        CompletableFuture.allOf(taskOneFuture, taskTwoFuture, taskThreeFuture)
24                .thenRun(() -> deferredResult.setResult(null));
25
26        return deferredResult;
27    }
28}
复制代码

7. 测试

启动应用并访问 /execute-tasks 路径,检查控制台输出,确认任务是否并行执行。

1Executing task one in AsyncExecutor-1
2Executing task two in AsyncExecutor-2
3Executing task three in AsyncExecutor-3

 

posted on   五官一体即忢  阅读(228)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示