簡體   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