I want to return an autocloseable object inside a CompletableFuture and use it in whenComplete without going to close it manually later.
This is the code that I've tried, but of course it won't work because it closes automatically when I return Jedis object. I haven't got any other ideas.
@Override
public CompletableFuture<Jedis> execute() {
if(!isServiceActive()) return CompletableFuture.completedFuture(null);
return CompletableFuture.supplyAsync(() -> {
try (Jedis jedis = pool.getResource()) {
return jedis;
} catch (RuntimeException e) {
logger.severe("Impossibile ottenere una nuova risorsa Jedis!", e);
throw new CompletionException(e);
}
}, executor);
}
In general, this is not possible;
CompletableFuture
is not designed for this. As you've noted, you can't close the resource before completing the future, because then it will be closed before any consumers get to act on it. That means the consumer will have to be responsible for closing the resource.However, if your goal is to only need that
Jedis
instance in the onewhenComplete
call, then perhaps there is an alternative solution. Either modifyexecute()
or create a new method that looks something like the following:The
Jedis
instance is closed by the time the future completes, but it still lets you do the needed work in theFunction
implementation. Using it would look like:Though I don't know if your code can be modified to use this approach, as I notice your
execute()
method is an override (whether from a class/interface you control, I don't know). But even if you can't modifyexecute()
directly or supply an overload feasibly, you might still be able to create a utility method that does something similar to the above.