Camel Components

Infinispan Embedded

Since Camel 2.13

Both producer and consumer are supported

This component allows you to interact with Infinispan distributed data grid / cache. Infinispan is an extremely scalable, highly available key/value data store and data grid platform written in Java.

The camel-infinispan-embedded 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-infinispan producer uses GET, PUT, REMOVE, and CLEAR operations. The local producer is also supported with a replicated or distributed cache.

The events are processed asynchronously.

If you use Maven, you must add the following dependency to your pom.xml:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-infinispan-embedded</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

infinispan-embedded://cacheName?[options]

The producer allows sending messages to a local infinispan cache. The consumer allows listening for events from local infinispan cache.

If no cache configuration is provided, embedded cacheContainer is created directly in the component.

Options read on the component only

All the endpoints of the component share one cache manager, which the component creates when it starts. So the options that configure the cache manager are read on the component only: configurationUri, cacheContainer, cacheContainerConfiguration and flags. Set on an endpoint, these options are not used, and the endpoint logs a WARN that names them. Configure them on the component instead, for example in application.properties:

camel.component.infinispan-embedded.configuration-uri = file:/etc/infinispan/infinispan.xml

The configurationUri is read with the resource loader of Camel, so it can be a name on the class path, a classpath: or file: uri, or an absolute path, and it can use property placeholders.

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The Infinispan Embedded component supports the following options which are listed below.

Name Description Default Type

configuration (common)

The configuration of the component, which each endpoint copies and then changes with its own options. The cache manager of the component uses only this configuration.

InfinispanEmbeddedConfiguration

queryBuilder (common)

To use a custom org.apache.camel.component.infinispan.InfinispanQueryBuilder that builds the query of the QUERY operation of the producer, which the CamelInfinispanQueryBuilder header replaces. When set, the consumer registers a continuous query instead of a listener, and receives the CacheEntryJoining, CacheEntryUpdated and CacheEntryLeaving events of the entries that match the query.

InfinispanQueryBuilder

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

clusteredListener (consumer)

Whether the consumer registers its listener as an Infinispan clustered listener, which receives the events of the changes made on all the nodes of the cluster, not only on the local node. A clustered listener receives only the events after a change, and only for entries that are created, modified, removed or expired. Not used when customListener or queryBuilder is set.

false

boolean

customListener (consumer)

To use a custom listener, a subclass of org.apache.camel.component.infinispan.embedded.InfinispanEmbeddedCustomListener annotated with the Infinispan Listener annotation, that the consumer registers on the cache in place of its own listener. The eventTypes, sync and clusteredListener options are not used then. Not used when queryBuilder is set.

InfinispanEmbeddedCustomListener

eventTypes (consumer)

The comma-separated names of the Infinispan event types that the consumer passes on to the route, for example CACHE_ENTRY_CREATED,CACHE_ENTRY_REMOVED. If not set, the consumer passes on all events. Its listener receives only the events of the types CACHE_ENTRY_ACTIVATED, CACHE_ENTRY_CREATED, CACHE_ENTRY_EXPIRED, CACHE_ENTRY_INVALIDATED, CACHE_ENTRY_LOADED, CACHE_ENTRY_MODIFIED, CACHE_ENTRY_PASSIVATED, CACHE_ENTRY_REMOVED and CACHE_ENTRY_VISITED. Not used when customListener or queryBuilder is set.

String

sync (consumer)

Whether Infinispan calls the listener of the consumer synchronously, so the cache operation that caused the event waits until the route has processed it. If false, Infinispan calls the listener in a thread of its own. Not used when customListener or queryBuilder is set.

true

boolean

defaultValue (producer)

The value that the GETORDEFAULT operation returns when the key is not in the cache. The CamelInfinispanDefaultValue header replaces this option.

Object

key (producer)

The key of the entry for the producer operations that take a key, such as PUT, GET, REMOVE and COMPUTE. When set, it takes precedence over the CamelInfinispanKey header.

