Cluster Executor with Embedded Infinispan

What You Will Learn

How to use the Infinispan ClusterExecutor to submit tasks that run on all nodes in an embedded cluster and collect results.

Prerequisites

  • Java 17+

Step 1: Add the Embedded Dependency

Add Infinispan core to your pom.xml:

<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-core</artifactId>
</dependency>

Step 2: Create a Clustered Cache Manager

Set up a clustered cache manager. The cluster executor is obtained from the cache manager:

      // Setup up a clustered cache manager
      GlobalConfigurationBuilder global = GlobalConfigurationBuilder.defaultClusteredBuilder();
      // Initialize the cache manager
      this.cacheManager = new DefaultCacheManager(global.build());

Step 3: Submit a Task to All Nodes

Use ClusterExecutor.submitConsumer() to run a SerializableFunction on every cluster member and handle results with a TriConsumer:

   public void submitTask(SerializableFunction<EmbeddedCacheManager, Object> task, TriConsumer<Address, Object, Throwable> triConsumer) {
      ClusterExecutor clusterExecutor = cacheManager.executor();
      clusterExecutor.submitConsumer(task, triConsumer);
   }

The first argument is the task that runs on each node (it receives the local EmbeddedCacheManager). The second argument is a callback that receives the node address, the result value, and any exception.

Step 4: Run the Tutorial

mvn package exec:java

Each cluster member generates a random integer, and the results are printed on the submitting node.

What’s Next