[jax-rs-spec users] Returning CompletionStage from a resource method

From: Pavel Bucek <>
Date: Tue, 25 Apr 2017 09:56:07 +0200

Dear experts,

I was implementing support for returning CompletionStage from a resource
method and I found some room for possible inconsistency:

When returned CompletionStage is already done, we don't need to spawn
another thread, we just use the same one. To be able to call "isDone",
the implementation must convert CompletionStage to CompletableFuture
(there is a method on the CompletionStage interface for that, so no
issues so far).

CompletableFuture has three terminal states:

- completed,
- completed exceptionally,
- cancelled.

When the state is completed, it is the same as when the resource method
would return the type directly.

When the state is completed exceptionally, it is the as when the
resource method would thrown an exception.

The question is what to do when the state is cancelled?

My initial implementation throws IllegalStateException, but when looking
to what is passed to CompletionStage.whenComplete, maybe the better way
would be to throw CancellationException. (assuming that both, ISE or CE
would be then processable by ExceptionMapper, if/when registered).

Any ideas/opinions on which exception should be thrown?

Thanks and regards,

ps.: after seeing what I wrote, I convinced myself to use
CancellationException - anyway, if anyone has other opinion, please
share it.