Object

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

oldValue (producer)

The value that the REPLACE and REPLACEASYNC operations compare with the current value of the entry, to replace the entry only if they are equal. If not set, an existing entry is replaced whatever its value. The CamelInfinispanOldValue header replaces this option.

Object

operation (producer)

The operation that the producer performs. The CamelInfinispanOperation header replaces this option.

Enum values:

  • PUT

  • PUTASYNC

  • PUTALL

  • PUTALLASYNC

  • PUTIFABSENT

  • PUTIFABSENTASYNC

  • GET

  • GETORDEFAULT

  • CONTAINSKEY

  • CONTAINSVALUE

  • REMOVE

  • REMOVEASYNC

  • REPLACE

  • REPLACEASYNC

  • SIZE

  • CLEAR

  • CLEARASYNC

  • QUERY

  • STATS

  • COMPUTE

  • COMPUTEASYNC

PUT

InfinispanOperation

value (producer)

The value of the entry for the PUT, PUTIFABSENT and REPLACE operations and their ASYNC variants, the value to look for with CONTAINSVALUE, and the value that REMOVE and REMOVEASYNC compare with the current value, to remove the entry only if they are equal. The CamelInfinispanValue header replaces this option.

Object

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

cacheContainer (advanced)

Autowired To use a custom org.infinispan.manager.EmbeddedCacheManager as the cache manager. When set or autowired, the cacheContainerConfiguration and configurationUri options are not used, and Camel does not stop the cache manager. Read on the component only, as the cache manager is shared by all the endpoints of the component: set on an endpoint, this option is not used.

EmbeddedCacheManager

cacheContainerConfiguration (advanced)

Autowired The org.infinispan.configuration.cache.Configuration of the default cache, named default, of the cache manager that Camel creates when cacheContainer is not set. When set, configurationUri is not used. Read on the component only, as the cache manager is shared by all the endpoints of the component: set on an endpoint, this option is not used.

Configuration

configurationUri (advanced)

The location of the configuration file of the cache manager that Camel creates, read from the classpath unless it has a prefix such as file:. Not used when cacheContainer or cacheContainerConfiguration is set. For infinispan-embedded it is an Infinispan XML configuration file, read on the component only; for infinispan it is a Hot Rod client properties file.

String

flags (advanced)

A comma-separated list of names of org.infinispan.context.Flag, for example SKIP_CACHE_LOAD,SKIP_CACHE_STORE, that the endpoints apply to each cache that they use. Read on the component only, as the cache manager is shared by all the endpoints of the component: set on an endpoint, this option is not used.

String

remappingFunction (advanced)

The java.util.function.BiFunction that the COMPUTE and COMPUTEASYNC operations call with the key and the current value of the entry, or null if there is none, to compute its new value. If the function returns null, the entry is removed.

BiFunction

resultHeader (advanced)

The name of the header in which the producer stores the result of the operation, keeping the message body. If not set, the result replaces the message body. The CamelInfinispanOperationResultHeader header replaces this option.

String

Endpoint Options

The Infinispan Embedded endpoint is configured using URI syntax:

infinispan-embedded:cacheName

With the following path and query parameters:

Path Parameters

Name Description Default Type

cacheName (common)

Required The name of the cache. The value current uses the default cache of the cache manager, and the default cache of a cache manager that Camel creates is named default. The CamelInfinispanCacheName header replaces this name for the producer.

String

Query Parameters

Name Description Default Type

queryBuilder (common)

To use a custom org.apache.camel.component.infinispan.InfinispanQueryBuilder that builds the query of the QUERY operation of the producer, which the CamelInfinispanQueryBuilder header replaces. When set, the consumer registers a continuous query instead of a listener, and receives the CacheEntryJoining, CacheEntryUpdated and CacheEntryLeaving events of the entries that match the query.

InfinispanQueryBuilder

clusteredListener (consumer)

