使用RabbitMQ进行Camel 2.12路由

时间:2013-09-03 15:23:35

标签: java rabbitmq apache-camel

我一直在尝试使用2.12.1-SNAPSHOT中的RabbitMQComponent版本进行路由。这样做,我已经能够轻松消费,但在路由到另一个队列时会出现广告问题。

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();

在此,我已经验证是否使用适当的路由键配置了指定的交换。我已经注意到我能够在体积上消耗,但不能产生out.queue。

以下是对处理邮件的RabbitMQProducer的唯一引用。

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]

我花了很多时间研究RabbitMQ组件的Camel单元测试,但我没有看到任何非常有价值的用途。有没有人能够让这个工作?

感谢。

3 个答案:

答案 0 :(得分:0)

我使用spring dsl做到了。这是我使用的网址。 java dsl中不需要端口号吗?

的RabbitMQ://本地主机:5672 / subscribeExchange队列= subscribeQueue&安培;耐用=真安培;用户名=旅客&安培;密码=旅客&安培; routingKey =订阅

答案 1 :(得分:0)

根据http://camel.apache.org/rabbitmq.html,端口是可选的。

最佳

答案 2 :(得分:0)

我遇到了同样的问题,尽管自从问到最初的问题以来我已经尝试了5年。但是在这里发布我是如何让它工作的,其他任何人都面临同样的问题。

问题是,即使我们添加' routingKey'到URI。诀窍是在发送之前添加标题。如果您记录正在发送的消息接收和消息,我们可以清楚地看到路由密钥是相同的。

以下是我的代码。它将从' receiveQueue'中读取消息。并发送到" 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);

}