I have an ExecutorService
which forwards the computed data to a CompletableFuture
:
class DataRetriever {
private final ExecutorService service = ...;
public CompletableFuture<Data> retrieve() {
final CompletableFuture<Data> future = new CompletableFuture<>();
service.execute(() -> {
final Data data = ... fetch data ...
future.complete(data);
});
return future;
}
}
I want the client/user to be able to cancel the task:
final DataRetriever retriever = new DataRetriever();
final CompletableFuture<Data> future = retriever().retrieve();
future.cancel(true);
This does not work, as this cancels the outer CompletableFuture
, but not the inner future as scheduled in the executor service.
Is it somehow possible to propagate cancel()
on the outer future to the inner future?