Whether the consumer registers its listener as an Infinispan clustered listener, which receives the events of the changes made on all the nodes of the cluster, not only on the local node. A clustered listener receives only the events after a change, and only for entries that are created, modified, removed or expired. Not used when customListener or queryBuilder is set.

false

boolean

customListener (consumer)

To use a custom listener, a subclass of org.apache.camel.component.infinispan.embedded.InfinispanEmbeddedCustomListener annotated with the Infinispan Listener annotation, that the consumer registers on the cache in place of its own listener. The eventTypes, sync and clusteredListener options are not used then. Not used when queryBuilder is set.

InfinispanEmbeddedCustomListener

eventTypes (consumer)

The comma-separated names of the Infinispan event types that the consumer passes on to the route, for example CACHE_ENTRY_CREATED,CACHE_ENTRY_REMOVED. If not set, the consumer passes on all events. Its listener receives only the events of the types CACHE_ENTRY_ACTIVATED, CACHE_ENTRY_CREATED, CACHE_ENTRY_EXPIRED, CACHE_ENTRY_INVALIDATED, CACHE_ENTRY_LOADED, CACHE_ENTRY_MODIFIED, CACHE_ENTRY_PASSIVATED, CACHE_ENTRY_REMOVED and CACHE_ENTRY_VISITED. Not used when customListener or queryBuilder is set.

String

sync (consumer)

Whether Infinispan calls the listener of the consumer synchronously, so the cache operation that caused the event waits until the route has processed it. If false, Infinispan calls the listener in a thread of its own. Not used when customListener or queryBuilder is set.

true

boolean

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

ExchangePattern

defaultValue (producer)

The value that the GETORDEFAULT operation returns when the key is not in the cache. The CamelInfinispanDefaultValue header replaces this option.

Object

key (producer)

The key of the entry for the producer operations that take a key, such as PUT, GET, REMOVE and COMPUTE. When set, it takes precedence over the CamelInfinispanKey header.

Object

oldValue (producer)

The value that the REPLACE and REPLACEASYNC operations compare with the current value of the entry, to replace the entry only if they are equal. If not set, an existing entry is replaced whatever its value. The CamelInfinispanOldValue header replaces this option.

Object

operation (producer)

The operation that the producer performs. The CamelInfinispanOperation header replaces this option.

Enum values:

  • PUT

  • PUTASYNC

  • PUTALL

  • PUTALLASYNC

  • PUTIFABSENT

  • PUTIFABSENTASYNC

  • GET

  • GETORDEFAULT

  • CONTAINSKEY

  • CONTAINSVALUE

  • REMOVE

  • REMOVEASYNC

  • REPLACE

  • REPLACEASYNC

  • SIZE

  • CLEAR

  • CLEARASYNC

  • QUERY

  • STATS

  • COMPUTE

  • COMPUTEASYNC

PUT

InfinispanOperation

value (producer)

The value of the entry for the PUT, PUTIFABSENT and REPLACE operations and their ASYNC variants, the value to look for with CONTAINSVALUE, and the value that REMOVE and REMOVEASYNC compare with the current value, to remove the entry only if they are equal. The CamelInfinispanValue header replaces this option.

Object

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

cacheContainer (advanced)

Autowired To use a custom org.infinispan.manager.EmbeddedCacheManager as the cache manager. When set or autowired, the cacheContainerConfiguration and configurationUri options are not used, and Camel does not stop the cache manager. Read on the component only, as the cache manager is shared by all the endpoints of the component: set on an endpoint, this option is not used.

EmbeddedCacheManager

cacheContainerConfiguration (advanced)

Autowired The org.infinispan.configuration.cache.Configuration of the default cache, named default, of the cache manager that Camel creates when cacheContainer is not set. When set, configurationUri is not used. Read on the component only, as the cache manager is shared by all the endpoints of the component: set on an endpoint, this option is not used.

Configuration

configurationUri (advanced)

