spring boot rabbitmq 接收消息时异常死循环

spring boot rabbitmq的使用很方便,只需要两个注解即可实现,这里就不赘述了,网上有很多资料

不过在使用时,我发现有些情况与我看的一些资料有出入

如果在发送消息时,调用的是AmqpTemplate.convertAndSend("",Object),那么你就需要增加一个配置做Json转换,不然在接收时消息异常,然后消息无法被消费,一直消费会造成死循环,具体配置可以看官方资料,因为我用String传输的,所以没有写这个配置

我看网上的资料都是说需要配置消费手动确认,不然出现异常的消息将会被丢弃,但实际上并不是这样,我这边的版本是1.5.10

实际上在@RabbitHandler注解的方法中抛出任何异常,都会造成当条消息无法被消费,下一次仍然消费上一条异常的消息,而如果每次消费这条消息都抛出了异常,那么这条消息的消费将会进入死循环

在方法内加入try catch即可避免此种状况

还有一种情况是try catch没有捕获到异常也会进入死循环的

我因为业务需要在接收消息的类里面注入了一个service,然后接收方法调用了service的方法,在这个service方法中有一个try catch,没有将异常抛出,然后有一次我发现service方法报错了,@RabbitHandler一直在消费同一条消息,显然也是进入了死循环

这种情况将捕获到的异常抛出即可

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring BootRabbitMQ 结合使用可以方便地构建基于消息队列的异步通信系统。在 Spring Boot 中,RabbitMQ 提供了一个简单易用的整合方案,让你能够轻松地处理消息的生产者(Producer)和消费者(Consumer)。 1. 引入依赖:首先,你需要在你的 Maven 或 Gradle 项目中添加 Spring AMQP 和 RabbitMQ 的依赖。例如,Maven 项目的 pom.xml 文件中添加: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置 RabbitMQ:在 application.properties 或 application.yml 文件中配置 RabbitMQ 的连接信息,包括主机名、端口号、队列名称等。 ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 3. 创建 RabbitMQ 的配置类:Spring Boot 会自动扫描该类下的配置,比如创建 RabbitTemplate 实例。 ```java import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfig { @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory factory = new CachingConnectionFactory(); factory.setHost("localhost"); factory.setUsername("guest"); factory.setPassword("guest"); return factory; } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); return template; } } ``` 4. 生产者(发送消息):使用 `RabbitTemplate` 发送消息到指定队列。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class RabbitProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message, String queueName) { rabbitTemplate.convertAndSend(queueName, message); } } ``` 5. 消费者(接收消息):创建一个消费者类,监听指定队列,并处理接收到的消息。 ```java import org.springframework.amqp.annotation.DeleteQueue; import org.springframework.amqp.annotation.QueueBind; import org.springframework.amqp.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @QueueBind(value = "myQueue") public class RabbitConsumer { @RabbitListener public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值