Camel Components

Hazelcast

Since Camel 2.7

The hazelcast- component allows you to work with the Hazelcast distributed data grid / cache. Hazelcast is an in-memory data grid, entirely written in Java (single jar). It offers a great palette of different data stores like map, multimap (same key, n values), queue, list and atomic number. The main reason to use Hazelcast is its simple cluster support. If you have enabled multicast on your network, you can run a cluster with a hundred nodes with no extra configuration. Hazelcast can simply configure to add additional features like n copies between nodes (default is 1), cache persistence, network configuration (if needed), near cache, eviction, and so on. For more information, consult the Hazelcast documentation on http://www.hazelcast.com/docs.jsp.

Hazelcast components

See the following for usage of each component:

Hazelcast Instance

Receive an event when a member joins or leaves a Hazelcast cluster.

Hazelcast List

Add, get and remove the items of a Hazelcast distributed list, and receive an event when an item is added or removed.

Hazelcast Map

Put, get, query and remove the entries of a Hazelcast distributed map, and receive an event when an entry changes.

Hazelcast Multimap

Put, get and remove the values of the keys of a Hazelcast distributed multimap, and receive an event when an entry changes.

Hazelcast PN Counter

Increment, decrement and read a Hazelcast PN counter, a CRDT counter.

Hazelcast Queue

Add, poll and remove the items of a Hazelcast distributed queue, and receive its items or an event when an item is added or removed.

Hazelcast Replicated Map

Put, get and remove the entries of a Hazelcast replicated map, and receive an event when an entry changes.

Hazelcast Ringbuffer

Add items to a Hazelcast ringbuffer, and read the item at its head or its tail.

Hazelcast SEDA

Send and receive messages asynchronously between Camel routes, in the same or in different JVMs, through a Hazelcast queue.

Hazelcast Set

Add and remove the items of a Hazelcast distributed set, and receive an event when an item is added or removed.

Hazelcast Topic

Publish messages to a Hazelcast topic, and receive the messages published to it.

Installation

Maven users will need to add the following dependency to their pom.xml for this component:

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

Using hazelcast reference

By its name

XML-only: Spring bean configuration with Hazelcast instance by name
<bean id="hazelcastLifecycle" class="com.hazelcast.core.LifecycleService"
      factory-bean="hazelcastInstance" factory-method="getLifecycleService"
      destroy-method="shutdown" />

<bean id="config" class="com.hazelcast.config.Config">
    <constructor-arg type="java.lang.String" value="HZ.INSTANCE" />
</bean>

<bean id="hazelcastInstance" class="com.hazelcast.core.Hazelcast" factory-method="newHazelcastInstance">
    <constructor-arg type="com.hazelcast.config.Config" ref="config"/>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="testHazelcastInstanceBeanRefPut">
        <from uri="direct:testHazelcastInstanceBeanRefPut"/>
        <setHeader name="CamelHazelcastOperationType">
            <constant>put</constant>
        </setHeader>
        <to uri="hazelcast-map:testmap?hazelcastInstanceName=HZ.INSTANCE"/>
    </route>

    <route id="testHazelcastInstanceBeanRefGet">
        <from uri="direct:testHazelcastInstanceBeanRefGet" />
        <setHeader name="CamelHazelcastOperationType">
            <constant>get</constant>
        </setHeader>
        <to uri="hazelcast-map:testmap?hazelcastInstanceName=HZ.INSTANCE"/>
        <to uri="seda:out" />
    </route>
</camelContext>

By instance

XML-only: Spring bean configuration with Hazelcast instance by reference
<bean id="hazelcastInstance" class="com.hazelcast.core.Hazelcast"
      factory-method="newHazelcastInstance" />
<bean id="hazelcastLifecycle" class="com.hazelcast.core.LifecycleService"
      factory-bean="hazelcastInstance" factory-method="getLifecycleService"
      destroy-method="shutdown" />

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="testHazelcastInstanceBeanRefPut">
        <from uri="direct:testHazelcastInstanceBeanRefPut"/>
        <setHeader name="CamelHazelcastOperationType">
            <constant>put</constant>
        </setHeader>
        <to uri="hazelcast-map:testmap?hazelcastInstance=#hazelcastInstance"/>
    </route>

    <route id="testHazelcastInstanceBeanRefGet">
        <from uri="direct:testHazelcastInstanceBeanRefGet" />
        <setHeader name="CamelHazelcastOperationType">
            <constant>get</constant>
        </setHeader>
        <to uri="hazelcast-map:testmap?hazelcastInstance=#hazelcastInstance"/>
        <to uri="seda:out" />
    </route>
