Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Camel 2.12使用RabbitMQ进行路由_Java_Rabbitmq_Apache Camel - Fatal编程技术网

Java Camel 2.12使用RabbitMQ进行路由

Java Camel 2.12使用RabbitMQ进行路由,java,rabbitmq,apache-camel,Java,Rabbitmq,Apache Camel,我一直在尝试使用2.12.1-SNAPSHOT中的RabbitMQComponent版本让camel路由。这样做,我可以轻松消费,但在路由到其他队列时会出现广告问题 CamelContext context = new DefaultCamelContext(); context.addComponent("rabbit-mq", factoryComponent()); from("rabbit-mq://localhost/test.exchange&queue=test.queue&u

我一直在尝试使用2.12.1-SNAPSHOT中的RabbitMQComponent版本让camel路由。这样做,我可以轻松消费,但在路由到其他队列时会出现广告问题

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(); CamelContext=新的DefaultCamelContext(); addComponent(“兔子mq”,factoryComponent()); 来自(“rabbitmq://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] 09:10:28119调试RabbitMQProducer[main]:-启动生产者:生产者[rabbit mq://localhost/out.queue?autoAck=false&autoDelete=false&durable=true&password=xxxxxx&routingKey=out.queue&username=guest] 09:10:48238调试RabbitMQProducer[Camel(Camel-1)线程#11-关机任务]:-停止producer:producer[rabbit mq://localhost/out.queue?autoAck=false&autoDelete=false&durable=true&password=xxxxxx&routingKey=out.queue&username=guest] 我花了很多时间研究RabbitMQ组件的Camel单元测试,但是我没有看到任何非常有用的东西。有人能让这个工作吗


谢谢。

我是用spring dsl做的。这是我使用的url。java dsl中不需要端口号吗

rabbitmq://localhost:5672/subscribeExchange?queue=subscribeQueue&durable=true&username=guest&password=guest&routingKey=subscribe

根据端口,端口是可选的


最好的

我遇到了同样的问题,尽管在最初的问题被问到5年后我一直在尝试。但我在这里发布了我是如何让它在其他人面临同样问题的情况下发挥作用的

问题是,即使我们将“routingKey”添加到URI中,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);

}