如何为同一个 Camel RouteBuilder 配置方法设置两个 RabbitMQ 连接 [英] How to set two RabbitMQ connections for the same Camel RouteBuilder configure method
问题描述
我想从兔子队列中侦听、处理并将消息发布到另一个兔子队列.我不和春天一起工作.消息在此配置中重复.
I want listen FROM rabbit queue, process, and post message TO another rabbit queue. I'm not working with spring. The messages are duplicated in this configuration.
这是一个简短的代码:
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("rabbitmq://localhost/B?autoDelete=false&queue=worker&threadPoolSize=1&autoAck=false").
log(LoggingLevel.INFO, "Message ${id}").
to("rabbitmq://localhost/B?autoDelete=false&queue=processed");
}
});
我也试过这个:
public static void main(String[] args) throws Exception {
org.apache.camel.impl.DefaultCamelContext context = new DefaultCamelContext();
com.rabbitmq.client.ConnectionFactory connectionFactoryWorker = new ConnectionFactory();
com.rabbitmq.client.ConnectionFactory connectionFactoryProcessed = new ConnectionFactory();
org.apache.camel.impl.SimpleRegistry registry = new SimpleRegistry();
String camelBeanNameWorker = "connectionFactoryWorker";
String camelBeanNameProcessed = "connectionFactoryProcessed";
registry.put(camelBeanNameWorker, connectionFactoryWorker);
registry.put(camelBeanNameProcessed, connectionFactoryProcessed);
context.setRegistry(registry);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("rabbitmq://localhost/B?connectionFactory=connectionFactoryWorker&autoDelete=false&queue=worker&threadPoolSize=1&autoAck=false").
log(LoggingLevel.INFO, "Message ${id}").
to("rabbitmq://localhost/B?connectionFactory=connectionFactoryProcessed&autoDelete=false&queue=processed");
}
});
context.start();
Thread.sleep(10000l);
context.stop();
}
Worker 队列以 4 条消息开始,但消息重新排队:
Worker queue start in four messages, but Messages are re-queued:
561 消息 ID-host-1510600494291-0-2
566 消息 ID-host-1510600494291-0-4
566 消息 ID-host-1510600494291-0-6
567 消息 ID-host-1510600494291-0-8
568 消息 ID-host-1510600494291-0-10
571 消息 ID-host-1510600494291-0-12
572 消息 ID-host-1510600494291-0-14
572 消息 ID-host-1510600494291-0-16
573 消息 ID-host-1510600494291-0-18
574 消息 ID-host-1510600494291-0-20
574 消息 ID-host-1510600494291-0-22
575 消息 ID-host-1510600494291-0-24
576 消息 ID-host-1510600494291-0-26
576 消息 ID-host-1510600494291-0-28
577 消息 ID-host-1510600494291-0-30
578 消息 ID-host-1510600494291-0-32
578 消息 ID-host-1510600494291-0-34
...
64k 后
...
10561 消息 ID-host-1510600494291-0-128690
10561 消息 ID-host-1510600494291-0-128692
10561 消息 ID-host-1510600494291-0-128694
10561 消息 ID-host-1510600494291-0-128696
561 Message ID-host-1510600494291-0-2
566 Message ID-host-1510600494291-0-4
566 Message ID-host-1510600494291-0-6
567 Message ID-host-1510600494291-0-8
568 Message ID-host-1510600494291-0-10
571 Message ID-host-1510600494291-0-12
572 Message ID-host-1510600494291-0-14
572 Message ID-host-1510600494291-0-16
573 Message ID-host-1510600494291-0-18
574 Message ID-host-1510600494291-0-20
574 Message ID-host-1510600494291-0-22
575 Message ID-host-1510600494291-0-24
576 Message ID-host-1510600494291-0-26
576 Message ID-host-1510600494291-0-28
577 Message ID-host-1510600494291-0-30
578 Message ID-host-1510600494291-0-32
578 Message ID-host-1510600494291-0-34
...
64k after
...
10561 Message ID-host-1510600494291-0-128690
10561 Message ID-host-1510600494291-0-128692
10561 Message ID-host-1510600494291-0-128694
10561 Message ID-host-1510600494291-0-128696
推荐答案
我相信我可能在另一个问题中找到了你的答案
I believe I may have found your answer in another question
显然设置 BridgeEnpoint=false 选项会阻止此操作.没有指定这个 EXCHANGE_NAME 和 ROUTING_KEY 标头是从接收到的消息中获取的,覆盖了您在to"配置中可能指定的任何内容.例如...<代码>rabbitmq://localhost/B?connectionFactory=connectionFactoryProcessed&autoDelete=false&queue=processed&BridgeEndpoint=false
Apparently setting BridgeEnpoint=false option stops this. Without specifying this the EXCHANGE_NAME and ROUTING_KEY headers are taken from the received message overriding whatever you might have specified in the "to" configuration. For example...
rabbitmq://localhost/B?connectionFactory=connectionFactoryProcessed&autoDelete=false&queue=processed&BridgeEndpoint=false
这篇关于如何为同一个 Camel RouteBuilder 配置方法设置两个 RabbitMQ 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!