Apache Camel and JBoss Data Grid Integration

Apache Camel is a framework to develop integration solutions. JBoss Data Grid is a distributable key/value cache. Both solutions are part of Red Hat middleware portfolio. JBoss Data Grid as a standalone product and Apache Camel as part of JBoss Fuse integration solution.

The integration between both products is through a supported Apache Camel component. The name of the component is camel-jbossdatagrid.

Here is a example of how to use this component to connect to a remote JBoss Data Grid instance(s). First, import the component dependency in your pom.xml.

<dependency>
 <groupId>org.apache.camel</groupId>
 <artifactId>camel-jbossdatagrid</artifactId>
 <version>2.17.0.Final-redhat-2</version>
</dependency>

This dependency isn’t in the default Maven repository. You must configure the Red Hat Maven repositories. You can go to this url to see how it’s done: https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html-single/development_guide/#use_the_maven_repository

Now you must develop a factory method that instantiate a RemoteCacheManager. This class is part of Hot Rod API; Hot Rod is the default API to connect to remote JBoss Data Grid instances.

package com.angelogalvao.datagrid.example;

import java.util.Map;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;

public class RemoteCacheManagerFactory {

   private RemoteCacheManager cacheManager; 
   private String[] hosts; 

   public RemoteCacheManagerFactory(String hosts) { 
      if( hosts == null ) 
         throw new IllegalArgumentException("Hosts is null"); 

      this.hosts = hosts.split(";"); 
   } 

   public RemoteCacheManager getRemoteCacheManager() { 
      if(cacheManager != null) 
         return cacheManager; 

      // Create the RemoteCacheManager 
      ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); 

      for (int i = 0; i < hosts.length; i++) { 
         String host = hosts[i]; 
         String[] hostConfig = host.split(":"); 
         configurationBuilder 
            .addServer() 
               .host(hostConfig[0]) 
               .port(Integer.parseInt(hostConfig[1])); 
      } 

      cacheManager = new RemoteCacheManager(configurationBuilder.build()); 
      return cacheManager; 
   } 
}

Configure the factory component in Spring/Blueprint context.

<bean class="com.angelogalvao.datagrid.example.RemoteCacheManagerFactory" id="remoteCacheManagerFactory">
    <argument value="localhost:11222"/>
</bean>

<bean id="cacheManager" factory-ref="remoteCacheManagerFactory" factory-method="getRemoteCacheManager"/>

Finally, create the Apache Camel routes to access the JBoss Data Grid. The example above have 2 routes: one for GET the value and other to PUT the value.

<camelContext id="example-camel-context" xmlns="http://camel.apache.org/schema/blueprint">
   <route id="rota-datagrid-get" autoStartup="true" > 
      <from uri="timer://foo?fixedRate=true&amp;period=5000"/>
      <setHeader headerName="CamelInfinispanKey">
         <constant>TEST</constant>            
      </setHeader>
      <to uri="infinispan://localhost?cacheContainer=#cacheManager&amp;cacheName=redhat-test&amp;command=GET"/> 
      <setBody> 
         <simple>${header.CamelInfinispanOperationResult}</simple> 
      </setBody> 
      <log message="TEST VALUE: ${body}"/>
   </route>
   <route id="rota-datagrid-put" autoStartup="true"> 
      <from uri="timer://foo?fixedRate=true&amp;period=5000"/>
      <setHeader headerName="CamelInfinispanKey">
         <constant>TEST</constant>
      </setHeader>
      <setHeader headerName="CamelInfinispanValue">
         <constant>REDHAT</constant>
      </setHeader>
      <to uri="infinispan://localhost?cacheContainer=#cacheManager&amp;cacheName=redhat-test&amp;command=PUT"/>
      <log message="PUT - TEST is REDHAT"/>
   </route>
</camelContext>

If you are using JBoss Fuse, don’t forget to enable camel-jbossdatagrid component, running this command in JBoss Fuse console:

JBoss Fuse...> osgi:install -s mvn:org.apache.camel:camel-jbossdatagrid:2.17.0.Final-redhat-2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s