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);
        }
    }
}

模拟生产者

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. TCP协议是安全的吗?

    2024-03-15 12:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 12:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 12:14:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 12:14:01       20 阅读

热门阅读

  1. 字符串基础

    2024-03-15 12:14:01       17 阅读
  2. Spring MVC相关

    2024-03-15 12:14:01       15 阅读
  3. 23.查询所有列

    2024-03-15 12:14:01       17 阅读
  4. 前端协商缓存和强缓存

    2024-03-15 12:14:01       17 阅读
  5. JVM-3

    JVM-3

    2024-03-15 12:14:01      18 阅读
  6. python-0006-django路由

    2024-03-15 12:14:01       25 阅读
  7. Django 数据库表模型与迁移

    2024-03-15 12:14:01       20 阅读
  8. 题目 1124: C语言训练-大、小写问题

    2024-03-15 12:14:01       21 阅读
  9. Python网络爬虫实战:从入门到进阶

    2024-03-15 12:14:01       22 阅读
  10. QT c++ 海康红外热像仪

    2024-03-15 12:14:01       15 阅读