Hazelcast Queue
Since Camel 2.7
Both producer and consumer are supported
The Hazelcast Queue component is one of Camel Hazelcast Components which allows you to access Hazelcast distributed queue.
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 Queue 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 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 | |
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 Queue endpoint is configured using URI syntax:
hazelcast-queue: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 | ||
Milliseconds that a poll of the consumer in poll mode waits for an item, at most 1000: a longer value has the effect of 1000. When no item arrives, the consumer polls again. After a poll fails, the consumer waits this long, and at least 1 second, before it polls again. | 10000 | long | |
The number of threads of the thread pool of the consumer in poll mode. The consumer polls the queue with a single task, so it uses one thread. | 1 | int | |
How the consumer receives the items. listen registers an item listener: the consumer receives an event for each item added to or removed from the queue, and the items stay on the queue. poll takes the items off the queue, one at a time. Enum values:
| listen | HazelcastQueueConsumerMode | |
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 | ||
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 |
Message Headers
The Hazelcast Queue component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelHazelcastObjectId (common) Constant: | The key of the entry: the producer of a map, multimap or replicated map uses it for the operations on a key (a Set of keys for the getAll operation of a map) and removes the header afterwards, and their consumers set it to the key of the entry of the event. | String | |
CamelHazelcastDrainToCollection (producer) Constant: | The Collection that the drainTo operation moves all items of the queue into. drainTo needs it. | Collection | |
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 | |
CamelHazelcastCacheName (consumer) Constant: | The name of the data structure that the event comes from: the cacheName of the endpoint. | String | |
CamelHazelcastOperationType (producer) Constant: | The operation to perform, such as put or get, by its name or as a HazelcastOperation: it overrides the defaultOperation option. The producer removes the header afterwards. | String |
Message Body
The message body at each side of the Hazelcast Queue 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 |
| In Listen mode, the default, the Hazelcast ItemEvent of an item added to or removed from the queue, not the item: ${body.item} is the item, and the CamelHazelcastListenerAction header is added or removed. In Poll mode the consumer takes the items off the queue, and the body is the item itself, without headers.
|
Producer input |
| The item that add, the default, put and offer add to the queue, and that removeValue removes (without a body it removes the head): an object Hazelcast can serialize, so a stream or no body fails. A Collection for removeAll and retainAll, and a java.util.function.Predicate for removeIf, which fails without one. The other operations do not use the body. |
Producer output |
| Unchanged for add, the default, and the other operations that change the queue. poll and peek give the head of the queue, or null when it is empty, and take waits for one; remainingCapacity gives an Integer, and drainTo the number of items it moved into the Collection of the CamelHazelcastDrainToCollection header, which it needs. removeIf gives a Boolean, true when it removed an item. The CamelHazelcastOperationType header overrides the defaultOperation option.
|
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.
Queue producer – to(“hazelcast-queue:foo”)
The queue producer provides 12 operations:
-
add
-
put
-
poll
-
peek
-
offer
-
removevalue
-
remainingCapacity
-
removeAll
-
removeIf
-
drainTo
-
take
-
retainAll
Example for add:
-
Java
-
XML
-
YAML
from("direct:add")
.setHeader("CamelHazelcastOperationType", constant("add"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:add"/>
<setHeader name="CamelHazelcastOperationType">
<constant>add</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:add
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: add
- to:
uri: hazelcast-queue:bar Example for put:
-
Java
-
XML
-
YAML
from("direct:put")
.setHeader("CamelHazelcastOperationType", constant("put"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:put"/>
<setHeader name="CamelHazelcastOperationType">
<constant>put</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:put
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: put
- to:
uri: hazelcast-queue:bar Example for poll:
-
Java
-
XML
-
YAML
from("direct:poll")
.setHeader("CamelHazelcastOperationType", constant("poll"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:poll"/>
<setHeader name="CamelHazelcastOperationType">
<constant>poll</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:poll
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: poll
- to:
uri: hazelcast-queue:bar Example for peek:
-
Java
-
XML
-
YAML
from("direct:peek")
.setHeader("CamelHazelcastOperationType", constant("peek"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:peek"/>
<setHeader name="CamelHazelcastOperationType">
<constant>peek</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:peek
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: peek
- to:
uri: hazelcast-queue:bar Example for offer:
-
Java
-
XML
-
YAML
from("direct:offer")
.setHeader("CamelHazelcastOperationType", constant("offer"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:offer"/>
<setHeader name="CamelHazelcastOperationType">
<constant>offer</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:offer
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: offer
- to:
uri: hazelcast-queue:bar Example for removevalue:
-
Java
-
XML
-
YAML
from("direct:removevalue")
.setHeader("CamelHazelcastOperationType", constant("removeValue"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:removevalue"/>
<setHeader name="CamelHazelcastOperationType">
<constant>removeValue</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:removevalue
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: removeValue
- to:
uri: hazelcast-queue:bar Example for remaining capacity:
-
Java
-
XML
-
YAML
from("direct:remaining-capacity")
.setHeader("CamelHazelcastOperationType", constant("remainingCapacity"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:remaining-capacity"/>
<setHeader name="CamelHazelcastOperationType">
<constant>remainingCapacity</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:remaining-capacity
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: remainingCapacity
- to:
uri: hazelcast-queue:bar Example for remove all:
-
Java
-
XML
-
YAML
from("direct:removeAll")
.setHeader("CamelHazelcastOperationType", constant("removeAll"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:removeAll"/>
<setHeader name="CamelHazelcastOperationType">
<constant>removeAll</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:removeAll
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: removeAll
- to:
uri: hazelcast-queue:bar Example for remove if:
-
Java
-
XML
-
YAML
from("direct:removeIf")
.setHeader("CamelHazelcastOperationType", constant("removeIf"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:removeIf"/>
<setHeader name="CamelHazelcastOperationType">
<constant>removeIf</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:removeIf
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: removeIf
- to:
uri: hazelcast-queue:bar The message body is the java.util.function.Predicate that selects the items to remove, and after the call the body is true when an item was removed. A Hazelcast queue cannot remove through its iterator, so the producer tests the items the queue holds and then removes the matching ones in one call: the operation is not atomic.
Example for drain to:
-
Java
-
XML
-
YAML
from("direct:drainTo")
.setHeader("CamelHazelcastOperationType", constant("drainTo"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:drainTo"/>
<setHeader name="CamelHazelcastOperationType">
<constant>drainTo</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:drainTo
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: drainTo
- to:
uri: hazelcast-queue:bar The CamelHazelcastDrainToCollection header must hold the Collection the items are moved into: without it the producer fails. After the call the body is the number of items moved.
Example for take:
-
Java
-
XML
-
YAML
from("direct:take")
.setHeader("CamelHazelcastOperationType", constant("take"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:take"/>
<setHeader name="CamelHazelcastOperationType">
<constant>take</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:take
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: take
- to:
uri: hazelcast-queue:bar Example for retain all:
-
Java
-
XML
-
YAML
from("direct:retainAll")
.setHeader("CamelHazelcastOperationType", constant("retainAll"))
.to("hazelcast-queue:bar"); <route>
<from uri="direct:retainAll"/>
<setHeader name="CamelHazelcastOperationType">
<constant>retainAll</constant>
</setHeader>
<to uri="hazelcast-queue:bar"/>
</route> - route:
from:
uri: direct:retainAll
steps:
- setHeader:
name: CamelHazelcastOperationType
constant: retainAll
- to:
uri: hazelcast-queue:bar Queue consumer – from(“hazelcast-queue:foo”)
The queue consumer provides two different modes:
-
Poll
-
Listen
Sample for Poll mode
-
Java
-
XML
-
YAML
from("hazelcast-queue:foo?queueConsumerMode=Poll")
.to("mock:result"); <route>
<from uri="hazelcast-queue:foo?queueConsumerMode=Poll"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: hazelcast-queue:foo
parameters:
queueConsumerMode: Poll
steps:
- to:
uri: mock:result In this way, the consumer will poll the queue and return the head of the queue or null after a timeout.
In Listen mode instead, the consumer will listen for events on queue.
The queue consumer in Listen mode provides 2 operations: * add * remove
Sample for Listen mode
-
Java
-
XML
-
YAML
from("hazelcast-queue:mm")
.log("object...")
.choice()
.when(header("CamelHazelcastListenerAction").isEqualTo("added"))
.log("...added")
.to("mock:added")
.when(header("CamelHazelcastListenerAction").isEqualTo("removed"))
.log("...removed")
.to("mock:removed")
.otherwise()
.log("fail!"); <route>
<from uri="hazelcast-queue:mm"/>
<log message="object..."/>
<choice>
<when>
<simple>${header.CamelHazelcastListenerAction} == 'added'</simple>
<log message="...added"/>
<to uri="mock:added"/>
</when>
<when>
<simple>${header.CamelHazelcastListenerAction} == 'removed'</simple>
<log message="...removed"/>
<to uri="mock:removed"/>
</when>
<otherwise>
<log message="fail!"/>
</otherwise>
</choice>
</route> - route:
from:
uri: hazelcast-queue:mm
steps:
- log:
message: "object..."
- choice:
when:
- simple: "${header.CamelHazelcastListenerAction} == 'added'"
steps:
- log:
message: "...added"
- to:
uri: mock:added
- simple: "${header.CamelHazelcastListenerAction} == 'removed'"
steps:
- log:
message: "...removed"
- to:
uri: mock:removed
otherwise:
steps:
- log:
message: "fail!"