首页 > 解决方案 > Spring-Boot @Async:如何在运行时使用@Async 找出实际的方法名称注释?

问题描述

我需要找出TaskDecorator 中的@Async 方法名称。如何从 TaskDecorator 中找出方法名称(asyncMethodWithReturnType),或者我有更好的方法来找出在 Spring Boot 2 中用 @Async 注释的实际方法?

@Bean(name = "threadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
    ThreadPoolTaskExecutor th =  new ThreadPoolTaskExecutor();
    // HERE AUDIT WOULD collect metric about method name.
    th.setTaskDecorator(runnable -> AuditWrapper.auditWrapper(runnable)) 
 
}

@Async
public Future<String> asyncMethodWithReturnType() {
    System.out.println("Execute method asynchronously - " 
      + Thread.currentThread().getName());
    try {
        Thread.sleep(5000);
        return new AsyncResult<String>("hello world !!!!");
    } catch (InterruptedException e) {
        //
    }

    return null;
}

如果您有更好的建议来找出正在执行的实际方法,请告诉我。我需要为审计/计量学目的收集方法名称。

标签: springspring-bootspring-asyncspring-boot-2

解决方案


推荐阅读