Spring AMQP RabbitMQ RPC - 处理响应异常 [英] Spring AMQP RabbitMQ RPC - Handle response exceptions

查看:268
本文介绍了Spring AMQP RabbitMQ RPC - 处理响应异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RPC AMQP RabbitMQ 队列来发送和接收消息.问题是我设置了一个 setReplyTimeout 值.发生这种情况时,我会收到org.springframework.amqp.AmqpRejectAndDontRequeueException:超时后收到回复".我在传入队列上设置了 DLQ,但是当 spring 尝试在其自动创建的队列上返回消息时,似乎收到了异常.因此,如何在将消息发送回生产者时处理异常?理想情况下,我希望任何在发送到发送到 DLQ 的生产者时出现异常的消息.

I am trying to use a RPC AMQP RabbitMQ queue to send and receive messages. The problem is that I have set a setReplyTimeout value. When that happens I get a "org.springframework.amqp.AmqpRejectAndDontRequeueException: Reply received after timeout". I have a DLQ set up on the incoming queue, but it appears that the exception is received when spring tries to return the message on its queue that is automatically created. Thus how can I handle exceptions when sending messages back to a producer? Ideally I would want any message that gets an exception while being sent to a producer sent to a DLQ.

我正在使用

@RabbitListener(queues = QueueConfig.QUEUE_ALL, containerFactory = "containerFactoryQueueAll")

它需要一个没有 setQueues 的 SimpleRabbitListenerContainerFactory.另外rabbitTemplate 没有rabbitTemplate.setReplyQueue

It requires a SimpleRabbitListenerContainerFactory which does not have setQueues. Also rabbitTemplate does not have a rabbitTemplate.setReplyQueue

谢谢,布莱恩

推荐答案

不要使用带有直接回复伪队列的默认内置回复侦听器容器,而是使用 具有命名队列的回复侦听器容器,该队列配置为将无法传递的消息路由到 DLQ.

Instead of using the default built-in reply listener container with the direct reply-to pseudo queue, use a Reply Listener Container with a named queue that is configured to route undeliverable messages to a DLQ.

RabbitTemplate 被配置为容器的监听器:

The RabbitTemplate is configured as the container's listener:

@Bean
public RabbitTemplate amqpTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
    rabbitTemplate.setMessageConverter(msgConv());
    rabbitTemplate.setReplyQueue(replyQueue());
    rabbitTemplate.setReplyTimeout(60000);
    rabbitTemplate.setUseDirectReplyToContainer(false);
    return rabbitTemplate;
}

@Bean
public SimpleMessageListenerContainer replyListenerContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory());
    container.setQueues(replyQueue());
    container.setMessageListener(amqpTemplate());
    return container;
}

@Bean
public Queue replyQueue() {
    return new Queue("my.reply.queue");
}

注意文档需要更新,但你也需要

Note that the documentation needs to be updated, but you also need

rabbitTemplate.setUseDirectReplyToContainer(false);

重要

如果您有多个客户端实例,每个实例都需要自己的回复队列.

If you have multiple instances of the client, each needs its own reply queue.

这篇关于Spring AMQP RabbitMQ RPC - 处理响应异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