{{announcement.body}}
{{announcement.title}}

Perform Remote Caches With JBoss Fuse and Red Hat Data Grid

DZone 's Guide to

Perform Remote Caches With JBoss Fuse and Red Hat Data Grid

We will demonstrate the use of the JBoss Fuse integration platform, in conjunction with the In-memory database named Red Hat Data Grid.

· Big Data Zone ·
Free Resource

Nowadays, the reality of companies demands even more integration between different technologies, working together, changing, and processing data. In this ecosystem, the use of an integration platform is essential. Still, the use of an in-memory database is necessary for a significant gain in information processing performance. In this article, we will demonstrate the use of the JBoss Fuse integration platform, in conjunction with the In-memory database named  Red Hat Data Grid.

JBoss Fuse is an integration platform based on the framework Apache Camel. It focuses on mediation, transformation, and data routing among many applications, services, and devices. 

Red Hat Data Grid, or Datagrid, is an in-memory data management system that uses the concept of key-value based on the open-source product Infinispan. Red Hat Data Grid is usable as a simple cache solution, a NoSql database, or even an event broker. We can assume that Red Hat Data Grid is the enterprise version, and the Infinispan is the community version of the product. This article is about how JBoss Fuse can be used as a middleware solution to communicate with Red Hat Data Grid, being this one configured as a remote cache server.

In the documentation of versions 6.3 and 7.1 of JBoss Fuse, we can find references to a component called camel-jbossdatagrid, which is no longer supported by Red Hat and that has been replaced by the component camel-infinispan in the latest versions.

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>org.apache.camel</groupId>
3
    <artifactId>camel-infinispan</artifactId>
4
     <version>${camel.version}</version>
5
</dependency> 



To demonstrate the integration between JBoss Fuse and Red Hat Data Grid, we created a quickstart project and in this article, we detail how this integration is completed. The source code of this project is available for download at https://git.io/Je1J2.

For this project, we used JBoss Fuse Standalone version 7.4 that uses the Apache Camel 2.21 and Red Hat Data Grid 7.4 that is equivalent to Infinispan version 9.4.

Beyond standalone mode, JBoss Fuse can be executed in the EAP, KARAF, and Openshift. All concepts applied here are compatible with any mode of use.

First Steps

To connect to Red Hat Data Grid, the first step is to create an instance of an object of type ‘RemoteCacheManager’. This object is responsible for storing the configurations to access the remote server and managing the communication with it.  

In the example below, we can visualize the implementation of ‘RemoteCacheManager’ configured in our example project. Basically, it is a java class that uses the class ‘ConfigurationBuilder’ to store the configurations of our Red Hat Data Grid.

Java
 




xxxxxxxxxx
1
22


1
@Component
2
public class RemoteCacheConfig {
3
        
4
    private  String rhdgHosts = "127.0.0.1:11222";  
5
    private  Integer socketTimeout = 5000;      
6
    private  Integer connecionTimeout = 5000;       
7
    private  Integer maxRetries = 10;
8
    
9
    @Bean
10
    public RemoteCacheManager cacheContainer() {
11
             
12
        ConfigurationBuilder builder =  new ConfigurationBuilder();
13
            builder.addServers(rhdgHosts);
14
            builder.socketTimeout(socketTimeout);
15
            builder.connectionTimeout(connecionTimeout);
16
            builder.maxRetries(maxRetries);        
17
            builder.connectionPool().minIdle(10);
18
            builder.connectionPool().maxIdle(50);
19
             
20
        return new RemoteCacheManager(builder.build()); 
21
     }  
22
}



 Basic methods for this connection are:

addServers

Adds a list of remote servers in the form: host1[:port][;host2[:port]].

Ex: 192.168.0.4:11222;192.168.0.5:11222.

socketTimeout

This property defines the maximum socket read timeout in milliseconds before giving up waiting for bytes from the server. Defaults to 60000 (1 minute)

connectionTimeout

