Spring Boot @Async method in controller is executing synchronously

后端 未结 6 1709
天涯浪人
天涯浪人 2020-12-07 18:02

My [basic] Spring Boot application accepts a request from the browser, sent via jQuery.get() and is supposed to immediately receive a response - such as \"y

6条回答
  •  隐瞒了意图╮
    2020-12-07 18:27

    As code sample for @dave-syer answer:

    This works asynchronously:

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

    But this doesn't:

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

提交回复
热议问题