Having in Apache Camel an JMS consumer with 1 ClientID and 2 Topics. How to realize it?

66 Views Asked by At

I have a simple durable subscriber listening to 2 JMS topics using 1 clientID:

@Bean
public void init() {

    ConnectionFactory connectionFactory = ...
    try {
        Connection connection = connectionFactory.createConnection();
        connection.setExceptionListener(eifErrorListener.defineExceptionListener());
        connection.setClientID("user");
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TopicSubscriber topicSubscriber1 = subscribe(session, topic1, SubName1);
        TopicSubscriber topicSubscriber2 = subscribe(session, topic2, SubName2);
        if (topicSubscriber1.getMessageListener() != null && topicSubscriber2.getMessageListener() != null) {
            logger.info("Successfully connected and subscribed with " + topicSubscriber1.getTopic().getTopicName() + " and " + topicSubscriber2.getTopic().getTopicName());
        }
    } catch (JMSException e) {
        eifErrorListener.defineExceptionListener().onException(e);
    }
}

private TopicSubscriber subscribe(Session session, String TopicName, String SubName) throws JMSException {
    Topic topic = session.createTopic(TopicName);
    TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, SubName);
    topicSubscriber.setMessageListener( new EIFMessageListner());
    return topicSubscriber;
}

Now I want to transfer this example into Apache Camel, but I always get this error message:

ClientID already connected from ...

Is there a possibility to achieve the same in Apache Camel having one client ID but 2 topics?

Any help, especially a small example would be really appreciated.

I tried the following code, but I receive the aforementioned error message.

EDIT: I have reworked my code to incorporate the idea from Justin, but unfortunately I receive the same error message when running the same:

javax.jms.InvalidClientIDException: Broker: localhost - Client: myClientID1 already connected from tcp://172.20.0.1:43100

Is there any idea what can be done to keep 1 ClientID with 2 topics like it is possible in my example above?

Thanks a lot in advance.

 public void configure() throws Exception {

        CamelContext context = new DefaultCamelContext();

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory ("failover:(tcp://localhost:61617,tcp://localhost:61618)?randomize=false");
        connectionFactory.setClientID("myClientID1");

        // Setting Use Single Connection to True
        ActiveMQComponent amqComponent = ActiveMQComponent.activeMQComponent();
        amqComponent.setUseSingleConnection(true);
        amqComponent.setConnectionFactory(connectionFactory);
        amqComponent.setConsumerType(ConsumerType.Simple);

        context.addComponent("active-mq", amqComponent);
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                // Configuring Routes
                from("active-mq:topic:TopicA?durableSubscriptionName=DSN1")
                        .to("log:receivedMessages");

                from("active-mq:topic:TopicB?durableSubscriptionName=DSN2")
                        .to("log:receivedMessages");
            }
        });
        context.start();
    }
0

There are 0 best solutions below