Spring @Async 典型用法

典型用法

⚠️ 注意:@Async 方法不能是 private,也不能在同一个类中直接调用,否则代理失效。

基础用法:异步发送邮件

在配置类上添加 @EnableAsync 来启用异步功能,方法被 @Async 标记后,Spring 会自动将其放入线程池中异步执行。

java 复制代码
// 启用异步支持(必须配置)
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class AsyncConfig {
}

--------------------------------------------------------------
// 异步发送邮件
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    @Async
    public void sendEmail(String to, String content) {
        // 模拟耗时操作
        System.out.println("正在异步发送邮件给:" + to);
        try {
            Thread.sleep(1000); // 模拟发送耗时
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

自定义线程池(推荐做法)

默认使用的是 Spring 的简单线程池,建议自定义以提高性能和可控性。

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

import java.util.concurrent.Executor;

@Configuration
public class CustomAsyncConfig {

    @Bean(name = "emailTaskExecutor")
    public Executor emailTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("Email-Async-");
        executor.initialize();
        return executor;
    }
}

然后在服务中指定使用该线程池:

java 复制代码
@Async("emailTaskExecutor")
public void sendEmail(String to, String content) {
    // 发送逻辑
}

异常处理(推荐封装)

异步方法中的异常不会直接抛出到主线程,需手动捕获或使用 try-catch 处理,也可以结合 @ControllerAdvice 或日志框架统一处理。

java 复制代码
@Async
public void doSomething() {
    try {
        // 执行任务
    } catch (Exception e) {
        // 记录日志或上报监控
        System.err.println("异步任务发生异常:" + e.getMessage());
    }
}

返回值类型支持

java 复制代码
// void 类型(无返回值)
@Async
public void logAccess(String user) {
    // 记录用户访问日志
}

//  Future / CompletableFuture(有返回值)
@Async
public Future<String> fetchData() {
    String result = "数据加载完成";
    return new AsyncResult<>(result);
}
相关推荐
Old Uncle Tom19 分钟前
提示词编写规范
数据库·算法
l1t21 分钟前
DeepSeek总结的Postgres 扩展天花板:当一个实例试图包揽一切时
数据库·postgresql
我要升天!33 分钟前
C语言连接 MySQL:libmysqlclient 获取方式详解
c语言·开发语言·数据库·mysql·adb
A-Jie-Y1 小时前
JAVA23种设计模式
java·设计模式
小同志001 小时前
IoC 详解
java·开发语言
BENA ceic1 小时前
Java进阶-在Ubuntu上部署SpringBoot应用
java·spring boot·ubuntu
juniperhan1 小时前
Flink 系列第17篇:Flink Table&SQL 核心概念、原理与实战详解
大数据·数据仓库·分布式·sql·flink
Irene19911 小时前
SQL 中的大小写规则总结:关键字、函数名不区分大小写(建议大写),字符串值、日期格式符严格区分大小写
sql·大小写规范
asdfg12589631 小时前
以生活例子理解编程中的“多态”
java·生活·多态