控制器中的弹簧启动@Async方法同步执行

我的[基本]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.";
  }
}

该类实现了一个接口,非常简单。它具有执行耗时任务的单一方法。我不需要从此服务呼叫中返回任何内容,因为对于失败或成功的情况,将在工作结束时发送一封电子邮件。因此,它实际上看起来像这样:DoWorkServiceImplDoWorkService

@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秒钟。因此,控制器映射方法似乎正在调用同步注释的内部方法。在传统的Spring应用程序中,我通常将其添加到XML配置中:@Async

<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


答案 1

您正在从同一类中的另一个方法调用该方法。除非您为不起作用的(当然并提供编织器)启用AspectJ代理模式(google“代理自调用”)。最简单的解决方法是将该方法放在另一个 .@Async@EnableAsync@Async@Bean


答案 2

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

下面是一个包含@Async的简单示例。请按照以下步骤获取在 Spring Boot 应用程序中工作@Async:

步骤 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());
        }
    }
}

步骤 3:在控制器中添加 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


推荐