Skip to content

Instantly share code, notes, and snippets.

Last active March 16, 2022 14:29
What would you like to do?
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.vertx:vertx-rx-java3:4.2.5
//DEPS io.vertx:vertx-web-client:4.2.5
import io.reactivex.rxjava3.core.Single;
import io.vertx.rxjava3.core.Vertx;
import io.vertx.rxjava3.ext.web.client.HttpResponse;
import io.vertx.rxjava3.ext.web.client.WebClient;
import java.util.AbstractMap;
import java.util.concurrent.CountDownLatch;
import static java.lang.String.format;
import static java.lang.System.out;
public class Services {
public static void main(String... args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
// Vertx instance and web client
Vertx vertx = Vertx.vertx();
WebClient webClient = WebClient.create(vertx);
// single sources. Lazy evaluation, no invocation at this point
Single<Integer> service1Code = service1(webClient);
Single<Integer> service2Code = service2(webClient);
// combine results together and create tuple
Single<AbstractMap.SimpleEntry<Integer, Integer>> tupleSource =
service1Code.zipWith(service2Code, (s1, s2) -> new AbstractMap.SimpleEntry<>(s1, s2));
// subscribe and invoke services
vertx.setPeriodic(1000, l -> out.println("[" + Thread.currentThread().getName() + "] is released"));
private static Single<Integer> service1(WebClient webClient) {
return webClient.getAbs("")
.doOnSuccess(response -> out.println("[" + Thread.currentThread().getName() + "] service 1: response received"))
private static Single<Integer> service2(WebClient webClient) {
return webClient.getAbs("")
.doOnSuccess(response -> out.println("[" + Thread.currentThread().getName() + "] service 2 response received"))
private static void printResult(AbstractMap.SimpleEntry<Integer, Integer> tuple){
"[%s] Result: service1:%d service2:%d",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment