Caused by:com.rabbitmq.client.ShutdownSignalException: connection error;(reply-code=530, reply-text=NOT_ALLOWED

学习rabbitMQ时 自己写了一个发送者 案例 运行后报错:

Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method<connection.close>(reply-code=530, reply-text=NOT_ALLOWED - access to vhost '/' refused for user 'admin', class-id=10, method-id=40)

代码如下:

package helloWorld;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class send {
	private final static String QUEUE_NAME="hello";
	public static void main(String[] args) throws IOException, TimeoutException {
		//创建链接工厂
		ConnectionFactory factory = new ConnectionFactory();
		//设置rabbitMQ地址
		factory.setHost("127.0.0.1");
		factory.setUsername("admin");
		factory.setPassword("123456");
		//建立连接
		Connection connection = factory.newConnection();
		//获取信道
		Channel channel = connection.createChannel();
		//声明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		//发布消息
		String message = "Hello World 2";
		channel.basicPublish("", QUEUE_NAME	, null, message.getBytes("UTF-8"));
		System.out.println("发送了消息"+message);
		//关闭链接
		channel.close();
		connection.close();
	}
}

  从报错看应该获取链接时报错了 看了下我配置的 用户信息

因为我的virtual host 自己学习的时候修改了下配置 不是默认的 / 改为 /1

加上了代码 factory.setVirtualHost("/1");  重新测试可以成功发送了

原文地址:https://www.cnblogs.com/zjf6666/p/14672487.html