This property defines the maximum socket connect timeout before giving up connecting to the server.


maxRetries Sets the maximum number of retries for each request. A valid value should be greater or equals than 0 (zero). Zero means no retry will be made in case of a network failure. It defaults to 10.

With this configuration, the component camel-infinispan is ready to connect and to make operations like insert, read and remove the key in Red Hat Data Grid using the Hot Rod protocol.

This protocol is used to accelerate client access and server interactions faster than other protocols based on text and also allowing clients to make smarter decisions for load balance, failover, and data query.

Before continuing, it is important to understand the syntax of an Infinispan endpoint (Datagrid).  To create producers and consumers, the component uses the following syntax:

Java
 




xxxxxxxxxx
1


1
infinispan://cacheName?[options]



To send a key, the URI will be like the example:

Java
 




xxxxxxxxxx
1


1
.to(infinispan:cacheCustomers?cacheContainer=#remoteCacheManagerExample")



In the example above, we are sending data to a remote cache called ‘cacheCustomers’, and the settings required for this submission are in the class called ‘remoteCacheManagerExample’. RemoteCacheManager is the equivalent of a dataSource for JDBC or JPA connections.

Several configuration options can be passed through the URI, and these options can be found on the component's website, within the 'The Apache Software Foundation' portal at https://camel.apache.org/components/latest/infinispan-component.html.


Inserting a Key

In our project, we created a webservice that sends a key and value through a JSON object to a route called put-key-route.  This route communicates with Red Hat Data Grid, passing key data that is stored through headers in the Camel message.

put-key-route

When the direct: saveKey route is triggered, it will insert 3 headers into the Camel message informing the type of operation, the name, and the contents of the key. 

CamelInfinispanOperation

The operation to perform. In this case PUT.

CamelInfinispanKey

The name of key 

CamelInfinispanValue

The key content

Below is the implementation of the direct: saveKey route.

Java
 




xxxxxxxxxx
1
21


 
1
//Save a key
2
from("direct:saveKey").routeId("put-key-route")
3
 
          
4
    .setHeader(InfinispanConstants.OPERATION,
5
                 constant(InfinispanOperation.PUT))   
6
 
7
   .process(new Processor() {
8
 
          
9
        @Override
10
        public void process(Exchange exchange) throws Exception {
11
            KeyDTO chave = exchange.getIn().getBody(KeyDTO.class);
12
 
          
13
            exchange.getIn().setHeader(InfinispanConstants.KEY,
14
                                    chave.getKey());
15
 
          
16
            exchange.getIn().setHeader(InfinispanConstants.VALUE,
17
                                     chave.getValue());
18
     }    
19
 
          
20
}).to("infinispan:{{custom.rhdg.cache.name}}?cacheContainer=#remoteCacheManagerExample")
21
    .process(new ResponseSaveKeyProcessor());



In the above implementation, we used constants from the camel-Infinispan component API itself to enter the header's names and even the operation value.

Querying a Key 

To query a key in the remote cache, we need to set a header (in Camel route) called ‘CamelInfinispanOperation’ with the value GET, which will inform the remote server that we are making a querying operation. After that, we need to set a second header called ‘CamelInfinispanKey’ with the name of the key to query.

get-key-route

The value of the key is stored in the body of the Camel message. If the key doesn’t exist, the value of the body will be null.

Below, we have a query route of a key.

Java
 




x


 
1
// get a key
2
from("direct:getKey")
3
    .routeId("get-key-route")
4
    .setHeader(InfinispanConstants.OPERATION, 
5
               constant(InfinispanOperation.GET))
6
 
          
7
    .setHeader(InfinispanConstants.KEY, 
8
               simple("${in.header.id}"))        .to("infinispan:{{custom.rhdg.cache.name}}?cacheContainer=#remoteCacheManagerExample");



The key value can be recovered through message body in exchange.getIn().getBody(). No error messages return in case a key is not found in the query.

Removing a Key

A key exclusion follows the same process of the query; we just change the type of operation. In this case, we send a header with the value REMOVE and another header with the key value. 

Below, is the route that removes a key in Red Hat Data Grid.

Java
 




xxxxxxxxxx
1
10


 
1
//Remove a key
2
from("direct:deleteKey")
3
 
          
4
      .routeId("delete-key-route")
5
      .setHeader(InfinispanConstants.OPERATION,
6
          constant(InfinispanOperation.REMOVE))
7
 
          
8
      .setHeader(InfinispanConstants.KEY ,
9
          simple("${in.header.id}"))
10
        .to("infinispan:{{custom.rhdg.cache.name}}?cacheContainer=#remoteCacheManagerExample");



Life Cycle of a Key

The properties of LifeSpan e MaxIdle controls the life cycle of a key. These properties are configured during the cache creation inside Red Hat Data Grid; however, it is possible to overwrite the configuration of each key inserted through JBoss Fuse.

LifeSpan is the maximum time that a key remains valid in the cache. If this value is -1, the key never expires. The MaxIdle is the time that a key remains in the cache without being used. LifeSpan should always be higher than MaxIdle.

To set the LifeSpan and MaxIdle values, we use 4 headers.

CamelInfinispanLifespanTime

The Lifespan time of value inside the cache. Negative values are interpreted as infinity.

We can refer to it through the constant LIFESPAN_TIME defined in the InfinispanConstants class.

CamelInfinispanTimeUnit

The Time Unit of an entry Lifespan Time.

We can refer to it through the constant LIFESPAN_TIME_UNIT  defined in the InfinispanConstants class.

CamelInfinispanMaxIdleTime

The maximum amount of time an entry is allowed to be idle for before it is considered as expired.

We can refer to it through the constant MAX_IDLE_TIME defined in the InfinispanConstants class.

CamelInfinispanMaxIdleTimeUnit

The Time Unit of an entry Max Idle Time.

We can refer to it through the constant MAX_IDLE_TIME_UNIT defined in the InfinispanConstants class.

In the example below, the inserted key will have a 10 seconds lifespan.

Java
 




xxxxxxxxxx
1
30


1
//Save a key
2
from("direct:saveKey").routeId("put-key-route")
3
    .setHeader(InfinispanConstants.OPERATION,
4
                 constant(InfinispanOperation.PUT))
5
 
          
6
    .setHeader(InfinispanConstants.LIFESPAN_TIME, simple("10"))
7
    
8
    .setHeader(InfinispanConstants.LIFESPAN_TIME_UNIT, 
9
                 simple(TimeUnit.SECONDS.toString()))
10
 
          
11
    .setHeader(InfinispanConstants.MAX_IDLE_TIME, simple("10"))
12
    
13
    .setHeader(InfinispanConstants.MAX_IDLE_TIME_UNIT,
14
                 simple(TimeUnit.SECONDS.toString()))
15
    
16
    .process(new Processor() {
17
 
          
18
        @Override
19
        public void process(Exchange exchange) throws Exception {
20
            KeyDTO chave = exchange.getIn().getBody(KeyDTO.class);
21
 
          
22
            exchange.getIn().setHeader(InfinispanConstants.KEY,
23
                                          chave.getKey());
24
 
          
25
            exchange.getIn().setHeader(InfinispanConstants.VALUE,
26
                                          chave.getValue());
27
      }    
28
 
          
29
   })
30
 .to("infinispan:{{custom.rhdg.cache.name}}?cacheContainer=#remoteCacheManagerExample");



Final Considerations

JBoss  Fuse and Red Hat Data Grid are powerful tools with a lot of features.  In this article, we talked about basic operations of inserting, getting, and removing keys in a remote cache. However, there are a lot of other features that we can explore, such as local cache, metrics, and advanced queries.

Topics:
big data ,camel ,datagrid ,infinispan ,java ,jboss fuse ,middleware ,red hat data grid ,red hat jboss fuse ,red hat middleware

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}