1、 错误信息
at com.feng.rabbitmq.six.ReceiveLogsDirect01.main(ReceiveLogsDirect01.java:31)
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'console' in vhost '/', class-id=40, method-id=30)
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
2、 错误原因
绑定交换机这里应该使用 channel.queueBind("console",EXCHANGE_NAME,"info");
但是我却使用的是 channel.exchangeBind(queueName,EXCHANGE_NAME,"info");
3、错误源文件 ReceiveLogs.java
package com.feng.rabbitmq.six;
import com.feng.rabbitmq.utiils.RabbitMQUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
/**
* 消费者接收消息
* 2021-12-10 09:54
*/
public class ReceiveLogsDirect01 {
public static final String EXCHANGE_NAME = "direct_logs";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMQUtils.getChannel();
//声明一个交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
//声明一个队列
/**
* Caused by: com.rabbitmq.client.ShutdownSignalException:
* channel error; protocol method: #method<channel.close>(reply-code=404,
* reply-text=NOT_FOUND - no exchange 'console' in vhost '/', class-id=40, method-id=30)
*/
channel.queueDeclare("console", false, false, false, null);
// String queueName = channel.queueDeclare().getQueue();
//交换机和队列绑定
// channel.exchangeBind("console",EXCHANGE_NAME,"info");
channel.queueBind("console",EXCHANGE_NAME,"info");
// channel.exchangeBind(queueName,EXCHANGE_NAME,"info");
channel.exchangeBind("console",EXCHANGE_NAME,"warning");
//接收消息
DeliverCallback deliverCallback = (consumerTag,message) -> {
System.out.println("ReceiveLogsDirect01控制台打印接收到的消息:"+new String(message.getBody(),"utf-8"));
};
channel.basicConsume("console",true,deliverCallback,consumerTag -> {
System.out.println("消费者取消消息时回调");
});
}
}