The location of the configuration file of the cache manager that Camel creates, read from the classpath unless it has a prefix such as file:. Not used when cacheContainer or cacheContainerConfiguration is set. For infinispan-embedded it is an Infinispan XML configuration file, read on the component only; for infinispan it is a Hot Rod client properties file.

String

flags (advanced)

A comma-separated list of names of org.infinispan.context.Flag, for example SKIP_CACHE_LOAD,SKIP_CACHE_STORE, that the endpoints apply to each cache that they use. Read on the component only, as the cache manager is shared by all the endpoints of the component: set on an endpoint, this option is not used.

String

remappingFunction (advanced)

The java.util.function.BiFunction that the COMPUTE and COMPUTEASYNC operations call with the key and the current value of the entry, or null if there is none, to compute its new value. If the function returns null, the entry is removed.

BiFunction

resultHeader (advanced)

The name of the header in which the producer stores the result of the operation, keeping the message body. If not set, the result replaces the message body. The CamelInfinispanOperationResultHeader header replaces this option.

String

Message Headers

The Infinispan Embedded component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelInfinispanEventType (consumer)

Constant: EVENT_TYPE

The type of the event that the consumer received: the name of the Infinispan event type, or CacheEntryJoining, CacheEntryUpdated or CacheEntryLeaving for a continuous query.

String

CamelInfinispanIsPre (consumer)

Constant: IS_PRE

true if the notification is before the event has occurred, false if after the event has occurred.

boolean

CamelInfinispanCacheName (common)

Constant: CACHE_NAME

The name of the cache. The consumer sets it for each event, and for the producer it replaces the cache name of the endpoint.

String

CamelInfinispanKey (common)

Constant: KEY

The key of the entry. The consumer sets it for each event that has one. The producer uses it for the operations that take a key, unless the key option is set.

Object

CamelInfinispanValue (producer)

Constant: VALUE

The value of the entry for the producer operations that take a value. It replaces the value option.

Object

CamelInfinispanDefaultValue (producer)

Constant: DEFAULT_VALUE

The value that the GETORDEFAULT operation returns when the key is not in the cache. It replaces the defaultValue option.

Object

CamelInfinispanOldValue (producer)

Constant: OLD_VALUE

The value that the REPLACE and REPLACEASYNC operations compare with the current value of the entry, to replace the entry only if they are equal. It replaces the oldValue option. The consumer of infinispan-embedded sets it to the value of the removed entry for a CACHE_ENTRY_REMOVED event.

Object

CamelInfinispanMap (producer)

Constant: MAP

The java.util.Map of the entries that the PUTALL and PUTALLASYNC operations store.

Map

CamelInfinispanOperation (producer)

Constant: OPERATION

The operation that the producer performs. It replaces the operation option.

Enum values:

  • PUT

  • PUTASYNC

  • PUTALL

  • PUTALLASYNC

  • PUTIFABSENT

  • PUTIFABSENTASYNC

  • GET

  • GETORDEFAULT

  • CONTAINSKEY

  • CONTAINSVALUE

  • REMOVE

  • REMOVEASYNC

  • REPLACE

  • REPLACEASYNC

  • SIZE

  • CLEAR

  • CLEARASYNC

  • QUERY

  • STATS

  • COMPUTE

  • COMPUTEASYNC

InfinispanOperation

CamelInfinispanOperationResult (producer)

Constant: RESULT

Deprecated The name of the header whose value is the result. Deprecation note: Never set nor read by the component. Use CamelInfinispanOperationResultHeader to choose the header that carries the result of an operation.

String

CamelInfinispanOperationResultHeader (producer)

Constant: RESULT_HEADER

The name of the header in which the producer stores the result of the operation, keeping the message body. It replaces the resultHeader option.

String

CamelInfinispanLifespanTime (producer)

Constant: LIFESPAN_TIME

The lifespan of the entry, after which it expires, for the PUT, PUTALL, PUTIFABSENT and REPLACE operations and their ASYNC variants. Only used together with the CamelInfinispanTimeUnit header. A negative value means that the entry does not expire.

