Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

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

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.0.

Is there a way to do that? I think this is pretty essential whenever making triggering an async computation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...