PashaDoro
PashaDoro

Reputation: 61

Apache Camel ?transacted=true

Hi Camel/jms developers. Using Apache Camel amqp jms connector. And as a Broker ActiveMQ.

My configuration is quite default.

Here is a consumer code example:

 public static void main(String[] args) throws Exception {
    AMQPComponent amqpComponent = AMQPComponent.amqpComponent(HOST, USER, PWD);
    CamelContext context = new DefaultCamelContext();
    context.addComponent("amqp", amqpComponent);
    context.start();
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {
            from("amqp:queue:1test.queue?transacted=true")
                .to("stream:out")
            .end();
        }
    });
    Thread.sleep(20*1000);
    context.stop();
}

Easy to see, I have configured transacted consumer. for 1test.queue. When Im running it, in log see:

[main] INFO org.apache.camel.impl.DefaultCamelContext - Route: route1 started and consuming from: amqp://queue:1test.queue?transacted=true
[AmqpProvider :(1):[amqp:HOST2]] INFO org.apache.qpid.jms.sasl.SaslMechanismFinder - Best match for SASL auth was: SASL-PLAIN
[AmqpProvider :(1):[amqp:HOST2]] INFO org.apache.qpid.jms.JmsConnection - Connection ID:...:1 connected to remote Broker: amqp:HOST2
[AmqpProvider :(2):[amqp:HOST2]] INFO org.apache.qpid.jms.sasl.SaslMechanismFinder - Best match for SASL auth was: SASL-PLAIN
[AmqpProvider :(2):[amqp:HOST2]] INFO org.apache.qpid.jms.JmsConnection - Connection ID:...:2 connected to remote Broker: amqp:HOST2
[AmqpProvider :(3):[amqp:HOST2]] INFO org.apache.qpid.jms.sasl.SaslMechanismFinder - Best match for SASL auth was: SASL-PLAIN
[AmqpProvider :(3):[amqp:HOST2]] INFO org.apache.qpid.jms.JmsConnection - Connection ID:...:3 connected to remote Broker: amqp:HOST2

If I removing ?transacted=true from consumer

[AmqpProvider :(1):[amqp:HOST2]] INFO org.apache.qpid.jms.sasl.SaslMechanismFinder - Best match for SASL auth was: SASL-PLAIN
[AmqpProvider :(1):[amqp:HOST2]] INFO org.apache.qpid.jms.JmsConnection - Connection ID:...:1 connected to remote Broker: amqp:HOST2

It appears only once.

How to explain this behavior? This is normally for transacted consumers in camel?

Thank you.

P.S Checked this topic but not sure how to map it to Camel reality.

Upvotes: 1

Views: 1234

Answers (2)

Roman Martin
Roman Martin

Reputation: 127

AMQP Camel Component has not defined a pooled connection factory so it is creating a new connection for each iteration to check if there are messages in the broker.

To avoid it you should define a CachingConnectionFactory to the AMQPComponent as:

import org.apache.camel.component.amqp.AMQPComponent;
import org.apache.camel.component.jms.JmsConfiguration;

import org.apache.qpid.jms.JmsConnectionFactory;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jms.connection.CachingConnectionFactory;

@Bean
public JmsConnectionFactory jmsConnectionFactory() {
    JmsConnectionFactory jmsConnectionFactory = new JmsConnectionFactory(brokerUser, brokerPassword, brokerUrl);

    return jmsConnectionFactory;
}

@Bean
@Primary
public CachingConnectionFactory jmsCachingConnectionFactory(JmsConnectionFactory jmsConnectionFactory) {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(jmsConnectionFactory);

    return cachingConnectionFactory;
}

@Bean
public JmsConfiguration jmsConfig(CachingConnectionFactory cachingConnectionFactory) {
    JmsConfiguration jmsConfiguration = new JmsConfiguration();

    jmsConfiguration.setConnectionFactory(cachingConnectionFactory);
    jmsConfiguration.setCacheLevelName("CACHE_CONSUMER");

    return jmsConfiguration;
}

@Bean
public AMQPComponent amqpComponent(JmsConfiguration jmsConfiguration) {
    AMQPComponent amqpComponent = new AMQPComponent();

    amqpComponent.setConfiguration(jmsConfiguration);

    return amqpComponent;
}

You will get the same behavior as JMS Camel Component.

More info at AMQP Camel Component page

Upvotes: 3

Rahul Bansal
Rahul Bansal

Reputation: 5

Use jms insted of amqp.

I had similar problem. But when I use JMS instead of AMQP it worked well there was log only one time i.e single connection got created.

Seems some issue is there with AMQ component.

Thanks, Rahul

Upvotes: 0

Related Questions