long

CamelInfinispanTimeUnit (producer)

Constant: LIFESPAN_TIME_UNIT

The java.util.concurrent.TimeUnit of the CamelInfinispanLifespanTime header.

Enum values:

  • NANOSECONDS

  • MICROSECONDS

  • MILLISECONDS

  • SECONDS

  • MINUTES

  • HOURS

  • DAYS

TimeUnit

CamelInfinispanMaxIdleTime (producer)

Constant: MAX_IDLE_TIME

The maximum time that the entry can stay without being accessed before it expires, for the same operations as the CamelInfinispanLifespanTime header. Only used together with the CamelInfinispanMaxIdleTimeUnit header, and only when the CamelInfinispanLifespanTime and CamelInfinispanTimeUnit headers are set too.

long

CamelInfinispanMaxIdleTimeUnit (producer)

Constant: MAX_IDLE_TIME_UNIT

The java.util.concurrent.TimeUnit of the CamelInfinispanMaxIdleTime header.

Enum values:

  • NANOSECONDS

  • MICROSECONDS

  • MILLISECONDS

  • SECONDS

  • MINUTES

  • HOURS

  • DAYS

TimeUnit

CamelInfinispanIgnoreReturnValues (producer)

Constant: IGNORE_RETURN_VALUES

Whether the producer applies the Infinispan IGNORE_RETURN_VALUES flag to the cache, so that a write operation does not need to read the existing value from a cache store or from another node. The result of such an operation, for example the previous value for PUT, can then be null.

false

boolean

CamelInfinispanEventData (consumer)

Constant: EVENT_DATA

The value of the entry that the event is about, when the event carries one. For a continuous query, it is the value of the entry that joins the result or is updated.

Object

CamelInfinispanQueryBuilder (producer)

Constant: QUERY_BUILDER

The org.apache.camel.component.infinispan.InfinispanQueryBuilder that builds the query of the QUERY operation. It replaces the queryBuilder option.

InfinispanQueryBuilder

CamelInfinispanCommandRetried (consumer)

Constant: COMMAND_RETRIED

This will be true if the write command that caused this had to be retried again due to a topology change.

boolean

CamelInfinispanEntryCreated (consumer)

Constant: ENTRY_CREATED

Indicates whether the cache entry modification event is the result of the cache entry being created.

boolean

CamelInfinispanOriginLocal (consumer)

Constant: ORIGIN_LOCAL

true if the call originated on the local cache instance; false if originated from a remote one.

boolean

CamelInfinispanCurrentState (consumer)

Constant: CURRENT_STATE

Whether Infinispan generated the event for an entry that already existed when the listener was registered, which it does only for a listener whose Listener annotation sets includeCurrentState to true.

boolean

Message Body

The message body at each side of the Infinispan Embedded component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Consumer

null

No body: the event type, the cache name and the key of the entry are in headers, and the value, when the event has one, in the CamelInfinispanEventData header.

Not known when customListener is set.

Producer input

any

The body is not used: the key, the value and the Map for PUTALL come from the CamelInfinispanKey, CamelInfinispanValue and CamelInfinispanMap headers, or the key and value options.

Producer output

unknown

The result of the operation, which the CamelInfinispanOperation header can set: the value for GET, or null when the key is not in the cache, the previous value for PUT, the default, a Boolean for CONTAINSKEY, an Integer for SIZE, a List for QUERY and a CompletableFuture for the ASYNC operations; PUTALL and CLEAR keep the body. With resultHeader the result goes to that header and the body is kept.

  • With operation=PUTASYNC: pojo (java.util.concurrent.CompletableFuture)

  • With operation=PUTALL: unchanged

  • With operation=PUTALLASYNC: pojo (java.util.concurrent.CompletableFuture)

  • With operation=PUTIFABSENTASYNC: pojo (java.util.concurrent.CompletableFuture)

  • With operation=CONTAINSKEY: value (java.lang.Boolean)

  • With operation=CONTAINSVALUE: value (java.lang.Boolean)

  • With operation=REMOVEASYNC: pojo (java.util.concurrent.CompletableFuture)

  • With operation=REPLACEASYNC: pojo (java.util.concurrent.CompletableFuture)

  • With operation=SIZE: value (java.lang.Integer)

  • With operation=CLEAR: unchanged

  • With operation=CLEARASYNC: pojo (java.util.concurrent.CompletableFuture)

  • With operation=QUERY: list or unchanged

  • With operation=STATS: pojo (org.infinispan.stats.Stats)

  • With operation=COMPUTEASYNC: pojo (java.util.concurrent.CompletableFuture)

