您的位置 首页 java

Java,Windows安装RabbitMQ,客户端HelloWorld测试代码

Windows安装 RabbitMQ

OTP下载:

Erlang .org/downloads

RabbitMQ是Erlang语言开发。

环境变量配置:ERLANG_HOME

%ERLANG_HOME%\bin

RabbitMQ下载:

rabbitmq -server/releases/download/v3.9.4/rabbitmq-server-3.9.4.exe

安装&启动:

 cd D:\ProgramFiles\RabbitMQServer\rabbitmq_server-3.9.4\sbin
查看状态:> rabbitmqctl status
安装插件(第一次):> rabbitmq-plugins.bat enable rabbitmq_management
启动服务:rabbitmq-server.bat  

访问服务

访问地址:,guest/guest

客户端 HelloWorld 测试代码

pom.xml

 <dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.7.0</version>
</dependency>  

消费者,监听接收

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

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class HelloWorldConsumer {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        //rabbitmq监听IP
        factory.setHost("127.0.0.1");
        //rabbitmq默认监听端口,注意要记得开启端口
        factory.setPort(5672);

        //设置访问的用户
        factory.setUsername("guest");
        factory.setPassword("guest");
        //建立连接
        Connection conn = factory.newConnection();
        //创建消息通道
        Channel channel = conn.createChannel();
        //创建hello队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" Waiting for msg....");
        //创建消费者,并接受消息
         Consumer  consumer = new DefaultConsumer(channel) {
            @ Override 
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,  byte [] body)
                    throws IOException {
                String msg = new String(body, "UTF-8");
                System.out.println("Received is = '" + msg + "'");
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }

}  

main方法运行。

生产者,发送消息

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

public class HelloWorldProducer {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        //rabbitmq监听IP
        factory.setHost("127.0.0.1");
        //rabbitmq默认监听端口,注意要记得开启端口
        factory.setPort(5672);
        //设置访问的用户
        factory.setUsername("guest");
        factory.setPassword("guest");
        //建立连接
        Connection conn = factory.newConnection();
        //创建消息通道
        Channel channel = conn.createChannel();
        String msg = " hello world !!!! 你好啊~";
        //创建hello队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        //发送消息
        channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
        System.out.println("send msg " + msg + " to [" + QUEUE_NAME + "] queue !");
        channel.close();
        conn.close();
    }

}  

main方法运行,消息已发送,消费者console打印消息。

文章来源:智云一二三科技

文章标题:Java,Windows安装RabbitMQ,客户端HelloWorld测试代码

文章地址:https://www.zhihuclub.com/198946.shtml

关于作者: 智云科技

热门文章

发表回复

您的电子邮箱地址不会被公开。

网站地图