9
votes

I have camel and rabbitmq configured like the following and it is working. I am looking to improve the config setup.

pom.xml

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-rabbitmq-starter</artifactId>
    <version>2.19.1</version>
</dependency>

application.yml

spring: 
  rabbitmq:
    host: rabbithost-url
    port: 5672
    username: my-user
    password: my-password

config bean

@Configuration
public class CamelConfig {

    @Resource private Environment env;

    @Bean
    public ConnectionFactory rabbitConnectionFactory(){
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(env.getProperty("spring.rabbitmq.host"));
        connectionFactory.setPort(Integer.valueOf(env.getProperty("spring.rabbitmq.port")));
        connectionFactory.setAutomaticRecoveryEnabled(true);
        // more config options here etc
        return connectionFactory;
    }
}

Route Example

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {

     from("direct:startQueuePoint")
          .id("idOfQueueHere")
          .to("rabbitmq://rabbithost-url:5672/TEST-QUEUE.exchange?queue=TEST-QUEUE.queue&autoDelete=false&connectionFactory=#rabbitConnectionFactory")
          .end();
    }
}

Would like to improve the following? Or at least see if its possible?

1) How do I leverage spring boot autowiring. I feel like im duplicating beans by added the custom CamelConfig > rabbitConnectionFactory? Its not using the RabbitAutoconfiguration?

2) When I am using the connection factory I am referencing the rabbitmq-url and port twice? I am adding it in the rabbitConnectionFactory bean object and in the camel url? e.g.

.to("rabbitmq://rabbithost-url:5672/ ..etc.. &connectionFactory=#rabbitConnectionFactory")

can I not just reference it once in the connection factory? tried the following without the host as its included in connectionFactory but it did not work.

.to("rabbitmq://TEST-QUEUE.exchange?queue=TEST-QUEUE.queue&autoDelete=false&connectionFactory=#rabbitConnectionFactory")

The 1st working example I am using is based off this. camel.apache.org/rabbitmq example (see Custom connection factory )

1

1 Answers

4
votes

Found from looking at the newer docs on github.

Notice that now no url needed in the start of the route.

.to(rabbitmq:exchangeName?options

Full camel route example below

.to(rabbitmq:exchangeName?queueName&exchangeType=direct&connectionFactory=#rabbitConnectionFactory&autoDelete=false

Here is url:

https://github.com/apache/camel/blob/master/components/camel-rabbitmq/src/main/docs/rabbitmq-component.adoc