5.5. Custom Listeners for Remote Cache

Custom listeners for a remote cache can be registered in the same way as an embedded cache, with the exception that sync=false must be present. For instance:
Using only Java

from(infinispan://?cacheContainer=#cacheManager&sync=false&customListener=#myCustomListener")
  .to(mock:result);

Using Blueprint and Java

Java class:

public class RemoteCacheManagerFactory {      
    ConfigurationBuilder clientBuilder;
    public RemoteCacheManagerFactory(String hostname, int port) {
        clientBuilder = new ConfigurationBuilder();
        clientBuilder.addServer()
            .host(hostname).port(port);
    }
    public RemoteCacheManager newRemoteCacheManager() {
        return new RemoteCacheManager(clientBuilder.build());
    }
}
blueprint.xml:
<bean id=”remoteCacheManagerFactory” class=“com.jboss.datagrid.RemoteCacheManagerFactory”>  
    <argument value=”localhost”/>      
    <argument value="11222”/>      
</bean>
 
<bean id=”cacheManager”
    factory-ref=”remoteCacheManagerFactory” 
    factory-method=“newRemoteCacheManager”>   
</bean>

<bean id="myCustomListener" class="org.example.com.CustomListener"/>

<camelContext id="route" xmlns="http://camel.apache.org/schema/blueprint">
    <route>
        <from uri="infinispan://?cacheContainer=#cacheManager&sync=false&customListener=#myCustomListener"/>
        <to uri="mock:result"/>
    </route>
</camelContext>

The instance of myCustomListener must exist. Users are encouraged to extend the org.apache.camel.component.infinispan.remote.InfinispanRemoteCustomListener class and annotate the resulting class with @ClientListener; this annotation is found in org.infinispan.client.hotrod.annotation.
Remote listeners may also be associated with custom filters and converters as shown below:
@ClientListener(includeCurrentState=true, filterFactoryName = "static-filter-factory", converterFactoryName = "static-converter-factory")
  private static class MyCustomListener extends InfinispanRemoteCustomListener {
}
In order to use custom filters or converters classes annotated with @NamedFactory must be implemented. A skeleton that implements the necessary methods is shown below:
import org.infinispan.notifications.cachelistener.filter;

@NamedFactory(name = "static-converter-factory")
public static class StaticConverterFactory implements CacheEventConverterFactory {
  @Override
  public CacheEventConverter<Integer, String, CustomEvent> getConverter(Object[] params) {
    ...
  }

  static class StaticConverter implements CacheEventConverter<Integer, String, CustomEvent>, Serializable {
    @Override
    public CustomEvent convert(Integer key, String previousValue, Metadata previousMetadata, 
                               String value, Metadata metadata, EventType eventType) {
      ...
    }
  }
}
   
@NamedFactory(name = "static-filter-factory")
public static class StaticCacheEventFilterFactory implements CacheEventFilterFactory {
  @Override
  public CacheEventFilter<Integer, String> getFilter(final Object[] params) {
    ...
  }

  static class StaticCacheEventFilter implements CacheEventFilter<Integer, String>, Serializable {
    @Override
    public boolean accept(Integer key, String previousValue, Metadata previousMetadata, 
                          String value, Metadata metadata, EventType eventType) {
      ...
    }
  }
}
Custom filters and converters must be registered with the server. Registering these classes is documented in the Remote Event Listeners section of the Red Hat JBoss Data Grid Developer Guide.

Note

In order to listen for remote HotRod events the cacheManager must be of type RemoteCacheManager and instantiated.