跳转到主内容
极星编程网:以代码为星,赴技术山海!

SpringBoot实现异步调用的全过程

目录

一、异步请求(Asynchronous Request)

二、在 Spring Boot 中,实现异步调用主要有以下几种方法:

1. 使用 @Async 注解步骤示例代码自定义线程池(可选)

调用线程池配置建议2. 使用 Java 原生线程池3. 使用 Spring 的 TaskExecutor

三、什么时候使用异步请求

四、总结

在Java中使用Spring Boot实现异步请求和异步调用是一个常见的需求,可以提高应用程序的性能和响应能力。

以下是实现这两种异步操作的基本方法:

一、异步请求(Asynchronous Request)

异步请求允许客户端发送请求后立即返回,而不需要等待服务器处理完成,异步调用允许在服务端异步执行方法,不阻塞主线程。

二、在 Spring Boot 中,实现异步调用主要有以下几种方法:

1. 使用 @Async 注解步骤启用异步支持:在主类上添加@EnableAsync。

定义异步方法:在需要异步执行的方法上使用@Async注解。

自定义线程池(可选)

:提高异步任务的线程管理效率,以便异步方法能够在独立的线程中执行示例代码

主类:

package com.work;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication(scanBasePackages = {"com.work.*"})@EnableAsync//@EnableScheduling public class SpringBootWorkApplication {public static void main(String[] args) {SpringApplication.run(SpringBootWorkApplication.class, args);}}

异步方法:

