Reactive API with Mutiny

What You Will Learn

How to use the Infinispan Mutiny API to perform non-blocking, reactive cache operations. Instead of the traditional RemoteCacheManager, this tutorial uses the Infinispan API entry point to obtain a MutinyCache.

Prerequisites

  • Java 17+

  • An Infinispan Server running on localhost:11222 (or Docker/Podman available for Testcontainers)

Start an Infinispan Server with Docker or Podman:

docker run -it --rm -p 11222:11222 -e USER=admin -e PASS=password quay.io/infinispan/server:latest
Tip
You can replace docker with podman in the command above if you use Podman.
Tip
If no server is running, the tutorial code automatically starts an Infinispan Server using Testcontainers.

Step 1: Connect with the Infinispan API

Use the Infinispan.create() factory to establish a connection:

      ConfigurationBuilder configurationBuilder = TutorialsConnectorHelper.connectionConfig();
      try {
         infinispan = Infinispan.create(configurationBuilder.create());

Step 2: Obtain a Mutiny Cache

Get a MutinyCache from the Mutiny API:

      cache = infinispan.mutiny()
            .caches().<String, String>get(TUTORIAL_CACHE_NAME)
            .await().atMost(Duration.ofMillis(100));

Step 3: Chain Reactive Operations

Use Mutiny operators to chain cache operations in a non-blocking pipeline:

      cache.set("hello", "reactive")
              .chain(ignore -> cache.get("hello"))
              .onItem().invoke(v -> System.out.printf("%s -- %s\n", LocalDateTime.now(), v))
              .map(v -> v + " is nice!!")
              .onItem().delayIt().onExecutor(executor).by(Duration.ofSeconds(1))
              .invoke(v -> System.out.printf("%s -- %s\n", LocalDateTime.now(), v))
              .await().atMost(Duration.ofSeconds(2));

This pipeline sets a value, reads it back, transforms it, adds a delay, and prints the result — all as a reactive chain.

Step 4: Run the Tutorial

mvn package exec:java

You should see two timestamped lines showing the reactive operations executing with a one-second delay between them.

What’s Next