What’s the best way to invoke Uni in an async observer? It would be great if I could just return Uni but unfortunately that doesn’t work.
JavaScript
x
void observe(@ObservesAsync MyEvent event) {
Uni<Object> task;
}
Advertisement
Answer
As mentioned by @ladicek, you can:
- use a synchronous observer and block until termination
- use a synchronous observer and “trigger” the async operation using a fire-and-forget approach
- use an async observer (while it’s not strictly async, it just runs on another thread) and produce a
CompletionStage
1) synchronous observer and block until termination
JavaScript
void observe(@Observes MyEvent event) {
Uni<Void> uni = ;
uni.await().indefinitely();
}
2) synchronous observer and trigger the async operation using a fire-and-forget approach
JavaScript
void observe(@Observes MyEvent event) {
Uni<Void> uni = ;
uni.subscribeAsCompletionStage(); // Fire and forget, no error reporting
}
Or:
JavaScript
void observe(@Observes MyEvent event) {
Uni<Void> uni = ;
uni.subscribe().with(success -> , failure -> log(failure));
}
Async observer and produce a CompletionStage
JavaScript
void observe(@Observes MyEvent event) {
Uni<Void> uni = ;
return uni.subscribeAsCompletionStage();
}