Chapter 5. Running Red Hat JBoss Data Grid with Apache Camel

Apache Camel is an open source integration and routing system that allows transference of messages from various sources to different destinations, providing an integration framework that allows interaction with various systems using the same API, regardless of the protocol or data type. Using Camel with Red Hat JBoss Data Grid and Red Hat JBoss Fuse simplifies integration in large enterprise applications by providing a wide variety of transports and APIs that add connectivity.
JBoss Data Grid provides support for caching on Camel routes in JBoss Fuse, partially replacing Ehcache. JBoss Data Grid is supported as an embedded cache (local or clustered) or as a remote cache in a Camel route.

5.1. The camel-jbossdatagrid Component

Red Hat JBoss Data Grid's camel-jbossdatagrid component includes the following features:
  • Local Camel Consumer

    Receives cache change notifications and sends them to be processed. This can be done synchronously or asynchronously, and is also supported with a replicated or distributed cache.

  • Local Camel Producer

    A producer creates and sends messages to an endpoint. The camel-jbossdatagrid producer uses GET, PUT, REMOVE, and CLEAR operations. The local producer is also supported with a replicated or distributed cache.

  • Remote Camel Producer

    In Remote Client-Server mode, the Camel producer can send messages using Hot Rod.

  • Remote Camel Consumer

    In Client-Server mode, receives cache change notifications and sends them to be processed. The events are processed asynchronously.

The following camel-jbossdatagrid dependency must be added to the pom.xml file to run JBoss Data Grid with Camel:
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jbossdatagrid</artifactId>
    <version>7.0.0.ER4-redhat-1</version>
    <!-- use the same version as your JBoss Data Grid version -->
</dependency>

Note

The camel-jbossdatagrid component ships with JBoss Data Grid, and is not included in the JBoss Fuse 6.1 or JBoss Fuse Service Works 6.0 distributions.
Camel components are the main extension point in Camel, and are associated with the name used in a URI, and act as a factory of endpoints. For example, a FileComponent is referred to in a URI as file, which creates FileEndpoints.
URI Format

The following URI format is used for camel-jbossdatagrid:

infinispan://hostname?[options]
URI Options

The producer can create and send messages to a local or remote JBoss Data Grid cache configured in the registry. If a cacheContainer is present, the cache will be either local or remote, depending on whether the cacheContainer instance is a DefaultCacheManager or RemoteCacheManager. If it is not present, the cache will try to connect to remote cache using the supplied hostname/port.

A consumer listens for events from the local JBoss Data Grid cache accessible from the registry.

Table 5.1. URI Options

Name Default Value Type Context Description
cacheContainer null CacheContainer Shared Reference to a org.infinispan.manager.CacheContainer in the Registry.
cacheName null String Shared The cache name to use. If not specified, the default cache is used.
command PUT String Producer The operation to perform. Only the PUT, GET, REMOVE, and CLEAR values are currently supported.
eventTypes null Set<String> Consumer
A comma separated list of the event types to register. By default, this listens for all event types. Possible values are defined in org.infinispan.notifications.cachelistener.event.Event.Type.
Example:
...?eventTypes=CACHE_ENTRY_EXPIRED,CACHE_ENTRY_EVICTED,...
sync true Boolean Consumer By default the consumer will receive notifications synchronously by the same thread that process the cache operation. Remote HotRod listeners support only asynchronous notification.
clustered false Boolean Consumer By default the consumer will only receive local events. By using this option, the consumer also listens to events originated on other nodes in the cluster. The only events available for clustered listeners are CACHE_ENTRY_CREATED, CACHE_ENTRY_REMOVED, and CACHE_ENTRY_MODIFIED.
Camel Operations

A list of all available operations, along with their header information, is found below:

Table 5.2. Put Operations

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationPut Embedded / Remote Puts a key/value pair in the cache, optionally with expiration CamelInfinispanKey, CamelInfinispanValue CamelInfinispanLifespanTime, CamelInfinispanLifespanTimeUnit, CamelInfinispanMaxIdleTime, CamelInfinispanMaxIdleTimeUnit, CamelInfinispanIgnoreReturnValues CamelInfinispanOperationResult
CamelInfinispanOperationPutAsync Asynchronously puts a key/value pair in the cache, optionally with expiration
CamelInfinispanOperationPutIfAbsent Puts a key/value pair in the cache if it did not exist, optionally with expiration
CamelInfinispanOperationPutIfAbsentAsync Asynchronously puts a key/value pair in the cache if it did not exist, optionally with expiration

