I want to be able to cancel a method marked with the @Async
annotation by it's future.
I have a Spring method marked with the @Async
annotation. This method does some computation, and eventually returns a result. All examples I have seen recommend using the AsyncResult
class to return this Future.
@Async
public Future<String> run() {
// ... Computation. Minutes pass ...
return new AsyncResult<String>("Result");
}
I call the following method, from another Component, in the following manner. For example purposes, I wish to immediately cancel this thread:
Future<String> future = component.run();
future.cancel(true);
In this case, the thread will never be cancelled. This is because, looking at the Spring implementation for AsyncResult
here: https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncResult.java#L71 , this method doesn't actually do a single thing. It simply returns false, that the thread could not be cancelled. This is my problem. How can I cancel the Thread being created by the @Async
method? I have no access to the internal thread being created by Spring - so I have no means myself with which to cancel it, do I?