java - 兔子处理程序 : How to catch "ListenerExecutionFailedException: Listener method ' no match' threw exception"correctly and proceed working

标签 java spring-boot rabbitmq

对于一个应用程序,我正在使用 Spring Boot 和 RabbitMQ 进行一些测试。 我设置了一个非常简单的发送者-接收者应用程序: 发件人:

public class Tut1Sender
{
    private final Gson gson = new Gson();

    @Autowired
    private RabbitTemplate template;

    @Autowired
    private Queue queue;

    public static int count = 1;

    @Scheduled(fixedDelay = 1000, initialDelay = 500)
    public void send() throws InterruptedException
    {
        String message = "Hello World! "+" Nr. "+count;
        MessageObject mo = new MessageObject(message);
        String toJson = gson.toJson(mo);
        this.template.convertAndSend(queue.getName(), toJson);
        System.out.println(" [x] Sent '" + toJson + "'");
        Thread.sleep(5);
        count++;
    }
}

这部分工作得很好,并用消息填充我的队列。

这是我的接收器:

@RabbitListener(queues = "hello")
public class Tut1Receiver
{

    private final Gson gson = new Gson();

    @RabbitHandler
    public void receive(String in) throws InterruptedException
    {
        System.out.println("Received Raw: " + in);
        MessageObject fromJson = gson.fromJson(in, MessageObject.class);

        System.out.println("Received Message '" + fromJson + "'");
        int nextInt = ThreadLocalRandom.current().nextInt(1000, 5000);
        System.out.println("Sleep for " + nextInt + " ms");
        Thread.sleep(nextInt);
    }
}

接收者正确处理发送者创建的消息。我得到了一个很好的输出,消息被确认并从队列中删除。

然后我通过RabbitMQ的Web-GUI直接将消息放入队列中。

发件人获取此消息。我可以这样说,因为我创建的消息从状态“Ready”切换到“Unacked”(如 Web-GUI 中所示) 发件人没有给我任何输出。

然后我配置了ContainerFactory:

@Profile("receiver")
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            ConnectionFactory connectionFactory)
    {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setErrorHandler(e -> {
            System.out.println("Error: "+e);
            System.out.println("Raw: "+((ListenerExecutionFailedException) e).getFailedMessage().toString());

        });
        return factory;
    }

现在我收到以下错误(无限循环)

Error: org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method 'no match' threw exception
Raw: (Body:'[B@53452feb(byte[11])' MessageProperties [headers={content_type=text/plain, content_encoding=UTF-8}, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=true, receivedExchange=, receivedRoutingKey=hello, deliveryTag=1, consumerTag=NOT_SET, consumerQueue=hello])

我该如何处理这个错误?发送者应该只显示错误,确认消息并继续处理下一条消息。

一般来说,处理错误消息的正确方法是什么?

最佳答案

对于损坏的消息,消费者可以拒绝传递消息。如果您确定损坏的消息无法被任何其他使用者处理,您应该告诉代理丢弃该消息或将其传递到死信交换。

来自official document of spring amqp ,我发现:

Another alternative is to set the container's rejectRequeued property to false. This causes all failed messages to be discarded. When using RabbitMQ 2.8.x or higher, this also facilitates delivering the message to a Dead Letter Exchange.

Or, you can throw a AmqpRejectAndDontRequeueException; this prevents message requeuing, regardless of the setting of the rejectRequeued property.

关于java - 兔子处理程序 : How to catch "ListenerExecutionFailedException: Listener method ' no match' threw exception"correctly and proceed working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57622635/

相关文章:

java - 如何处理并发数据库请求或如何在长时间运行的查询正在进行时运行短 SQL 查询

spring-boot - spring boot 从请求中获取不记名 token 并调用另一个微服务

python - 使用 RabbitMQ 有没有办法在没有出队操作的情况下查看队列内容?

spring - SpringBoot-微服务的Docker化+ Angular + MySQL + RabbitMQ

java - Flutter java.io.File android.app.Activity.getExternalFilesDir(java.lang.String)' 在空对象引用上

java - 将 XMLGregorianCalendar 转换为 GregorianCalendar 的正确方法

Java 8 上的 Spring Boot 应用程序的 Gradle 包装器失败,Gradle 3 : can't find cacerts

python - 无法启动 Python - ImportError : No module named posixpath

JavaFX 8 : primaryStage cannot be resolved

java - 用@Configuration 和@Controller 注释了一个类。在重构方面需要帮助