02 rabbitmq之hellolworld

上一节我们安装好了rabbitmq,本节我们将看一下rabbitmq的第一个简单程序。

1、前提约束

  • 已经安装rabbitmq
    https://www.jianshu.com/p/1efa0b1a7aa3
  • 会使用idea创建maven项目
    https://www.jianshu.com/p/042073b7710b

2、操作步骤

rabbitmq提供了各种语言的详尽的使用文档,路径如下:
https://www.rabbitmq.com/getstarted.html
我们马上要测试的这个程序架构如下:

helloworld

  • 在idea中创建一个maven项目,假设名称为rabbitmqtest
  • 在pom.xml中加入依赖:
        
            com.rabbitmq
            amqp-client
            5.4.3
        
  • 在src/main/java文件夹下创建包net.wanho.rabbitmq.helloworld
  • net.wanho.rabbitmq.helloworld包下面创建Send.java:
package net.wanho.rabbitmq.helloworld;

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

import java.nio.charset.StandardCharsets;


public class Send {

    public static void main(String[] argv) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //创建连接
        Connection connection = connectionFactory.newConnection();
        //创建一个管道
        Channel channel = connection.createChannel();
        //声明一个队列
        String queueName = "hello";
        /**
         * param1:队列名称
         * param2:是否持久化
         * param3:队列是否独占此连接
         * param4:队列不再使用时是否自动删除
         * param5:队列参数
         * 这里没有指定交换机,消息将发送给默认交换机,每个队列也会绑定那个默认的交换机,但是不能显 示绑定或解除绑定
         * 默认的交换机,routingKey等于队列名称
         */
        channel.queueDeclare(queueName, false, false, false, null);
        //声明一个消息
        String message = "Hello World!";
        /** 消息发布方法
         *  param1:Exchange的名称,如果没有指定,则使用Default Exchange
         *  param2:routingKey,消息的路由Key,是用于Exchange(交换机)将消息转发到指定的消息队列
         *  param3:消息包含的属性
         *  param4:消息体
         **/
        channel.basicPublish("", queueName, null, message.getBytes(StandardCharsets.UTF_8));
        System.out.println("send " + message);
    }
}

  • net.wanho.rabbitmq.helloworld包下面创建Recv.java:
package net.wanho.rabbitmq.helloworld;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Recv {


    public static void main(String[] argv) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //创建连接
        Connection connection = connectionFactory.newConnection();
        //创建一个管道
        Channel channel = connection.createChannel();
        //声明队列
        String queueName = "hello";
        channel.queueDeclare(queueName, false, false, false, null);

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //交换机
                String exchange = envelope.getExchange();
                //路由key
                String routingKey = envelope.getRoutingKey();
                //消息id
                long deliveryTag = envelope.getDeliveryTag();
                //消息内容
                String msg = new String(body, "utf-8");
                System.out.println("receive message:" + msg);
            }
        };

        /*** 监听队列String queue, boolean autoAck,Consumer callback *
         * 参数明细
         * 1、队列名称
         * 2、是否自动回复,
         * 设置为true为表示消息接收到自动向mq回复接收到了,mq接收到回复会删除消息,
         * 设置为false则需要手动回复
         * 3、消费消息的方法,消费者接收到消息后调用此方法
         * */
        channel.basicConsume(queueName, true, consumer);
    }
}
  • 测试
    先启动Send.java,再启动Recv.java,我们便能看到send的消息发送到了recv.
    以上就是我们使用rabbitmq完成的第一个简单的消息发送接收demo.

你可能感兴趣的:(02 rabbitmq之hellolworld)