RabbitMQ学习之Java客户端连接测试

前面花了几天纠结完RabbitMQ在Linux下的安装之后,开始找简单的例子来测试RabbitMQ和Java的连接。 和前面的安装一样,问题依旧。因为网上的帖子大多数都是很正常的步骤,并且没有贴出来自己途中可能遇到的低级错误。 本文将沿用网上很经典的一个帖子来说出我过程中出现的低级问题。

如果您和我一样是一个Linux和RabbitMQ的新手,首先不要一开始就想的太好太完美,首先从最简单的做起。 不要去找那些看上去很高大上的帖子,也不要一开始就和maven以及spring去集成。就写简单的main方法,来测试是否能够很顺利的从Java连接成功RabbitMQ。

下面的例子是网上的,但是不知道原贴到底是谁的,我也稍微加工一下,弥补新手遗漏的一些信息。

1.首先需要去官网下载Java需要的jar包,建议先不要用maven依赖,慢慢来。 依次进入下面的路径,这个是我现在最新的jar包路径,下载解压之后里面就会有jar包,然后导入到项目中即可。 http://www.rabbitmq.com/releases/rabbitmq-java-client/

2.创建Java项目,并写连接类,下面两个类为网上经典的类。

import java.io.IOException; 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 {  
    ConnectionFactory factory = new ConnectionFactory();  
    factory.setHost("10.10.6.246");
    factory.setUsername("admin");
    factory.setPassword("admin");
    factory.setPort(5672);
    Connection connection = factory.newConnection();  
    Channel channel = connection.createChannel();  

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);  
    String message = "Hello World!";  
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());  
    System.out.println(" [x] Sent '" + message + "'");  

    channel.close();  
    connection.close();  
}  

}


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

public class Reqv { private final static String QUEUE_NAME = "hello";

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();

    factory.setUsername("admin");
    factory.setPassword("admin");
    factory.setHost("10.10.6.246");
    factory.setVirtualHost("/"); 
    factory.setPort(5672);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);
    while(true){
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
    }
}

}

看似简单的例子,如果RabbitMQ安装的时候没有弄好的话,依旧还是可能出现一些问题的,以下就是我的问题。

3.出现的问题。 >>>第一个

Exception in thread "main" com.rabbitmq.client.PossibleAuthenticationFailureException: Possibly caused by authentication failure at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:355) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:516) at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:533) at com.mq.test.Reqv.main(Reqv.java:30) Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; reason: java.net.SocketException: Connection reset at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343) at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216) at com.rabbitmq.client.impl.AMQChannel.rpc(AMQChannel.java:202) at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:347) ... 3 more Caused by: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.io.BufferedInputStream.fill(Unknown Source) at java.io.BufferedInputStream.read(Unknown Source) at java.io.DataInputStream.readUnsignedByte(Unknown Source) at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95) at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:131) at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:515)

解决方案: >1.是不是觉得自己的用户完全没有问题,web端也能正常的连接上,用户名,端口换了很多次,依旧还是不解到底是什么问题呢? >2.此问题就是连接失败,认证失败,如果觉得自己所有的东西都已经弄好了,很有可能就是RabbitMQ的用户权限出现了问题。

这个是我之前的用户状态,很明显这个地方用户权限是有问题的,因为新手和无知,所以也找不出为什么。

rabbitmqctl set_permissions -p VHostPath【虚拟主机路径 ‘/’】 User【用户名字】 "." "." ".*"

/etc/init.d/rabbitmq-server restart

重新设置用户的权限之后重启RabbitMQ,再次运行是否成功了呢?如果运行结果出现了:

[x]Sent 'hello world!'


[*] Waiting for messages. To exitpress CTRL+C

[x] Received 'hello world!'

那么恭喜你终于解决了连接的问题了。

第二个

Exception in thread "main" java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124) at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:768) at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:61) at com.mq.test.Reqv.main(Reqv.java:32) Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'hello' in vhost '/' refused for user 'admin', class-id=50, method-id=10), null, ""} at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343) at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118) ... 3 more Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'hello' in vhost '/' refused for user 'admin', class-id=50, method-id=10), null, ""} at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:474) at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:315) at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144) at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91) at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:533)

第二个问题和第一个基本上差不多,意思就是无法访问队列信息,所以很有可能还是用户权限的问题。如果按照上面的设置已经解决好了,事实上不会出现 这个问题了,所以解决方式和第一个是一样的。

4.给出一篇RabbitMQ权限设置的帖子,以供参考。


已有 0 条评论

    感谢参与互动!