Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Created August 26, 2022 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbzarelli/a9bcf2c159028d21ad5c725028310d27 to your computer and use it in GitHub Desktop.
Save gbzarelli/a9bcf2c159028d21ad5c725028310d27 to your computer and use it in GitHub Desktop.
Emitter send and wait
private final Emitter<MyObject> emitter;
@Override
public void sendingAndWait(final MyObject payload) {
final var future = new CompletableFuture<Void>();
final var message = Message.of(payload,
() -> success(future),
(reason) -> failure(future, reason));
emitter.send(message);
future.toCompletableFuture().join();
}
private CompletableFuture<Void> success(final CompletableFuture<Void> future) {
future.complete(null);
return CompletableFuture.completedFuture(null);
}
private CompletableFuture<Void> failure(final CompletableFuture<Void> future,
final Throwable reason) {
future.completeExceptionally(reason);
return CompletableFuture.completedFuture(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment