簡體   English   中英

Spring 4-@Scheduled工作,但@Async不工作

[英]Spring 4 - @Scheduled working but @Async not working

我有一個rest服務,該服務應該驗證一些東西並在另一個服務上調用@Async方法。 像這樣:

@RestController
public class MyRest {

    @Autowired
    private MyService myService;

    @RequestMapping(value = "/rest/do-stuff", method = RequestMethod.GET)
    public ResponseEntity<String> doStuff() {
        myService.computeAndDoStuff();
        return "ok thanks";
    }
}

@Service
public class MyService {
    @Scheduled(cron="*/5 * * * * ?")
    public void doSchedule() {
        System.out.println("I like to work");
    }

    @Async
    public void computeAndDoStuff() {
         if (1 < 2) {
             throw new RuntimeException("Indeed");
         }
         System.out.println("I hope it doesn't get printed");
    }
}

即使MyService保持工作並每5秒鍾打印一次“我喜歡工作”,但是當其余部分都被擊中“ / rest / do-stuff”時,它會返回500個http代碼,其中包含stacktrace和“ Indeed”消息。

好吧,因為我是從另一個類調用的(這里沒有代理類問題),所以我希望我的其余部分將返回200個HTTP代碼,並帶有“確定,謝謝”消息。 我想念什么?

以防萬一,我的dispatcher-servlet.xml看起來像這樣:

...
    <task:scheduler id="scheduleExec" pool-size="10" />
    <task:executor id="asynExec" pool-size="10" />
    <task:annotation-driven executor="asynExec" scheduler="scheduleExec" proxy-target-class="true" />
...

順便說一句,如果我在上面三行評論,@Scheduled停止工作(但@Async當然沒有工作:P)。

我嘗試使用您的示例,並且即使基於XML的配置也可以使用。 在調用其余端點時,我得到一個響應,說ok thanks (盡管它給了我一個編譯時錯誤,因為我的返回類型不是String,並且我試圖返回一個)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM