Skip to content

Instantly share code, notes, and snippets.

@cescoffier
Last active July 14, 2021 12:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cescoffier/e8c8a18897f9e5ca15f1378876a1bd93 to your computer and use it in GitHub Desktop.
Save cescoffier/e8c8a18897f9e5ca15f1378876a1bd93 to your computer and use it in GitHub Desktop.
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.smallrye.reactive:smallrye-mutiny-vertx-redis-client:1.1.0
//DEPS io.smallrye.reactive:mutiny:0.7.0
//DEPS org.testcontainers:testcontainers:1.14.3
//DEPS org.slf4j:slf4j-nop:1.7.30
package io.vertx.mutiny.redis;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.mutiny.core.Vertx;
import org.testcontainers.containers.GenericContainer;
public class RedisAndMutiny {
public static void main(String[] args) {
GenericContainer<?> container = new GenericContainer<>("redis:latest")
.withExposedPorts(6379);
container.start();
Vertx vertx = Vertx.vertx();
RedisClient redis = RedisClient.create(vertx, new JsonObject()
.put("port", container.getMappedPort(6379))
.put("host", container.getContainerIpAddress()));
JsonObject luke = new JsonObject()
.put("name", "Luke Skywalker")
.put("birth_year", "19BBY");
JsonObject c3p0 = new JsonObject()
.put("name", "C-3PO")
.put("birth_year", "112BBY");
JsonObject r2d2 = new JsonObject()
.put("name", "R2-D2")
.put("birth_year", "33BBY");
JsonObject vader = new JsonObject()
.put("name", "Darth Vader")
.put("birth_year", "41.9BBY");
Uni.combine().all().unis(
redis.hmset("1", luke),
redis.hmset("2", c3p0),
redis.hmset("3", r2d2),
redis.hmset("4", vader)
).asTuple()
.await().indefinitely();
// Item inserted
Uni<JsonArray> result = redis.keys("*")
.onItem().transformToMulti(keys -> Multi.createFrom().iterable(keys))
.onItem().castTo(String.class)
.onItem().transformToUniAndMerge(key -> redis.hgetall(key))
.collectItems().in(() -> new JsonArray(), (arr, obj) -> arr.add(obj));
System.out.println(result.await().indefinitely());
vertx.closeAndAwait();
container.stop();
}
}
@maxandersen
Copy link

cool - I recommend to put //DEPS org.slf4j:slf4j-nop:1.7.30 or similar in then you don't get the ~5-8 lines of warnings about sl4j!

@cescoffier
Copy link
Author

Thanks for the suggestion @maxandersen. I've added the dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment