• RabbitMQ 模拟实现【六】:程序模拟实现


    模拟实现

    模拟消费者

    package com.example.demo.demo;
    
    import com.example.demo.common.Consumer;
    import com.example.demo.common.MqException;
    import com.example.demo.mqclient.Channel;
    import com.example.demo.mqclient.Connection;
    import com.example.demo.mqclient.ConnectionFactory;
    import com.example.demo.mqsever.core.BasicProperties;
    import com.example.demo.mqsever.core.ExchangeType;
    
    import java.io.IOException;
    
    /*
     * 这个类表示一个消费者.
     * 通常这个类也应该是在一个独立的服务器中被执行
     */
    public class DemoConsumer {
        public static void main(String[] args) throws IOException, MqException, InterruptedException {
            System.out.println("启动消费者!");
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("127.0.0.1");
            factory.setPort(9090);
    
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            channel.exchangeDeclare("testExchange", ExchangeType.DIRECT, true, false, null);
            channel.queueDeclare("testQueue", true, false, false, null);
    
            channel.basicConsume("testQueue", true, new Consumer() {
                @Override
                public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws MqException, IOException {
                    System.out.println("[消费数据] 开始!");
                    System.out.println("consumerTag=" + consumerTag);
                    System.out.println("basicProperties=" + basicProperties);
                    String bodyString = new String(body, 0, body.length);
                    System.out.println("body=" + bodyString);
                    System.out.println("[消费数据] 结束!");
                }
            });
    
            // 由于消费者也不知道生产者要生产多少, 就在这里通过这个循环模拟一直等待消费.
            while (true) {
                Thread.sleep(500);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    模拟生产者

    package com.example.demo.demo;
    
    import com.example.demo.mqclient.Channel;
    import com.example.demo.mqclient.Connection;
    import com.example.demo.mqclient.ConnectionFactory;
    import com.example.demo.mqsever.core.ExchangeType;
    
    import java.io.IOException;
    
    /*
     * 这个类用来表示一个生产者.
     * 通常这是一个单独的服务器程序.
     */
    public class DemoProducer {
        public static void main(String[] args) throws IOException, InterruptedException {
            System.out.println("启动生产者");
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("127.0.0.1");
            factory.setPort(9090);
    
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            // 创建交换机和队列
            channel.exchangeDeclare("testExchange", ExchangeType.DIRECT, true, false, null);
            channel.queueDeclare("testQueue", true, false, false, null);
    
            // 创建一个消息并发送
            byte[] body = "hello".getBytes();
            boolean ok = channel.basicPublish("testExchange", "testQueue", null, body);
            System.out.println("消息投递完成! ok=" + ok);
    
            Thread.sleep(500);
            channel.close();
            connection.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    效果展示

    启动结果如下:
    在这里插入图片描述在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    Java基于微信小程序的自习室系统的设计,附源码、教程
    空间金字塔池化Spatial Pyramid Pooling
    【计算机视觉】尺度不变特征变换(SIFT)
    Linux系统下的Swift与Ceph分布式存储解决方案
    解决使用Quartz执行的任务对象(job)中无法注入bean的问题
    【LeetCode】36、有效的数独
    c++ 中的函数指针
    【LeetCode力扣】189 53 轮转数组 | 最大子数组和
    openGauss+KeepAlived(故障转移)
    python中sql拼接时对字段可能为空的处理思路
  • 原文地址:https://blog.csdn.net/m0_65038072/article/details/136707155