jdkar
jdkar

Reputation: 21

Apache Camel Hazelcast queue polling

This is a first time I am using Hazelcast distributed cache (queue) via apache camel. I created a route builder where I configured a hazelcast-queue endpoint as described in apache camel documentation. Camel route is started but it wasn't connecting to hazelcast (client configuration) instance. it was always starting new hazelcast (in build /embedded) from library.

I tried passing Hazelcast instance (client config) in route setter using hazel constants host, port, HazelcastInstance name, etc.., but all tries were unsuccessful.

Sample code

fromF("hazelcast-%sfoo?queueConsumerMode=Poll", HazelcastConstants.QUEUE_PREFIX)
    .setHeader(HazelcastConstants.INSTANCE_HOST, constant("127.0.0.1"))
    .setHeader(HazelcastConstants.INSTANCE_PORT, constant(5701))
    .setHeader(HazelcastConstants.HAZELCAST_INSTANCE_NAME_PARAM, constant("hazelcastinstance"))
    .log("::>: Camel log " + body());

Camel started and listening [10.67.113.160]:5701 instead [127.0.0.1]:5701.

Upvotes: 1

Views: 658

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

Assuming that you have a running Hazelcast cluster, what you want to achieve can be done with Spring Boot by following the next steps:

1. Add the dependency camel-hazelcast-starter to your project

With maven, you would add the next dependency

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-hazelcast-starter</artifactId>
</dependency>

2. Create your specific HazelcastInstance

In the example below, it creates a bean called customHazelcastConfig of type HazelcastInstance built from the client configuration file custom-client-hazelcast-config.xml that has been added to the root of the classpath.

@Component
public class HazelcastConfigProvider {

    @Bean
    public HazelcastInstance customHazelcastConfig() {
        return HazelcastClient.newHazelcastClient(
            new ClientClasspathXmlConfig("custom-client-hazelcast-config.xml")
        );
    }
}

3. Specify your HazelcastInstance to your Camel endpoint

In the example below, we indicate camel-hazelcast to retrieve the HazelcastInstance from the registry using customHazelcastConfig as name corresponding to our specific instance created in step #2.

fromF(
 "hazelcast-%sfoo?queueConsumerMode=Poll&hazelcastInstance=#customHazelcastConfig",
  HazelcastConstants.QUEUE_PREFIX
).log("Receiving: ${body}");

4. Switch in client mode (Optional)

By default the component camel-hazelcast is in node mode or cluster mode. To switch in client mode simply add camel.component.hazelcast-queue.hazelcast-mode=client to your application.properties

This step is optional and could be skipped


More details can be found from https://camel.apache.org/components/next/hazelcast-queue-component.html

Upvotes: 0

Related Questions