Java 驼峰路由未按预期工作

Java 驼峰路由未按预期工作,java,spring,apache-camel,Java,Spring,Apache Camel,我尝试在一个兔子队列和另一个兔子队列之间创建一个非常简单的路由。消息从一个队列传递到第二个队列时不应进行任何处理。但由于未知原因,消息会一次又一次地重定向到第一个队列,而不是第二个队列 @Component public class CamelRouter extends SpringRouteBuilder { @Override public void configure() { from("rabbitmq://localhost/test-in?autoA

我尝试在一个兔子队列和另一个兔子队列之间创建一个非常简单的路由。消息从一个队列传递到第二个队列时不应进行任何处理。但由于未知原因,消息会一次又一次地重定向到第一个队列,而不是第二个队列

@Component
public class CamelRouter extends SpringRouteBuilder {
    @Override
    public void configure() {
        from("rabbitmq://localhost/test-in?autoAck=false&autoDelete=false&durable=true&exchangeType=fanout&queue=test-in&username=guest&password=xxx")
        .log(LoggingLevel.ERROR, "Output of message from Queue: ${in.body}")
        .to("rabbitmq://localhost/test-out?autoAck=false&autoDelete=false&durable=true&exchangeType=fanout&queue=test-out&username=guest&password=xxx");
    }
}
日志如下:

09:04:18.564 [thread] WARN  route1                                     - Output of message from Queue: test
09:04:18.700 [thread] WARN  route1                                     - Output of message from Queue: test
09:04:18.835 [thread] WARN  route1                                     - Output of message from Queue: test
09:04:18.968 [thread] WARN  route1                                     - Output of message from Queue: test
09:04:19.104 [thread] WARN  route1                                     - Output of message from Queue: test
09:04:19.238 [thread] WARN  route1                                     - Output of message from Queue: test
驼峰配置有什么问题?在我看来,它尽可能简单。

请尝试以下方法:

@Component
public class CamelRouter extends SpringRouteBuilder {
    @Override
    public void configure() {
        from("rabbitmq://localhost/test-in?autoAck=false&autoDelete=false&durable=true&exchangeType=fanout&queue=test-in&username=guest&password=xxx")
        .removeHeaders("rabbitmq.*")
        .log(LoggingLevel.ERROR, "Output of message from Queue: ${in.body}")
        .to("rabbitmq://localhost/test-out?autoAck=false&autoDelete=false&durable=true&exchangeType=fanout&queue=test-out&username=guest&password=xxx");
    }
}

Camel支持从消息头重写一些设置,如队列(RabbitMq组件就是这样做的),因此我们需要删除它们,以避免将消息发送回源队列。可以找到rabitmq头的完整列表。我猜“rabbitmq.REPLY_TO”头是有问题的头。

与其删除头,不如在exchange上使用out消息,如下所示。在这个特定的例子中,
rabbitmq
前缀可能是可以的,但是如果您在其他组件上尝试这种方法(imap是一个很好的例子),由于各种奇怪的原因,它将无法工作

    from("rabbitmq://localhost/test-in?autoAck=false&autoDelete=false&durable=true&exchangeType=fanout&queue=test-in&username=guest&password=xxx")
        .log(LoggingLevel.ERROR, "Output of message from Queue: ${in.body}")
        .process(new Processor() {

            @Override
            public void process(Exchange exchange) throws Exception {
                exchange.getOut().setBody(exchange.getIn().getBody());                  
            }
        })
        .to("rabbitmq://localhost/test-out?autoAck=false&autoDelete=false&durable=true&exchangeType=fanout&queue=test-out&username=guest&password=xxx");

你用什么版本的骆驼?它很有用。但是你能解释一下为什么有必要删除标题吗?删除标题可能会充满各种令人痛苦的错误。。更好的方法是使用out消息,我将添加另一个答案