RabbitMQ/JAVA 客户端连接测试

这里是一个简单的helloworld测试。

这里在eclipse平台中实现 

细节不再赘述。重点是导入rabbitmq-java-client的jar包 下载地址:http://www.rabbitmq.com/releases/rabbitmq-java-client/v3.1.5/rabbitmq-java-client-bin-3.1.5.zip

将里面的jar包全部导入工程中。如下:

接下来先看生产者的代码:

package rabbitmq;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
public class Send {
     private final static String QUEUE_NAME = "hello";
      public static void main(String[] args) throws java.io.IOException {  
            /** 
             * 创建连接连接到MabbitMQ 
             */  
            ConnectionFactory factory = new ConnectionFactory();  
            // 设置MabbitMQ所在主机ip或者主机名  
            factory.setHost("123.206.216.31");  
            factory.setPort(5672);
            factory.setUsername("admin");
            factory.setPassword("admin");
            // 创建一个连接  
            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();  
        }  

}

 消费者的代码:

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

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.QueueingConsumer;
public class Recv {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException  {
        
        // TODO Auto-generated method stub
         ConnectionFactory factory = new ConnectionFactory();  
            factory.setHost("123.206.216.31");  
            factory.setPort(5672);
            factory.setUsername("admin");
            factory.setPassword("admin");
            // 打开连接和创建频道,与发送端一样  
            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 + "'");  
            }  

    }

}

不是一次性通过的。端口号搞错了。应该是5672 写成了15672。

为什么写成15672是因为我在web登录时用的是这个端口号。后来经过百度得到

默认安装的Rabbit MQ 监听端口是5672  下面是官网查到的:

当然也会有其他类型的错误,用户权限在rabbitmq安装的时候没有配置好。注意一下

最后贴一下运行结果:

原文地址:https://www.cnblogs.com/tinmh/p/6042167.html