Not known when resultHeader is set.

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Usage

Camel Operations

This section lists all available operations, along with their header information.

Table 1. Put Operations
Operation Name Description

InfinispanOperation.PUT

Put a key/value pair in the cache, optionally with expiration

InfinispanOperation.PUTASYNC

Asynchronously puts a key/value pair in the cache, optionally with expiration

InfinispanOperation.PUTIFABSENT

Put a key/value pair in the cache if it did not exist, optionally with expiration

InfinispanOperation.PUTIFABSENTASYNC

Asynchronously puts a key/value pair in the cache if it did not exist, optionally with expiration

  • Required Headers:

    • CamelInfinispanKey

    • CamelInfinispanValue

  • Optional Headers:

    • CamelInfinispanLifespanTime

    • CamelInfinispanTimeUnit

    • CamelInfinispanMaxIdleTime

    • CamelInfinispanMaxIdleTimeUnit

  • Result:

    • the message body, or the header named by the resultHeader option

Table 2. Put All Operations
Operation Name Description

InfinispanOperation.PUTALL

Adds multiple entries to a cache, optionally with expiration

CamelInfinispanOperation.PUTALLASYNC

Asynchronously adds multiple entries to a cache, optionally with expiration

  • Required Headers:

    • CamelInfinispanMap

  • Optional Headers:

    • CamelInfinispanLifespanTime

    • CamelInfinispanTimeUnit

    • CamelInfinispanMaxIdleTime

    • CamelInfinispanMaxIdleTimeUnit

Table 3. Get Operations
Operation Name Description

InfinispanOperation.GET

Retrieve the value associated with a specific key from the cache

InfinispanOperation.GETORDEFAULT

Retrieves the value, or default value, associated with a specific key from the cache

  • Required Headers:

    • CamelInfinispanKey

Table 4. Contains Key Operation
Operation Name Description

InfinispanOperation.CONTAINSKEY

Determines whether a cache contains a specific key

  • Required Headers

    • CamelInfinispanKey

  • Result:

    • the message body, or the header named by the resultHeader option

Table 5. Contains Value Operation
Operation Name Description

InfinispanOperation.CONTAINSVALUE

Determines whether a cache contains a specific value

  • Required Headers:

    • CamelInfinispanKey

Table 6. Remove Operations
Operation Name Description

InfinispanOperation.REMOVE

Removes an entry from a cache, optionally only if the value matches a given one

InfinispanOperation.REMOVEASYNC

Asynchronously removes an entry from a cache, optionally only if the value matches a given one

  • Required Headers:

    • CamelInfinispanKey

  • Optional Headers:

    • CamelInfinispanValue

  • Result:

    • the message body, or the header named by the resultHeader option

Table 7. Replace Operations
Operation Name Description

InfinispanOperation.REPLACE

Conditionally replaces an entry in the cache, optionally with expiration

InfinispanOperation.REPLACEASYNC

Asynchronously conditionally replaces an entry in the cache, optionally with expiration

  • Required Headers:

    • CamelInfinispanKey

    • CamelInfinispanValue

    • CamelInfinispanOldValue

  • Optional Headers:

    • CamelInfinispanLifespanTime

    • CamelInfinispanTimeUnit

    • CamelInfinispanMaxIdleTime

    • CamelInfinispanMaxIdleTimeUnit

  • Result:

    • the message body, or the header named by the resultHeader option

