如何为相同的Camel RouteBuilder配置方法设置两个RabbitMQ连接

时间:2017-11-13 11:36:22

标签: rabbitmq apache-camel

我希望从兔子队列中侦听,处理并将消息发布到另一个兔子队列。我不是在和春天一起工作。消息在此配置中重复。

这是一个简短的代码:

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

工作队列以四条消息开始,但消息重新排队:

  

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

2 个答案:

答案 0 :(得分:0)

在不知道结果的情况下查看您的代码,我注意到当您尝试使用注册表中的bean时,您错过了您错过了“#”。

使用注册表中的内容时需要做的是

public static void main(String[] args) throws Exception {
DefaultCamelContext context = new DefaultCamelContext();

ConnectionFactory connectionFactory = new ConnectionFactory();
SimpleRegistry registry = new SimpleRegistry();
String beanNameWorker = "connectionFactoryWorker";
String beanNameProcessed = "connectionFactoryProcessed";
registry.put(beanNameWorker, connectionFactory);
registry.put(beanNameProcessed, connectionFactory);
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");
    }
});

}

顺便说一下,实际结果是什么?你有什么错误?

答案 1 :(得分:0)

我相信我可能在另一个问题中找到了答案

Infinite loop in Camel - Rabbit MQ

显然设置BridgeEnpoint = false选项会停止此操作。如果不指定此项,则EXCHANGE_NAME和ROUTING_KEY标头将从收到的消息中获取,该消息将覆盖您在"中指定的任何内容。组态。例如... rabbitmq://localhost/B?connectionFactory=connectionFactoryProcessed&autoDelete=false&queue=processed&BridgeEndpoint=false