package com.work.service;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;import lombok.extern.slf4j.Slf4j;/*** 异步调用service* @author summer*/@Service@Slf4j public class AsyncService {/*** 使用 @Async 注解 实现异步调用* taskExecutor为自定义线程池,指定自定义线程池* @return* @throws InterruptedException*/@Async("taskExecutor") public void async(){log.info("async异步任务开始: " + Thread.currentThread().getName());try {// 模拟耗时操作(实际工作中,此处写业务逻辑处理) Thread.sleep(30000);} catch (InterruptedException e) {e.printStackTrace();}log.info("async异步任务完成");}}自定义线程池(可选)

package com.work.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.task.TaskExecutor;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;/*** 自定义异步线程执行线程池* @author summer**/@Configuration public class ExecutorConfig {

@Bean(name = "taskExecutor") public TaskExecutor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(5);executor.setMaxPoolSize(10);executor.setQueueCapacity(50);executor.setThreadNamePrefix("TaskExecutor-");executor.initialize();return executor;}}在@Async("taskExecutor")中指定自定义线程池。

调用package com.work.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.work.common.result.CommonResult;import com.work.service.AsyncService;import lombok.extern.slf4j.Slf4j;

/*** 测试异步执行Controller* @author summer**/@RestController@RequestMapping("/async")@Slf4j public class AsyncTestController {

@Autowired private AsyncService asyncService;

@GetMapping("/async") public CommonResult async() {asyncService.async();log.info("async异步任务调用成功");return CommonResult.success("async异步任务调用成功");}}异步方法休眠30秒,可以看到控制台打印的日志

线程池配置建议CPU 密集型任务:建议核心线程数为 CPU 核心数的n倍,最大线程数为核心线程数的 2 倍。

IO 密集型任务:建议核心线程数设置为较大的值,最大线程数可以为核心线程数的 2 倍甚至更多。

合理配置线程池可以避免线程饥饿和死锁等问题,提升系统的吞吐量。

2. 使用 Java 原生线程池Spring 提供线程池,但可以直接使用 Java 原生的线程池来实现异步。

示例代码:

package com.work.service;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import org.springframework.stereotype.Service;import lombok.extern.slf4j.Slf4j;

/*** 异步调用service* @author summer**/@Service@Slf4j public class AsyncService{

/*** 使用 Java 原生线程池来实现异步调用*/ public void asyncThreadPool() {ThreadPoolExecutor pool=new ThreadPoolExecutor(5, 10,2, TimeUnit.SECONDS, new LinkedBlockingQueue(100),new ThreadPoolExecutor.CallerRunsPolicy());pool.execute(() -> {log.info("asyncThreadPool异步任务开始: " + Thread.currentThread().getName());try {// 模拟耗时操作(实际工作中,此处写业务逻辑处理) Thread.sleep(30000);} catch (InterruptedException e) {e.printStackTrace();}log.info("asyncThreadPool异步任务完成");});}}

调用:

package com.work.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.work.common.result.CommonResult;import com.work.service.AsyncService;import lombok.extern.slf4j.Slf4j;

/*** 测试异步执行Controller* @author summer**/@RestController@RequestMapping("/async")@Slf4j public class AsyncTestController {@Autowired private AsyncService asyncService;

@GetMapping("/asyncThreadPool") public CommonResult asyncThreadPool() {asyncService.asyncThreadPool();log.info("asyncThreadPool异步任务调用成功");return CommonResult.success("asyncThreadPool异步任务调用成功");}}异步方法休眠30秒,可以看到控制台打印的日志

3. 使用 Spring 的 TaskExecutorTaskExecutor是 Spring 提供的抽象接口,适合用来执行异步任务。

配置 TaskExecutor:package com.work.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.task.TaskExecutor;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;/*** 自定义异步线程执行线程池* @author summer**/@Configuration public class ExecutorConfig {

@Bean(name = "taskExecutor") public TaskExecutor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(5);executor.setMaxPoolSize(10);executor.setQueueCapacity(50);executor.setThreadNamePrefix("TaskExecutor-");executor.initialize();return executor;}}

示例代码:

package com.work.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.task.TaskExecutor;import org.springframework.stereotype.Service;import lombok.extern.slf4j.Slf4j;/*** 异步调用service* @author summer**/@Service@Slf4j public class AsyncService{@Autowired private TaskExecutor taskExecutor;

/*** 使用 Spring 的 TaskExecutor 来实现异步调用*/ public void asyncExecutor() {taskExecutor.execute(() -> {log.info("asyncExecutor异步任务开始:" + Thread.currentThread().getName());try {// 模拟耗时操作(实际工作中,此处写业务逻辑处理) Thread.sleep(30000);} catch (InterruptedException e) {e.printStackTrace();}log.info("asyncExecutor异步任务完成");});}}

调用:

package com.work.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.work.common.result.CommonResult;import com.work.service.AsyncService;import lombok.extern.slf4j.Slf4j;

/*** 测试异步执行Controller* @author summer**/@RestController@RequestMapping("/async")@Slf4j public class AsyncTestController {@Autowired private AsyncService asyncService;

@GetMapping("/asyncExecutor") public CommonResult asyncExecutor() {asyncService.asyncExecutor();log.info("asyncExecutor异步任务调用成功");return CommonResult.success("asyncExecutor异步任务调用成功");}}异步方法休眠30秒,可以看到控制台打印的日志

三、什么时候使用异步请求

异步请求在以下情况下特别有用:长时间运行的任务:例如文件上传、复杂计算、大量数据处理等。

I/O操作:例如数据库查询、调用外部API、文件读写等。

资源密集型任务:例如图像处理、视频编码等。

四、总结

方法、优点、缺点@Async 注解简单易用,与 Spring 集成良好需要 Spring 容器管理的 Bean 才能生效ExecutorService更底层、更灵活手动管理线程池TaskExecutor Spring 提供的抽象,方便扩展配置稍显复杂这些示例展示了如何在Spring Boot中实现异步请求和异步调用。Spring Boot提供了`@Async`注解和`Java原生线程池`、`TaskExecutor`来实现这一功能,提高了应用程序的并发处理能力,开发者可以根据不同的需求选择合适的异步处理方式。

合理配置线程池,以确保最佳性能和资源利用,正确处理异步任务中的异常,以及在合适的场景中应用异步处理技术,是开发高性能应用的关键。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:SpringBoot中使用@Async实现异步调用​方法示例SpringBoot异步调用相同类的解决方案SpringBoot实现异步调用的方法示例SpringBoot中实现异步调用@Async详解SpringBoot中使用@Async实现异步任务调用详解SpringBoot中异步调用时的注意事项

相关文章