Last active
October 27, 2024 19:28
-
-
Save gbzarelli/a9bcf2c159028d21ad5c725028310d27 to your computer and use it in GitHub Desktop.
Java - Emitter send and wait #helpdev-blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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