Event listeners can be used with Infinispan caches to notify remote clients of events happening on the Cache, such as CacheEntryCreated, CacheEntryModified or CacheEntryRemoved. In this tutorial we've learnt how to monitor a local embedded Infinispan cache: Monitoring WildFly's Infinispan caches.

The following example shows how a remote client, using the Hot Rod protocol, is able to listen and print events happening on the remote cache:

import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.annotation.ClientCacheEntryCreated;
import org.infinispan.client.hotrod.annotation.ClientCacheEntryModified;
import org.infinispan.client.hotrod.annotation.ClientListener;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.event.ClientCacheEntryCreatedEvent;
import org.infinispan.client.hotrod.event.ClientCacheEntryModifiedEvent;
import org.infinispan.client.hotrod.impl.ConfigurationProperties;
public class RemoteListenerDemo {
   public static void main(String[] args) throws InterruptedException {
      ConfigurationBuilder builder = new ConfigurationBuilder();
      RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
      RemoteCache<String, String> cache = cacheManager.getCache();
      BasicListener listener = new BasicListener();
      cache.put("entry1", "value1");
      cache.put("entry2", "value2");
      cache.put("entry3", "value3");
   public static class BasicListener {
      public void entryCreated(ClientCacheEntryCreatedEvent<String> event) {
         System.out.printf("Created %s%n", event.getKey());
      public void entryModified(ClientCacheEntryModifiedEvent<String> event) {
         System.out.printf("Going to modify %s%n", event.getKey());

The expected output will be:


Please note that the actual value is not sent back to the client for performance reasons. As a matter of fact, receiving remote events has a performance impact, which increases as the cache size grows. In order to avoid bottlenecks to Hot Rod remote clients, it is recommended to filter remote events on the server side.


Related articles available on mastertheboss.com

Getting Started with Infinispan - Part 1

This tutorial has been updated to work with the lastes Infinispan

Infinispan tutorial part 2

In the first tutorial we have covered some basic concepts about I

Infinispan RESTful interface

This is an update for Infinispan REST tutorial which is based on

Develop a clustered application with Infinispan Data Grid

In this tutorial we will use Infinispan Data Grid Platform to sto

Using Infinispan with WildFly

The Infinispan subsystem provides caching support for HA services

Using Infinispan Query API

Applications using a NoSQL storage often need to query data using