当前位置:   article > 正文

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: ......

caused by: com.rabbitmq.client.shutdownsignalexception: channel error; proto

在启动RabbitMQ消费端的时候报错:Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'messageChange' in vhost '/': received 'fanout' but current is 'direct', class-id=40, method-id=10)

 详细信息如下:

  1. F:\jdk1.8.0_151\bin\java.exe "-javaagent:E:\IntelliJ IDEA 2019.2.1\lib\idea_rt.jar=58077:E:\IntelliJ IDEA 2019.2.1\bin" -Dfile.encoding=UTF-8 -classpath F:\jdk1.8.0_151\jre\lib\charsets.jar;F:\jdk1.8.0_151\jre\lib\deploy.jar;F:\jdk1.8.0_151\jre\lib\ext\access-bridge-64.jar;F:\jdk1.8.0_151\jre\lib\ext\cldrdata.jar;F:\jdk1.8.0_151\jre\lib\ext\dnsns.jar;F:\jdk1.8.0_151\jre\lib\ext\jaccess.jar;F:\jdk1.8.0_151\jre\lib\ext\jfxrt.jar;F:\jdk1.8.0_151\jre\lib\ext\localedata.jar;F:\jdk1.8.0_151\jre\lib\ext\nashorn.jar;F:\jdk1.8.0_151\jre\lib\ext\sunec.jar;F:\jdk1.8.0_151\jre\lib\ext\sunjce_provider.jar;F:\jdk1.8.0_151\jre\lib\ext\sunmscapi.jar;F:\jdk1.8.0_151\jre\lib\ext\sunpkcs11.jar;F:\jdk1.8.0_151\jre\lib\ext\zipfs.jar;F:\jdk1.8.0_151\jre\lib\javaws.jar;F:\jdk1.8.0_151\jre\lib\jce.jar;F:\jdk1.8.0_151\jre\lib\jfr.jar;F:\jdk1.8.0_151\jre\lib\jfxswt.jar;F:\jdk1.8.0_151\jre\lib\jsse.jar;F:\jdk1.8.0_151\jre\lib\management-agent.jar;F:\jdk1.8.0_151\jre\lib\plugin.jar;F:\jdk1.8.0_151\jre\lib\resources.jar;F:\jdk1.8.0_151\jre\lib\rt.jar;F:\workspace\rabbitmqDemo\rabbitmqConsumer02\target\test-classes;F:\repository_ssh\org\springframework\boot\spring-boot-starter-logging\2.1.0.RELEASE\spring-boot-starter-logging-2.1.0.RELEASE.jar;F:\repository_ssh\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\repository_ssh\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\repository_ssh\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;F:\repository_ssh\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;F:\repository_ssh\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;F:\repository_ssh\com\rabbitmq\amqp-client\5.7.0\amqp-client-5.7.0.jar;F:\repository_ssh\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar xyfer.Consumer02
  2. java.io.IOException
  3. at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:129)
  4. at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:125)
  5. at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:147)
  6. at com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:783)
  7. at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.exchangeDeclare(AutorecoveringChannel.java:242)
  8. at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.exchangeDeclare(AutorecoveringChannel.java:232)
  9. at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.exchangeDeclare(AutorecoveringChannel.java:212)
  10. at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.exchangeDeclare(AutorecoveringChannel.java:217)
  11. at xyfer.Consumer02.main(Consumer02.java:28)
  12. Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'messageChange' in vhost '/': received 'fanout' but current is 'direct', class-id=40, method-id=10)
  13. at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
  14. at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
  15. at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:502)
  16. at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:293)
  17. at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:141)
  18. ... 6 more
  19. Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'messageChange' in vhost '/': received 'fanout' but current is 'direct', class-id=40, method-id=10)
  20. at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:522)
  21. at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:346)
  22. at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:182)
  23. at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:114)
  24. at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:672)
  25. at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:48)
  26. at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:599)
  27. at java.lang.Thread.run(Thread.java:748)

仔细看一下报错信息:Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'messageChange' in vhost '/': received 'fanout' but current is 'direct', class-id=40, method-id=10)

大概意思是交换机的类型不匹配,消费端应该收到名称为“messageChange”,类型为“fanout”的交换机发送的消息,但是当前RabbitMQ中存在的名称为“messageChange”的交换机的类型却是“direct”,所以交换机的类型不匹配。

打开RabbitMQ可以看到确实是这样:

目前有两种解决方式:

一、更改消费端代码中声明的交换机名称即可;

二,把RabbitMQ中的同名却不同类型的交换机删除即可;

消费端代码如下,交换机名称为“messageChange”,类型为“fanout”;

  1. package xyfer;
  2. import com.rabbitmq.client.*;
  3. import java.io.IOException;
  4. import java.util.concurrent.TimeoutException;
  5. public class Consumer02 {
  6. //Publish/subscribe发布订阅模式
  7. private static final String QUEUE_SMS ="queueSms";
  8. private static final String EXCHANGE = "messageChange"; 同名
  9. public static void main(String[] args) {
  10. Connection connection = null;
  11. Channel channel = null;
  12. try {
  13. ConnectionFactory connectionFactory = new ConnectionFactory();
  14. connectionFactory.setHost("127.0.0.1");
  15. connectionFactory.setPort(5672);
  16. connection = connectionFactory.newConnection();
  17. channel = connection.createChannel();
  18. //通道绑定交换机
  19. /**
  20. * 参数明细
  21. * 1、交换机名称
  22. * 2、交换机类型,fanout、topic、direct、headers
  23. */
  24. //Publish/subscribe发布订阅模式
  25. channel.exchangeDeclare(EXCHANGE, BuiltinExchangeType.FANOUT); 不同类型
  26. //通道绑定队列
  27. /**
  28. * 声明队列,如果Rabbit中没有此队列将自动创建
  29. * param1:队列名称
  30. * param2:是否持久化
  31. * param3:队列是否独占此连接
  32. * param4:队列不再使用时是否自动删除此队列
  33. * param5:队列参数
  34. * String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
  35. *
  36. */
  37. channel.queueDeclare(QUEUE_SMS,true,false,false,null);//通道绑定短信队列
  38. //交换机和队列绑定
  39. /**
  40. * 参数明细
  41. * 1、队列名称
  42. * 2、交换机名称
  43. * 3、路由key
  44. */
  45. //Publish/subscribe发布订阅模式
  46. channel.queueBind(QUEUE_SMS,EXCHANGE,"");
  47. DefaultConsumer consumer = new DefaultConsumer(channel) {
  48. /**
  49. * 消费者接收消息调用此方法
  50. * @param consumerTag 消费者的标签,在channel.basicConsume()去指定
  51. * @param envelope 消息包的内容,可从中获取消息id,消息routingkey,交换机,消息和重传标志
  52. (收到消息失败后是否需要重新发送)
  53. * @param properties
  54. * @param body
  55. * @throws IOException
  56. * String consumerTag, Envelope envelope, BasicProperties properties, byte[] body
  57. */
  58. @Override
  59. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
  60. //交换机
  61. String exchange = envelope.getExchange();
  62. //路由key
  63. String routingKey = envelope.getRoutingKey();
  64. envelope.getDeliveryTag();
  65. String msg = new String(body,"utf-8");
  66. System.out.println("mq收到的消息是:"+msg );
  67. }
  68. };
  69. System.out.println("消费者启动成功!");
  70. channel.basicConsume(QUEUE_SMS,true,consumer);
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. } catch (TimeoutException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/496758
推荐阅读
相关标签
  

闽ICP备14008679号