Hazelcast Instance
Since Camel 2.7
Only consumer is supported
The Hazelcast instance component is one of Camel Hazelcast Components which allows you to consume join/leave events of the cache instance in the cluster. Hazelcast makes sense in one single "server node", but it’s extremely powerful in a clustered environment.
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,*.yamlfiles, 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 Hazelcast Instance component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
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 | |
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 | |
The Hazelcast instance that the endpoints of this component use when they set none of hazelcastInstance, hazelcastInstanceName, hazelcastConfig and hazelcastConfigUri. Without it, Camel starts a Hazelcast instance for each such endpoint, with the configuration that Hazelcast finds itself (a hazelcast.xml file, or hazelcast-client.xml in client mode, or else the Hazelcast defaults), and shuts it down when the component stops. | HazelcastInstance | ||
Whether the endpoints start or look up a Hazelcast member (node) or a Hazelcast client (client): in client mode hazelcastInstanceName names a client, and hazelcastConfigUri and hazelcastConfig give a client configuration. Any value other than node means client. | node | String |
Endpoint Options
The Hazelcast Instance endpoint is configured using URI syntax:
hazelcast-instance:cacheName
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The operation to perform when the message has no CamelHazelcastOperationType header. Each data structure supports only some of the operations, and another one fails. hazelcast-queue uses add and hazelcast-topic publish when it is not set, and the producers of the other data structures fail without an operation. hazelcast-seda and hazelcast-instance do not use it. Enum values:
| HazelcastOperation | ||
The location of a Hazelcast XML configuration file (a client configuration when hazelcastMode is client), from which Camel starts a Hazelcast instance for the endpoint. A member configuration that sets an instance name reuses the running member of that name. The file is on the classpath by default; use the file: prefix for a file. Used only when neither hazelcastInstance nor hazelcastInstanceName gives an instance, and when hazelcastConfig is not set. | String | ||
The Hazelcast instance to use. It takes precedence over hazelcastInstanceName, hazelcastConfig, hazelcastConfigUri and the hazelcastInstance option of the component. Camel does not shut it down. | HazelcastInstance | ||
The name of a Hazelcast instance that is already running in this JVM: a member, or a client when hazelcastMode is client. It takes precedence over hazelcastConfig and hazelcastConfigUri. When no instance has this name, the endpoint gets its instance as if this option was not set. | String | ||
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 | |
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 | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern |
Message Headers
The Hazelcast Instance component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelHazelcastListenerAction (consumer) Constant: | The kind of event: added, removed, updated, evicted or expired for an entry of a map, multimap or replicated map, added or removed for an item of a list, set or queue and for a member of the cluster (hazelcast-instance), and received for a message of a topic. | String | |
CamelHazelcastListenerType (consumer) Constant: | The type of listener that received the event: instancelistener for hazelcast-instance, and cachelistener for the other consumers. | String | |
CamelHazelcastListenerTime (consumer) Constant: | The time when the consumer received the event, in milliseconds since the epoch. | Long | |
CamelHazelcastInstanceHost (consumer) Constant: | The host name of the cluster member that joined or left. | String | |
CamelHazelcastInstancePort (consumer) Constant: | The port of the cluster member that joined or left. | Integer |
Message Body
The message body at each side of the Hazelcast Instance 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 |
| No body: the CamelHazelcastListenerAction header says whether a member was added to or removed from the cluster, and CamelHazelcastInstanceHost and CamelHazelcastInstancePort give its address. |
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.
instance consumer - from("hazelcast-instance:foo")
The instance consumer fires if a new cache instance joins or leaves the cluster.
Here’s a sample:
-
Java
-
XML
-
YAML
from("hazelcast-instance:foo")
.log("instance...")
.choice()
.when(header("CamelHazelcastListenerAction").isEqualTo("added"))
.log("...added")
.to("mock:added")
.otherwise()
.log("...removed")
.to("mock:removed"); <route>
<from uri="hazelcast-instance:foo"/>
<log message="instance..."/>
<choice>
<when>
<simple>${header.CamelHazelcastListenerAction} == 'added'</simple>
<log message="...added"/>
<to uri="mock:added"/>
</when>
<otherwise>
<log message="...removed"/>
<to uri="mock:removed"/>
</otherwise>
</choice>
</route> - route:
from:
uri: hazelcast-instance:foo
steps:
- log:
message: "instance..."
- choice:
when:
- simple: "${header.CamelHazelcastListenerAction} == 'added'"
steps:
- log:
message: "...added"
- to:
uri: mock:added
otherwise:
steps:
- log:
message: "...removed"
- to:
uri: mock:removed