Table 8. Clear Operations
Operation Name Description

InfinispanOperation.CLEAR

Clears the cache

InfinispanOperation.CLEARASYNC

Asynchronously clears the cache

Table 9. Size Operation
Operation Name Description

InfinispanOperation.SIZE

Returns the number of entries in the cache

  • Result:

    • the message body, or the header named by the resultHeader option

Table 10. Stats Operation
Operation Name Description

InfinispanOperation.STATS

Returns statistics about the cache

  • Result:

    • the message body, or the header named by the resultHeader option

Table 11. Query Operation
Operation Name Description

InfinispanOperation.QUERY

Executes a query on the cache

  • Required Headers:

    • CamelInfinispanQueryBuilder

  • Result:

    • the message body, or the header named by the resultHeader option

Write methods like put(key, value) and remove(key) do not return the previous value by default.

Key and cache name headers

The key option of the endpoint takes precedence over the CamelInfinispanKey header. When the endpoint does not set key, the producer uses the CamelInfinispanKey header.

The CamelInfinispanCacheName header takes precedence over the cache name of the endpoint.

An infinispan-embedded consumer sets CamelInfinispanKey and CamelInfinispanCacheName to the key and the cache of the event. When a route sends those events to an infinispan-embedded producer, the producer works on the cache of the event, and on the key of the event unless the endpoint sets key. To use the cache and the key configured on the producer endpoint, remove the headers first:

from("infinispan-embedded:source")
    .removeHeaders("CamelInfinispan*")
    .to("infinispan-embedded:target?operation=PUT&key=myKey&value=myValue");

Examples

  • Put a key/value into a named cache:

    Java-only: uses InfinispanConstants and InfinispanOperation Java constants
    from("direct:start")
        .setHeader(InfinispanConstants.OPERATION).constant(InfinispanOperation.PUT) (1)
        .setHeader(InfinispanConstants.KEY).constant("123") (2)
        .setHeader(InfinispanConstants.VALUE).constant("Hello") (3)
        .to("infinispan-embedded:myCacheName"); (4)
    1 Set the operation to perform
    2 Set the key used to identify the element in the cache
    3 Set the value to store: the message body is not used
    4 Put an element to the cache named myCacheName of the cache manager of the component

    It is possible to configure the lifetime and/or the idle time before the entry expires and gets evicted from the cache, as example:

    Java-only: uses InfinispanConstants Java constants and TimeUnit enum
    from("direct:start")
        .setHeader(InfinispanConstants.OPERATION).constant(InfinispanOperation.GET)
        .setHeader(InfinispanConstants.KEY).constant("123")
        .setHeader(InfinispanConstants.LIFESPAN_TIME).constant(100L) (1)
        .setHeader(InfinispanConstants.LIFESPAN_TIME_UNIT).constant(TimeUnit.MILLISECONDS.toString()) (2)
        .to("infinispan-embedded:myCacheName");
    1 Set the lifespan of the entry
    2 Set the time unit for the lifespan
  • Queries

    Java-only: uses InfinispanQueryBuilder anonymous class and Java constants
    from("direct:start")
        .setHeader(InfinispanConstants.OPERATION, InfinispanConstants.QUERY)
        .setHeader(InfinispanConstants.QUERY_BUILDER, new InfinispanQueryBuilder() {
            @Override
            public Query build(QueryFactory<Query> qf) {
                return qf.from(User.class).having("name").like("%abc%").build();
            }
        })
        .to("infinispan-embedded:myCacheName");
  • Custom Listeners

    • Java

    • XML

    • YAML

    from("infinispan-embedded:myCacheName?customListener=#myCustomListener")
      .to("mock:result");
    <route>
      <from uri="infinispan-embedded:myCacheName?customListener=#myCustomListener"/>
      <to uri="mock:result"/>
    </route>
    - route:
        from:
          uri: infinispan-embedded:myCacheName
          parameters:
            customListener: "#myCustomListener"
          steps:
            - to:
                uri: mock:result

    The instance of myCustomListener must exist and Camel should be able to look it up from the Registry. Users are encouraged to extend the org.apache.camel.component.infinispan.embedded.InfinispanEmbeddedCustomListener class and annotate the resulting class with @Listener which can be found in the package org.infinispan.notifications.

