繁体   English   中英

使用RabbitMQ的骆驼2.12路由

[英]Camel 2.12 routing with RabbitMQ

我一直在尝试使用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单元测试,但没有发现任何极有价值的用途。 有人能使它正常工作吗?

谢谢。

我是用Spring DSL做到的。 这是我使用的网址。 java dsl中的端口号不是必需的吗?

rabbitmq://本地主机:5672 / subscribeExchange?queue = subscribeQueue&durable = true&username = guest&password = guest&routingKey = subscribe

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

最好

尽管自提出原始问题以来已经尝试了5年,但我还是遇到了同样的问题。 但是在这里发布我是如何工作的,以防其他人遇到相同的问题。

问题是,即使我们向URI添加“ routingKey”,rabbitmq路由密钥也不会更改。 诀窍是在发送前添加标题。 如果您记录了接收消息和正在发送的消息,我们可以清楚地看到路由密钥是相同的。

下面是我的代码。 它将从“ 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);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM