RabbitMQ —— 简单队列

前文

消息中间件 —— 简介

RabbitMQ —— 介绍

RabbitMQ —— 下载、安装

RabbitMQ —— 工作模式

创建 Maven 工程

在这里插入图片描述

添加 Maven 依赖

<dependencies>

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

  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
  </dependency>

  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.4</version>
  </dependency>

  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
</dependencies>

连接配置

package com.java.rabbitmq.util;

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

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

/**
 * @author Woo_home
 * @create 2020/5/26 11:18
 */

public class ConnectionUtils {

    /**
     * 获取 MQ 的连接
     * @return
     */
    public static Connection getConnection() throws IOException, TimeoutException {
        // 定义一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 设置服务地址
        factory.setHost("localhost");

        // 设置端口号(AMQP)
        factory.setPort(5672);

        // 设置 virtual hosts
        factory.setVirtualHost("/vhost");

        // 设置用户名
        factory.setUsername("admin");

        // 设置密码
        factory.setPassword("admin");

        return factory.newConnection();
    }
}

生产者

package com.java.rabbitmq.simple;

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

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

/**
 * @author Woo_home
 * @create 2020/5/26 11:24
 */

public class Send {

	// 设置队列名称
    private static final String QUEUE_NAME = "test_simple_queue";

    public static void main(String[] args) throws IOException, TimeoutException {

        // 获取一个连接
        Connection connection = ConnectionUtils.getConnection();

        // 从连接中获取一个通道
        Channel channel = connection.createChannel();

        // 创建队列声明
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        String msg = "hello simple...";

		// 发送消息
        channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());

        System.out.println("--- send msg : " + msg);

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

运行该程序
在这里插入图片描述

测试

刷新一下 Queue 界面 http://localhost:15672/#/queues

此时 RabbitMQ 控制台的 Queue 界面已经有了一个新的队列 test_simple_queue在这里插入图片描述
在该界面点击 GET MESSAGE 就可以获取消息
在这里插入图片描述

消费者

package com.java.rabbitmq.simple;

/**
 * @author Woo_home
 * @create 2020/5/26 12:36
 */

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;


/**
 * 消费者获取消息
 */
public class Receive {

    // 队列名称
    private static final String QUEUE_NAME = "test_simple_queue";

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

        // 获取连接
        Connection connection = ConnectionUtils.getConnection();

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

        // 定义队列的消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);

        // 监听队列
        channel.basicConsume(QUEUE_NAME, true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String msgString = new String(delivery.getBody());
            System.out.println("msg : " + msgString);
        }
    }
}

输出:
在这里插入图片描述

新的 API(DefaultConsumer)

在上面的消费者代码中,QueueingConsumer 已经弃用了,但也是可以继续使用的,然而既然是已经弃用了的,那么就用新的吧,使用 DefaultConsumer,如下:

package com.java.rabbitmq.simple;

/**
 * @author Woo_home
 * @create 2020/5/26 12:36
 */

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;


/**
 * 消费者获取消息
 */
public class Receive {

    // 队列名称
    private static final String QUEUE_NAME = "test_simple_queue";

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

        // 获取连接
        Connection connection = ConnectionUtils.getConnection();

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

        // 队列声明
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String s = new String(body, "utf-8");
                System.out.println("receive : " + s);
            }
        };

        // 监听队列
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}

在这里插入图片描述


相关 MQ 文章阅读

ActiveMQ 下载、安装

ActiveMQ —— Java 连接 ActiveMQ(点对点)

ActiveMQ —— Java 连接 ActiveMQ(发布订阅 Topic)

ActiveMQ —— Broker

ActiveMQ —— Spring 整合 ActiveMQ

SpringBoot 整合 ActiveMQ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值