Table 5.3. Put All Operations

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationPutAll Embedded / Remote Adds multiple entries to a cache, optionally with expiration CamelInfinispanMap CamelInfinispanLifespanTime, CamelInfinispanLifespanTimeUnit, CamelInfinispanMaxIdleTime, CamelInfinispanMaxIdleTimeUnit  
CamelInfinispanOperationPutAllAsync Asynchronously adds multiple entries to a cache, optionally with expiration  

Table 5.4. Get Operation

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationGet Embedded / Remote Retrieves the value associated with a specific key from the cache CamelInfinispanKey   

Table 5.5. Contains Key Operation

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationContainsKey Embedded / Remote Determines whether a cache contains a specific key CamelInfinispanKey CamelInfinispanOperationResult

Table 5.6. Contains Value Operation

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationContainsValue Embedded / Remote Determines whether a cache contains a specific value CamelInfinispanKey   

Table 5.7. Remove Operations

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationRemove Embedded / Remote Removes an entry from a cache, optionally only if the value matches a given one CamelInfinispanKey CamelInfinispanValue CamelInfinispanOperationResult
CamelInfinispanOperationRemoveAsync Asynchronously removes an entry from a cache, optionally only if the value matches a given one

Table 5.8. Replace Operations

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationReplace Embedded / Remote Conditionally replaces an entry in the cache, optionally with expiration CamelInfinispanKey, CamelInfinispanValue, CamelInfinispanOldValue CamelInfinispanLifespanTime, CamelInfinispanLifespanTimeUnit, CamelInfinispanMaxIdleTime, CamelInfinispanMaxIdleTimeUnit, CamelInfinispanIgnoreReturnValues CamelInfinispanOperationResult
CamelInfinispanOperationReplaceAsync Asynchronously conditionally replaces an entry in the cache, optionally with expiration

Table 5.9. Clear Operation

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationClear Embedded / Remote Clears the cache    

Table 5.10. Size Operation

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationSize Embedded / Remote Returns the number of entries in the cache CamelInfinispanOperationResult

Table 5.11. Query Operation

Operation Name Context Description Required Headers Optional Headers Result Header
CamelInfinispanOperationQuery Remote Executes a query on the cache CamelInfinispanQueryBuilder CamelInfinispanOperationResult

Note

Any operations that take CamelInfinispanIgnoreReturnValues will receive a null result.

Table 5.12. Message Headers

Name Default Value Type Context Description
CamelInfinispanCacheName null String Shared The cache participating in the operation or event.
CamelInfinispanMap null Map Producer A Map to use in case of the CamelInfinispanOperationPutAll operation.
CamelInfinispanKey null Object Shared The key to perform the operation to or the key generating the event.
CamelInfinispanValue null Object Producer The value to use for the operation.
CamelInfinispanOperationResult null Object Producer The result of the operation.
CamelInfinispanEventType null String Consumer
For local cache listeners (non-clustered), one of the following values: CACHE_ENTRY_ACTIVATED, CACHE_ENTRY_PASSIVATED, CACHE_ENTRY_VISITED, CACHE_ENTRY_LOADED, CACHE_ENTRY_EVICTED, CACHE_ENTRY_CREATED, CACHE_ENTRY_REMOVED, CACHE_ENTRY_MODIFIED
For remote HotRod listeners, one of the following values: CLIENT_CACHE_ENTRY_CREATED, CLIENT_CACHE_ENTRY_MODIFIED, CLIENT_CACHE_ENTRY_REMOVED, CLIENT_CACHE_FAILOVER.
CamelInfinispanIsPre null Boolean Consumer Infinispan fires two events for each operation when local non-clustered listener is used: one before and one after the operation. For clustered listeners and remote HotRod listeners, Infinispan fires only one event after the operation.
CamelInfinispanQueryBuilder null InfinispanQueryBuilder Producer An instance of InfinispanQueryBuilder that, in its build(), defines the query to be executed on the cache.
CamelInfinispanLifespanTime null long Producer The Lifespan time of a value inside the cache. Negative values are interpreted as infinity.
CamelInfinispanTimeUnit null String Producer The Time Unit of an entry Lifespan Time.
CamelInfinispanMaxIdleTime null long Producer The maximum amount of time an entry is allowed to be idle for before it is considered as expired.
CamelInfinispanMaxIdleTimeUnit null String Producer The Time Unit of an entry Max Idle Time.