Using the Infinispan based idempotent repository

In this section, we will use the Infinispan based idempotent repository.

  • Java

  • XML

InfinispanEmbeddedConfiguration conf = new InfinispanEmbeddedConfiguration(); (1)
conf.setConfigurationUri("classpath:infinispan.xml")

InfinispanEmbeddedIdempotentRepository repo = new InfinispanEmbeddedIdempotentRepository("idempotent");  (2)
repo.setConfiguration(conf);

context.addRoutes(new RouteBuilder() {
    @Override
    public void configure() {
        from("direct:start")
            .idempotentConsumer(header("MessageID"), repo) (3)
            .to("mock:result");
    }
});
  1. Configure the cache

  2. Configure the repository bean

  3. Set the repository to the route

<bean id="infinispanRepo" class="org.apache.camel.component.infinispan.embedded.InfinispanEmbeddedIdempotentRepository" destroy-method="stop">
  <constructor-arg value="idempotent"/> (1)
  <property name="configuration"> (2)
    <bean class="org.apache.camel.component.infinispan.embedded.InfinispanEmbeddedConfiguration">
      <property name="configurationUri" value="classpath:infinispan.xml"/>
    </bean>
  </property>
</bean>

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:start" />
        <idempotentConsumer idempotentRepository="infinispanRepo"> (3)
            <header>MessageID</header>
            <to uri="mock:result" />
        </idempotentConsumer>
    </route>
</camelContext>
  1. Set the name of the cache that will be used by the repository

  2. Configure the repository bean

  3. Set the repository to the route

Using the Infinispan based aggregation repository

In this section, we will use the Infinispan based aggregation repository.

  • Java

  • XML

InfinispanEmbeddedConfiguration conf = new InfinispanEmbeddedConfiguration(); (1)
conf.setConfigurationUri("classpath:infinispan.xml");

InfinispanEmbeddedAggregationRepository repo = new InfinispanEmbeddedAggregationRepository("aggregation");  (2)
repo.setConfiguration(conf);

context.addRoutes(new RouteBuilder() {
    @Override
    public void configure() {
        from("direct:start")
                .aggregate(header("MessageID"))
                .completionSize(3)
                .aggregationRepository(repo) (3)
                .aggregationStrategy("myStrategy")
                .to("mock:result");
    }
});
  1. Configure the cache

  2. Create the repository bean

  3. Set the repository to the route

<bean id="infinispanRepo" class="org.apache.camel.component.infinispan.embedded.InfinispanEmbeddedAggregationRepository" destroy-method="stop">
  <constructor-arg value="aggregation"/> (1)
  <property name="configuration"> (2)
    <bean class="org.apache.camel.component.infinispan.embedded.InfinispanEmbeddedConfiguration">
      <property name="configurationUri" value="classpath:infinispan.xml"/>
    </bean>
  </property>
</bean>

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="direct:start" />
        <aggregate aggregationStrategy="myStrategy"
                   completionSize="3"
                   aggregationRepository="infinispanRepo"> (3)
            <correlationExpression>
                <header>MessageID</header>
            </correlationExpression>
            <to uri="mock:result"/>
        </aggregate>
    </route>
</camelContext>
  1. Set the name of the cache that will be used by the repository

  2. Configure the repository bean

  3. Set the repository to the route

With the release of Infinispan 11, it is required to set the encoding configuration on any cache created. This is critical for consuming events too. For more information, have a look at Data Encoding and MediaTypes in the official Infinispan documentation.