在文档中 https://docs.spring.io/spring-amqp/reference/htmlsingle/ 我明白了
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "myQueue", durable = "true"),
exchange = @Exchange(value = "auto.exch", ignoreDeclarationExceptions = "true"),
key = "orderRoutingKey")
)
public void processOrder(Order order) {
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue,
exchange = @Exchange(value = "auto.exch"),
key = "invoiceRoutingKey")
)
public void processInvoice(Invoice invoice) {
}
这里有1个队列和2个另外的路由键,每个人都为他的方法 但是我的代码没有从密钥中获取消息!
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = DRIVER_QUEUE, durable = "true"),
exchange = @Exchange(value = "exchange", ignoreDeclarationExceptions = "true", autoDelete = "true"),
key = "order")
)
public String getOrders(byte[] message) throws InterruptedException {
System.out.println("Rout order");
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = DRIVER_QUEUE, durable = "true"),
exchange = @Exchange(value = "exchange", ignoreDeclarationExceptions = "true", autoDelete = "true"),
key = "invoice")
)
public String getOrders(byte[] message) throws InterruptedException {
System.out.println("Rout invoice");
}
他们都从队列中获取消息而不看密钥...... 网站发送队列消息与密钥“发票”,我在控制台“路线顺序”中看到 什么问题??非常感谢!
rabbitmq 3.7.3 春天4.2.9 org.springframework.amqp 1.7.5
答案 0 :(得分:2)
错误是您将所有邮件发送到同一队列。
您应该为每个侦听器使用不同的队列。您的绑定只是告诉RK ="发票"和RK ="顺序"必须在同一队列上,而不是该侦听器使用该RK处理队列元素。
你应该绑定例如通过密钥"发票"交换到DRIVER_QUEUE1(例如"排队订单")并通过密钥" order"交换到DRIVER_QUEUE2(例如" queue-invoices")。通过这种方式,您可以分隔消息,并且可以放置两个侦听器,一个用于发票,一个用于订单。例如。像这样的东西:
@RabbitListener(queues = "queue-orders")
public void handleOrders(@Payload Order in,
@Header(AmqpHeaders.RECEIVED_ROUTING_KEY) String key) {
logger.info("Key: {}, msg: {}",key,in.toString());
}
@RabbitListener(queues = "queue-invoices")
public void handleInvoices(@Payload Invoice in,
@Header(AmqpHeaders.RECEIVED_ROUTING_KEY) String key) {
logger.info("Key: {}, msg: {}",key,in.toString());
}
我不喜欢完整的注释,因为当进行代理配置时,IMHO完全注释变得无用(或更好,添加额外检查对我来说无用)。但如果您愿意,整个注释应该像
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "queue-orders", durable = "true"),
exchange = @Exchange(value = "exchange", ignoreDeclarationExceptions = "true", autoDelete = "true"),
key = "invoice")
)
然后您可以通过convertAndSend(exchangename,routingkey,object)发送消息,如
Order order = new Order(...);
rabbitTemplate.convertAndSend("exchange", "order", order);
Invoice invoice = new Invoice(...);
rabbitTemplate.convertAndSent("exchange", "invoice", invoice);
如果您的启动应用程序实现RabbitListenerConfigurer,那么您可以配置所有内容,例如
@SpringBootApplication
public class MyApplication implements RabbitListenerConfigurer {
// other config stuff here....
@Bean("queue1")
public Queue queue1() {
return new Queue("queue-orders", true);
}
@Bean("queue2")
public Queue queue2() {
return new Queue("queue-invoices", true);
}
@Bean
public Binding binding1(@Qualifier("queue1") Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("invoice");
}
@Bean
public Binding binding2(@Qualifier("queue2") Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("order");
}
@Bean
public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
return rabbitTemplate;
}
@Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
factory.setMessageConverter(consumerJackson2MessageConverter());
return factory;
}
@Override
public void configureRabbitListeners(final RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}
// Exchange.
@Bean
public TopicExchange exchange() {
return new TopicExchange("exchange");
}
}
希望能够回答您的要求。