RabbitMQ(二)

一、目标

生产者发送消息,消费者接收消息,用最简单的方式实现。

官网说明参见下面超链接:

RabbitMQ tutorial - “Hello World!” — RabbitMQ
在这里插入图片描述

二、具体操作

1、创建Java工程

因为我之前创建过,所以会提示目录非空
完成后会得到一个空的项目,为了后续方便,我们不使用默认的model,而是将其作为一个父工程来使用,此时我们需要配置一下父工程中的pom.xml文件:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
    </parent>

这个操作是将父工程的父工程设置为spring,方便当前父工程中的子工程使用spring中的组件。
然后我们新建一个名为module01-hello-world的module:
在这里插入图片描述
在这里插入图片描述
module01-hello-world的pom.xml文件中加入如下依赖配置:

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

至此,完成该模块的环境准备工作,下面是业务代码。

2、发送消息

①Java代码

连接信息改成你自己的:

package com.xxx.rabbitmq.simple;

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

/**
 * @ClassName: Producer
 * @Package: com.xxx.rabbitmq.simple
 * @Author: 
 * @CreateDate: 
 * @Version: V1.0.0
 * @Description:
 */

public class Producer {
    public static void main(String[] args) throws Exception {

        // 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();

        // 设置主机地址
        connectionFactory.setHost("192.168.xx.xx");

        // 设置连接端口号:默认为 5672
        connectionFactory.setPort(5672);

        // 虚拟主机名称:默认为 /
        connectionFactory.setVirtualHost("/");

        // 设置连接用户名;默认为guest
        connectionFactory.setUsername("guest");

        // 设置连接密码;默认为guest
        connectionFactory.setPassword("123456");

        // 创建连接
        Connection connection = connectionFactory.newConnection();

        // 创建频道
        Channel channel = connection.createChannel();

        // 声明(创建)队列
        // queue      参数1:队列名称
        // durable    参数2:是否定义持久化队列,当 MQ 重启之后还在
        // exclusive  参数3:是否独占本次连接。若独占,只能有一个消费者监听这个队列且 Connection 关闭时删除这个队列
        // autoDelete 参数4:是否在不使用的时候自动删除队列,也就是在没有Consumer时自动删除
        // arguments  参数5:队列其它参数
        channel.queueDeclare("simple_queue", true, false, false, null);

        // 要发送的信息
        String message = "你好;小兔子!";

        // 参数1:交换机名称,如果没有指定则使用默认Default Exchange
        // 参数2:路由key,简单模式可以传递队列名称
        // 参数3:配置信息
        // 参数4:消息内容
        channel.basicPublish("", "simple_queue", null, message.getBytes());

        System.out.println("已发送消息:" + message);

        // 关闭资源
        channel.close();
        connection.close();

    }
}

②查看效果

在这里插入图片描述
在这里插入图片描述

3、接收消息

①Java代码

连接信息改成你自己的:

package com.xxx.rabbitmq.simple;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @ClassName: Consumer
 * @Package: com.xxx.rabbitmq.simple
 * @Author: 
 * @CreateDate: 
 * @Version: V1.0.0
 * @Description:
 */

public class Consumer {
    public static void main(String[] args) throws Exception {

        // 1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 2. 设置参数
        factory.setHost("192.168.xxx.xxx");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("guest");
        factory.setPassword("123456");

        // 3. 创建连接 Connection
        Connection connection = factory.newConnection();

        // 4. 创建Channel
        Channel channel = connection.createChannel();

        // 5. 创建队列
        // 如果没有一个名字叫simple_queue的队列,则会创建该队列,如果有则不会创建
        // 参数1. queue:队列名称
        // 参数2. durable:是否持久化。如果持久化,则当MQ重启之后还在
        // 参数3. exclusive:是否独占。
        // 参数4. autoDelete:是否自动删除。当没有Consumer时,自动删除掉
        // 参数5. arguments:其它参数。
        // channel.queueDeclare("simple_queue",true,false,false,null);

        // 接收消息
        DefaultConsumer consumer = new DefaultConsumer(channel){

            // 回调方法,当收到消息后,会自动执行该方法
            // 参数1. consumerTag:标识
            // 参数2. envelope:获取一些信息,交换机,路由key...
            // 参数3. properties:配置信息
            // 参数4. body:数据
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("consumerTag:"+consumerTag);
                System.out.println("Exchange:"+envelope.getExchange());
                System.out.println("RoutingKey:"+envelope.getRoutingKey());
                System.out.println("properties:"+properties);
                System.out.println("body:"+new String(body));

            }

        };

        // 参数1. queue:队列名称
        // 参数2. autoAck:是否自动确认,类似咱们发短信,发送成功会收到一个确认消息
        // 参数3. callback:回调对象
        // 消费者类似一个监听程序,主要是用来监听消息
        channel.basicConsume("simple_queue",true,consumer);

    }
}

②控制台打印

consumerTag:amq.ctag-8EB87GaZFP52LKSMcj98UA
Exchange:
RoutingKey:simple_queue
properties:#contentHeader(content-type=null, content-encoding=null, headers=null, delivery-mode=null, priority=null, correlation-id=null, reply-to=null, expiration=null, message-id=null, timestamp=null, type=null, user-id=null, app-id=null, cluster-id=null)
body:你好;小兔子!

③查看后台管理界面

因为消息被消费掉了,所以RabbitMQ服务器上没有了:
在这里插入图片描述
至此,我们就完成了使用RabbitMQ实现的“helloworld”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值