</camelContext>

Configuring HazelcastInstance on component

You can also configure the hazelcast instance on the component which will then be used by all hazelcast endpoints. In the example above we set up this for the hazelcast map component and setup hazelcast via verbose <bean> configurations.

<bean id="config" class="com.hazelcast.config.Config">
    <constructor-arg type="java.lang.String" value="HZ.INSTANCE" />
    <property name="networkConfig" ref="myNetworkConfig"/>
</bean>

<bean id="myNetworkConfig" class="com.hazelcast.config.NetworkConfig">
  <property name="port">1234</property>
</bean>

<bean id="myHazelcastInstance" class="com.hazelcast.core.Hazelcast" factory-method="newHazelcastInstance">
    <constructor-arg type="com.hazelcast.config.Config" ref="config"/>
</bean>

<bean id="hazelcast" class="org.apache.camel.component.hazelcast.map.HazelcastMapComponent">
  <property name="hazelcastInstance" ref="myHazelcastInstance"/>
</bean>

Java serialization filter

Hazelcast stores objects that implement Serializable or Externalizable using Java serialization, and deserializes them when Camel reads them back from a map, queue, topic or any other data structure. A Hazelcast JavaSerializationFilterConfig limits which classes Hazelcast deserializes.

When Camel creates the Hazelcast instance itself, it applies a default filter. The default allows class names that start with java., javax. or org.apache.camel., and rejects those that start with java.net.. Camel creates the instance itself when:

  • an endpoint sets none of hazelcastInstance, hazelcastInstanceName, hazelcastConfig or hazelcastConfigUri, and no instance is configured on the component;

  • a Hazelcast aggregation repository, idempotent repository, key-value repository or route policy starts its own instance because none was given to it.

Camel uses a configuration you supply as it is: a Config or ClientConfig bean set with hazelcastConfig, a file set with hazelcastConfigUri, or an existing instance set with hazelcastInstance or found with hazelcastInstanceName. Hazelcast’s own default configuration declares no serialization filter, so declare one in your configuration, and make it cover the classes your application stores in the cluster:

<hazelcast xmlns="http://www.hazelcast.com/schema/config">
    <serialization>
        <java-serialization-filter>
            <whitelist>
                <prefix>java.</prefix>
                <prefix>javax.</prefix>
                <prefix>org.apache.camel.</prefix>
                <prefix>com.example.model.</prefix>
            </whitelist>
            <blacklist>
                <prefix>java.net.</prefix>
            </blacklist>
        </java-serialization-filter>
    </serialization>
</hazelcast>

A hazelcast-client configuration takes the same serialization element. In Java, set the filter on the SerializationConfig of a Config or a ClientConfig:

JavaSerializationFilterConfig filter = new JavaSerializationFilterConfig();
filter.setWhitelist(new ClassFilter().addPrefixes("java.", "javax.", "org.apache.camel.", "com.example.model."));
filter.setBlacklist(new ClassFilter().addPrefixes("java.net."));

Config config = new Config();
config.getSerializationConfig().setJavaSerializationFilterConfig(filter);

Alternatively, set a JVM-wide filter with the jdk.serialFilter system property, for example -Djdk.serialFilter='!java.net.**;java.**;javax.**;org.apache.camel.**;com.example.model.**;!*'. It applies to all Java deserialization in the JVM, not only to Hazelcast.

When Camel starts a Hazelcast member or client from a Config or ClientConfig you supplied that declares no filter, and no JVM-wide filter is set, it logs a WARN.

Aggregation repositories

camel-hazelcast provides two aggregation repositories for the Aggregate EIP:

  • HazelcastAggregationRepository keeps the groups and the completed exchanges in Hazelcast IMap maps.

  • ReplicatedHazelcastAggregationRepository keeps them in Hazelcast ReplicatedMap maps, which have a copy on every member, so each member reads them from its own copy.

By default (optimistic=false), a repository holds a cluster-wide lock on the correlation key only while it writes a group. The Aggregate EIP reads the group, aggregates the new message into it, and writes it back, and only its own lock keeps the other threads of the same aggregator out of these steps. This is safe when one aggregator updates a given correlation key at a time, such as a route that runs on one member only. When the aggregators of two members (or two routes) update the same correlation key at the same time, one of them can overwrite the group that the other one just wrote, and the messages that the other one added are lost. This applies to both repositories.

ReplicatedHazelcastAggregationRepository does not support optimistic locking: a ReplicatedMap has no atomic compare-and-set, so the repository fails to start with optimistic=true. A lock around the read would not make it safe for several aggregators either, as a member reads the group from its own copy, which can miss a write that another member has just made.