在Spring Boot中实现异步请求、并行执行

在Spring Boot中实现异步请求、并行执行,可以使用@Async注解来定义异步方法,同时使用FutureCompletableFuture或其他异步处理机制来处理异步结果和回调。以下是实现这一功能的几个步骤:

开启异步支持

首先,需要在Spring Boot应用中开启异步支持。这可以通过在一个配置类上添加@EnableAsync注解来实现。AsyncConfig 类开启异步支持并自定义Executor

java 复制代码
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(5);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("Async-");
        executor.initialize();
        return executor;
    }
}

定义异步服务

然后,定义一个服务类,并在其中的方法上使用@Async注解来标记它们为异步方法。这里可以返回FutureCompletableFutureListenableFuture等,以便处理异步结果。

java 复制代码
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
public class AsyncService {

    @Async
    public CompletableFuture<String> processAsync() {
        // 模拟异步任务
        try {
            Thread.sleep(1000);
            return CompletableFuture.completedFuture("单个任务执行完成");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return CompletableFuture.failedFuture(e);
        }
    }

    @Async
    public CompletableFuture<String> processAsyncParallel(int taskNumber) {
        // 模拟异步任务
        try {
            Thread.sleep(1000); // 假设这是耗时操作
            return CompletableFuture.completedFuture("任务 #" + taskNumber + " 执行完成");
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return CompletableFuture.failedFuture(e);
        }
    }

    public List<CompletableFuture<String>> executeTasksInParallel() {
        List<CompletableFuture<String>> futures = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            futures.add(processAsyncParallel(i));
        }
        return futures;
    }
}

在Controller中调用并行执行方法并等待所有任务完成

AsyncController中,我们将添加一个映射来启动并行任务并等待它们全部完成。

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@RestController
public class AsyncController {

    @Autowired
    private AsyncService asyncService;

    @GetMapping("/startParallelTasks")
    public String startParallelTasks() {
        List<CompletableFuture<String>> futures = asyncService.executeTasksInParallel();

        // 等待所有任务完成
        CompletableFuture<Void> allDoneFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
                .thenApply(v -> futures.stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList()));

        allDoneFuture.thenAccept(results -> results.forEach(System.out::println));

        return "并行任务开始执行";
    }

    @GetMapping("/waitForSpecificTask")
    public String waitForSpecificTask() throws Exception {
        CompletableFuture<String> future = asyncService.processAsync();

        // 等待特定任务完成
        String result = future.get(); // 阻塞直到任务完成

        return "特定任务完成,结果:" + result;
    }
}

说明

  • executeTasksInParallel方法在AsyncService中启动了多个并行的异步任务。这些任务通过CompletableFuture并行执行。
  • startParallelTasks中,我们使用CompletableFuture.allOf来等待所有任务完成。allOf返回一个新的CompletableFuture,该CompletableFuture在所有给定的CompletableFutures完成时完成。
  • waitForSpecificTask方法演示了如何等待一个特定的异步任务完成。使用CompletableFuture.get()方法可以阻塞当前线程直到异步任务完成,并获取结果。

代码示例展示了如何在Spring Boot应用中并行执行多个异步任务,并等待特定或所有任务的完成,充分利用@Async注解和CompletableFuture的能力来实现高效的异步编程模式。

相关推荐
曼岛_20 分钟前
[Java实战]Spring Boot + Netty 实现 TCP 长连接客户端及 RESTful 请求转发(二十六)
java·spring boot·tcp/ip
老友@26 分钟前
Spring Data Elasticsearch 中 ElasticsearchOperations 构建查询条件的详解
java·后端·spring·elasticsearch·operations
NaclarbCSDN27 分钟前
Java集合框架
java·开发语言·前端
蟹至之37 分钟前
万字解析:Java字符串
java·字符串·stringbuilder·string·stringbuffer
果冻kk1 小时前
【实战教程】从零实现DeepSeek AI多专家协作系统 - Spring Boot+React打造AI专家团队协作平台
人工智能·spring boot·react.js
CircleMouse1 小时前
springboot如何通过提供的注解方式来操作Redis
java·spring boot·redis·spring·mybatis
阿海打酱油1 小时前
idea经常卡顿解决办法
java·ide·intellij-idea
bing_1581 小时前
Spring Boot 项目中什么时候会抛出 FeignException?
java·spring boot·后端
Java&Develop2 小时前
springboot + mysql8降低版本到 mysql5.7
java·spring boot·后端
sg_knight2 小时前
从单体架构到微服务:架构演进之路
java·spring boot·spring·spring cloud·微服务·云原生·架构