Providing a timeout value when using @Async for a method using Spring 3.0

前端 未结 2 736
自闭症患者
自闭症患者 2020-12-14 21:42

I looked through the documentation but couldn\'t find if there is a way to specify a timeout for async operations spawned when using @Async annotated methods using Spring 3.

相关标签:
2条回答
  • 2020-12-14 21:53

    Timeouts are not provided by the @Async annotation, since the timeout should be decided by the caller of the function, not the function itself.

    I'm assuming you're referring to the timeout on an @Async-annotated method which returns a result. Such methods should return an instance of Future, and the get() method on Future is used to specify the timeout.

    e.g.

    @Async
    public Future<String> doSomething() {
       return new AsyncResult<String>("test");
    }
    

    and then

    Future<String> futureResult = obj.doSomething();  // spring makes this an async call
    String result = futureResult.get(1, TimeUnit.SECOND);
    
    0 讨论(0)
  • 2020-12-14 22:09

    In @Async source code is no option for configuration.

    0 讨论(0)
提交回复
热议问题