繁体   English   中英

控制器中的 Spring Boot @Async 方法正在同步执行

[英]Spring Boot @Async method in controller is executing synchronously

我的 [basic] Spring Boot 应用程序接受来自浏览器的请求,通过jQuery.get()发送并应该立即收到响应 - 例如“您的请求已排队”。 为此,我编写了一个控制器:

@Controller
public class DoSomeWorkController {

  @Autowired
  private final DoWorkService workService;

  @RequestMapping("/doSomeWork")
  @ResponseBody
  public String doSomeWork() {

    workService.doWork(); // time consuming operation
    return "Your request has been queued.";
  }
}

DoWorkServiceImpl类实现了一个DoWorkService接口并且非常简单。 它有一个单一的方法来执行一项耗时的任务。 我不需要此服务调用返回的任何内容,因为将在工作结束时发送一封电子邮件,无论是失败还是成功场景。 所以它实际上看起来像:

@Service
public class DoWorkServiceImpl implements DoWorkService {

  @Async("workExecutor")
  @Override
  public void doWork() {

    try {
        Thread.sleep(10 * 1000);
        System.out.println("completed work, sent email");
    }
    catch (InterruptedException ie) {
        System.err.println(ie.getMessage());
    }
  }
}

我认为这会起作用,但浏览器的 Ajax 请求在返回响应之前等待了 10 秒。 因此,控制器映射方法正在同步调用用@Async注释的内部方法,看起来。 在传统的 Spring 应用程序中,我通常将其添加到 XML 配置中:

<task:annotation-driven />
<task:executor id="workExecutor" pool-size="1" queue-capacity="0" rejection-policy="DISCARD" />

所以我认为在主应用程序类中编写与此等效的内容会有所帮助:

@SpringBootApplication
@EnableAsync
public class Application {

  @Value("${pool.size:1}")
  private int poolSize;;

  @Value("${queue.capacity:0}")
  private int queueCapacity;

  @Bean(name="workExecutor")
  public TaskExecutor taskExecutor() {
      ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
      taskExecutor.setMaxPoolSize(poolSize);
      taskExecutor.setQueueCapacity(queueCapacity);
      taskExecutor.afterPropertiesSet();
      return taskExecutor;
  }

  public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
  }
}

这并没有改变行为。 Ajax 响应在发送请求 10 秒后仍然到达。 我错过了什么?

Spring Boot 应用程序可以在这里下载 安装 Maven 后,可以使用简单的命令运行该项目:

mvn clean spring-boot:run

注意由于下面@Dave Syer 提供的答案,该问题得到了解决,他指出我的应用程序中缺少@EnableAsync ,即使我在上面的代码片段中有一行。

您正在从同一类中的另一个方法调用@Async方法。 除非您为@EnableAsync启用 AspectJ 代理模式(当然还提供编织器),否则将无法工作(谷歌“代理自调用”)。 最简单的解决方法是将@Async方法放在另一个@Bean

对于所有仍在寻找以简单方式解释的@Asnyc 中所有步骤的人,这里是答案:

这是@Async 的一个简单示例。 按照以下步骤让 @Async 在您的 Spring Boot 应用程序中工作:

步骤1:添加@EnableAsync注解并将TaskExecutor Bean添加到应用程序类。

例子:

@SpringBootApplication
@EnableAsync
public class AsynchronousSpringBootApplication {

    private static final Logger logger = LoggerFactory.getLogger(AsynchronousSpringBootApplication.class);

    @Bean(name="processExecutor")
    public TaskExecutor workExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setThreadNamePrefix("Async-");
        threadPoolTaskExecutor.setCorePoolSize(3);
        threadPoolTaskExecutor.setMaxPoolSize(3);
        threadPoolTaskExecutor.setQueueCapacity(600);
        threadPoolTaskExecutor.afterPropertiesSet();
        logger.info("ThreadPoolTaskExecutor set");
        return threadPoolTaskExecutor;
    }

    public static void main(String[] args) throws Exception {
  SpringApplication.run(AsynchronousSpringBootApplication.class,args);
 }
}

第 2 步:添加执行异步进程的方法

@Service
public class ProcessServiceImpl implements ProcessService {

    private static final Logger logger = LoggerFactory.getLogger(ProcessServiceImpl.class);

    @Async("processExecutor")
    @Override
    public void process() {
        logger.info("Received request to process in ProcessServiceImpl.process()");
        try {
            Thread.sleep(15 * 1000);
            logger.info("Processing complete");
        }
        catch (InterruptedException ie) {
            logger.error("Error in ProcessServiceImpl.process(): {}", ie.getMessage());
        }
    }
}

第三步:在Controller中添加API来执行异步处理

@Autowired
private ProcessService processService;

@RequestMapping(value = "ping/async", method = RequestMethod.GET)
    public ResponseEntity<Map<String, String>> async() {
        processService.process();
        Map<String, String> response = new HashMap<>();
        response.put("message", "Request is under process");
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

我还在 GitHub 上用这些步骤写了一个博客和一个工作应用程序。 请查看: http : //softwaredevelopercentral.blogspot.com/2017/07/asynchronous-processing-async-in-spring.html

我有一个类似的问题,我在正确的 bean 中有 @Async 和 @EnableAsync 注释,但该方法仍在同步执行。 在我检查日志后,有一条警告说我有多个 ThreadPoolTask​​Executor 类型的 bean,但没有一个 bean 称为taskExecutor所以......

@Bean(name="taskExecutor")
public ThreadPoolTaskExecutor defaultTaskExecutor() {
     ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
     //Thread pool configuration
     //...
     return pool;
}

有关线程池可用的配置,请参阅http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/concurrent/ThreadPoolTask​​Executor.html

遵循三个步骤:

1 步:将@EnableAsync 与@configuration 或@SpringBootApplication 一起使用

@EnableAsync 公共类应用程序 {

2 步:

/**
 * THIS FOR ASYNCRONOUS PROCESS/METHOD
 * @return
 */
@Bean
public Executor asyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(5);
    executor.setQueueCapacity(500);
    executor.setThreadNamePrefix("Asynchronous Process-");
    executor.initialize();
    return executor;
}

第 3 步:将 @Async 放在预期的方法上

作为@dave-syer 答案的代码示例:

这是异步工作的:

private void longRunning() {
    try {
        log.info("wait 3 seconds");
        Thread.sleep(3000);
    } catch (InterruptedException e1) {
    }
    log.info("done");               
}

@Async  
@Override
public void doWork() {
    longRunning();
}

但这不会:

@Async
private void longRunning() {
    try {
        log.info("wait 3 seconds");
        Thread.sleep(3000);
    } catch (InterruptedException e1) {
    }
    log.info("done");               
}

@Override
public void doWork() {
    longRunning();
}

我使用 spring-boot 主类来定义异步配置 @EnableAsync注释使 Spring 能够在后台线程池中运行@Async方法。 这个类还通过定义一个新的 bean 来自定义 Executor。 此处,该方法名为taskExecutor() ,因为这是 Spring 搜索的特定方法名称。

Spring-Boot-Application.class

@SpringBootApplication
@EnableAsync
public class AsyncApplication {

    @Value("${config.threadpool.corepool.size}")
    private Integer corePoolSize;

    @Value("${config.threadpool.maxpool.size}")
    private Integer maxPoolSize;

    public static void main(String[] args) {
        SpringApplication.run(AsyncApplication.class, args);
    }

    //name of below method should not be changed.
    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        //other proeprties to be set here
        executor.setThreadNamePrefix("ASYNC-");
        executor.initialize();
        return executor;
    }
}

在实现中,在方法级别使用@Async使方法异步。 方法需要公开才能使用@Async 此外,调用@Async方法的@Async注释方法将不起作用。

以下示例实现供参考 -

@Async
  public void updateData(String userId) throws ApplicationException {
    logger.info("Updating details for User with {}", userId);
    //your code goes here...
  }

配置属性在application.properties文件中定义

#Core Pool Size for Async
config.threadpool.corepool.size=100
#Max Pool Size for Async
config.threadpool.maxpool.size=400   

关于如何定义池的规则,请参考rules-of-a-threadpool-executor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM