2
votes

I've been attempting to get camel to route using the RabbitMQComponent releases in the 2.12.1-SNAPSHOT. In doing so, I've been able to consume easily, but have ad issues when routing to another queues.

CamelContext context = new DefaultCamelContext();

context.addComponent("rabbit-mq", factoryComponent());

from("rabbit-mq://localhost/test.exchange&queue=test.queue&username=guest&password=guest&autoDelete=false&durable=true")
.log("${in.body}")
.to("rabbit-mq://localhost/out.queue&routingKey=out.queue&durable=true&autoAck=false&autoDelete=false&username=guest&password=guest")
.end();

In this, I've verified that there the specified exchanges are configured with the appropriate routing keys. I've noted that I'm able to consume in volume, but not able to produce to the out.queue.

The following are the only reference to the RabbitMQProducer that would process the message.

09:10:28,119 DEBUG RabbitMQProducer[main]: - Starting producer: Producer[rabbit-mq://localhost/out.queue?autoAck=false&autoDelete=false&durable=true&password=xxxxxx&routingKey=out.queue&username=guest]
09:10:48,238 DEBUG RabbitMQProducer[Camel (camel-1) thread #11 - ShutdownTask]: - Stopping producer: Producer[rabbit-mq://localhost/out.queue?autoAck=false&autoDelete=false&durable=true&password=xxxxxx&routingKey=out.queue&username=guest]

I've spent time looking into the Camel unit tests for the RabbitMQ component, but I've seen nothing of extremely valuable use. Has anyone been able to get this to work?

Thanks.

3

3 Answers

0
votes

i did it using spring dsl. Here's the url that I used. Isn't port number necessary in java dsl ?

rabbitmq://localhost:5672/subscribeExchange?queue=subscribeQueue&durable=true&username=guest&password=guest&routingKey=subscribe

0
votes

I came across the same issue even though I'm trying after 5 years since the original question was asked. But posting here how I got it working incase anyone else face the same issue.

The problem is, rabbitmq routing key doesn't gets changed even though we add the 'routingKey' to the URI. The trick was to add a header before sending out. If you log the message receive and message which is being sent out we can clearly see the routing key is the same.

Below is my code. It will read the message from 'receiveQueue' and send to 'sendQueue'

@Value("${rabbit.mq.host}")
private String host;

@Value("${rabbit.mq.port}")
private int port;

@Value("${rabbit.mq.exchange}")
private String exchange;

@Value("${rabbit.mq.receive.queue}")
private String receiveQueue;

@Value("${rabbit.mq.send.queue}")
private String sendQueue;


public void configure() throws Exception {
    String uriPattern = "rabbitmq://{0}:{1}/{2}?queue={3}&declare=false";
    String fromUri = MessageFormat.format(uriPattern, host, port, exchange, receiveQueue);
    String toUri = MessageFormat.format(uriPattern, host, port, exchange, sendQueue);

    from(fromUri).to("log:Incoming?showAll=true&multiline=true").
            unmarshal().json(JsonLibrary.Gson, Message.class).bean(MessageReceiver.class).to("direct:out");

    from("direct:out").marshal().json(JsonLibrary.Gson).setHeader("rabbitmq.ROUTING_KEY",
            constant(sendQueue)).to(toUri);

}