当前位置: 代码网 > it编程>编程语言>Java > RabbitMQ的Direct Exchange模式实现的消息发布案例(示例代码)

RabbitMQ的Direct Exchange模式实现的消息发布案例(示例代码)

2024年09月12日 Java 我要评论
producer生产者代码import com.rabbitmq.client.channel;import com.rabbitmq.client.connection;import com.rab

producer生产者代码

import com.rabbitmq.client.channel;
import com.rabbitmq.client.connection;
import com.rabbitmq.client.connectionfactory;
public class rabbitmqproducer {
    private final static string exchange_name = "direct_message_exchange";
    private final static string exchange_type = "direct";
    public static void main(string[] args) {
        // 1. 创建连接工厂,设置连接参数
        connectionfactory connectionfactory = new connectionfactory();
        connectionfactory.sethost("localhost");
        connectionfactory.setport(5672); // rabbitmq默认端口
        connectionfactory.setusername("guest");
        connectionfactory.setpassword("guest");
        try (connection connection = connectionfactory.newconnection();
             channel channel = connection.createchannel()) {
            // 2. 声明交换机 (direct类型,持久化)
            channel.exchangedeclare(exchange_name, exchange_type, true);
            // 3. 声明队列 (持久化,非独占,连接断开时不自动删除)
            channel.queuedeclare("queue5", true, false, false, null);
            channel.queuedeclare("queue6", true, false, false, null);
            channel.queuedeclare("queue7", true, false, false, null);
            // 4. 绑定队列到交换机,设置路由键
            channel.queuebind("queue5", exchange_name, "order");
            channel.queuebind("queue6", exchange_name, "order");
            channel.queuebind("queue7", exchange_name, "course");
            // 5. 准备要发送的消息
            string message = "你好,学相伴:www.kuangstudy.com";
            // 6. 向交换机发送消息,使用路由键 "course"
            channel.basicpublish(exchange_name, "course", null, message.getbytes("utf-8"));
            system.out.println("消息发送成功!");
        } catch (exception ex) {
            // 捕获异常并打印堆栈信息
            ex.printstacktrace();
            system.out.println("消息发送出现异常...");
        } finally {
            // 在try-with-resources中,不再需要显式关闭连接和通道
            // 会自动关闭连接和通道
        }
    }
}

功能点:

  • 声明了一个direct类型的交换机,并绑定了三个队列(queue5queue6queue7)。其中queue5queue6都绑定到order路由键,而queue7绑定到course路由键。
  • 发送了一条消息到course路由键绑定的队列中(即queue7)。

consumer消费者代码

import com.rabbitmq.client.channel;
import com.rabbitmq.client.connection;
import com.rabbitmq.client.connectionfactory;
import com.rabbitmq.client.delivercallback;
public class rabbitmqconsumer {
    private final static string queue_name = "queue7"; // 与生产者的绑定一致
    private final static string exchange_name = "direct_message_exchange";
    private final static string exchange_type = "direct";
    public static void main(string[] args) {
        // 1. 创建连接工厂,设置连接参数
        connectionfactory connectionfactory = new connectionfactory();
        connectionfactory.sethost("localhost");
        connectionfactory.setport(5672); // rabbitmq默认端口
        connectionfactory.setusername("guest");
        connectionfactory.setpassword("guest");
        try (connection connection = connectionfactory.newconnection();
             channel channel = connection.createchannel()) {
            // 2. 声明交换机和队列,与生产者保持一致
            channel.exchangedeclare(exchange_name, exchange_type, true);
            channel.queuedeclare(queue_name, true, false, false, null);
            // 3. 绑定队列到交换机,路由键为"course"
            channel.queuebind(queue_name, exchange_name, "course");
            system.out.println(" [*] 等待接收消息...");
            // 4. 定义接收消息的回调函数
            delivercallback delivercallback = (consumertag, delivery) -> {
                string message = new string(delivery.getbody(), "utf-8");
                system.out.println(" [x] 接收到的消息: '" + message + "'");
                // 这里可以添加进一步的消息处理逻辑
            };
            // 5. 开始消费消息 (自动应答)
            channel.basicconsume(queue_name, true, delivercallback, consumertag -> { });
        } catch (exception ex) {
            // 捕获异常并打印堆栈信息
            ex.printstacktrace();
            system.out.println("消费者运行中出现异常...");
        }
    }
}

功能点: 

   1.  与生产者保持一致:消费者的队列名称、交换机名称和路由键与生产者保持一致,即监听queue7队列,并接收路由键为course的消息。

   2. 回调函数处理消息:使用delivercallback来定义收到消息后的处理逻辑。在回调函数中,delivery.getbody()获取消息内容,随后可以对消息进行处理、存储或其他业务逻辑操作。

   3 自动应答basicconsume中的true表示自动应答(auto-acknowledge),即消息处理完毕后,rabbitmq会自动确认消息已成功处理。如果需要手动应答,可以将true替换为false,并在处理完成后调用channel.basicack()来手动确认消息。

到此这篇关于rabbitmq的direct exchange模式实现的消息发布案例的文章就介绍到这了,更多相关rabbitmq direct exchange消息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com