项目实战 — 消息队列(9){编写demo程序}

本文介绍了使用SpringBoot构建一个消息队列服务器,展示了如何通过`TigerMqApplication`启动服务器,以及`DemoProducer`和`DemoConsumer`如何实现在不同主机间的生产者消费者模型,包括创建交换机、队列并发送和接收消息。
摘要由CSDN通过智能技术生成

消息队列服务器核心功能就是,提供了虚拟主机,交换机, 队列,消息等概念的管理,实现三种典型的消息转发方式,可以实现跨主机/服务器之间的生产者消费模型。

这里,就编写一个demo,实现跨主机的生产者消费者模型。

🍅 完善服务器的启动类

@SpringBootApplication
public class TigerMqApplication {
    public static ConfigurableApplicationContext context;

    public static void main(String[] args) throws IOException {
        context = SpringApplication.run(TigerMqApplication.class, args);

        BrokerServer brokerServer = new BrokerServer(9090);
        brokerServer.start();
    }
}

🍅 创建Demo程序

 

/*
* 表示一个生产者
* */
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,TigerMQ".getBytes();
        boolean ok = channel.basicPublish("testExchange","testQueue",null,body);
        System.out.println("消息投递完成!ok = " + ok);

        Thread.sleep(500);
        channel.close();
        connection.close();
    }
}

/*
* 表示一个消费者
* */
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);
        }

    }
}

🍅 启动服务器和客户端程序

启动服务器

 启动生产者和消费者

启动生产者
[Connection] 发送请求! type=1, length=188
[Connection] 收到响应! type=1, length=192
[Connection] 发送请求! type=3, length=512
[Connection] 收到响应! type=3, length=192
[Connection] 发送请求! type=5, length=349
[Connection] 收到响应! type=5, length=192
[Connection] 发送请求! type=9, length=437
[Connection] 收到响应! type=9, length=192
消息投递完成!ok = true
[Connection] 发送请求! type=2, length=188
[Connection] 收到响应! type=2, length=192
[Connection] 连接正常断开!

Process finished with exit code 0
启动消费者!
[Connection] 发送请求! type=1, length=188
[Connection] 收到响应! type=1, length=192
[Connection] 发送请求! type=3, length=512
[Connection] 收到响应! type=3, length=192
[Connection] 发送请求! type=5, length=349
[Connection] 收到响应! type=5, length=192
[Connection] 发送请求! type=10, length=315
[Connection] 收到响应! type=10, length=192
[Connection] 收到响应! type=12, length=528
[消费数据]开始!
consumerTag = C-4e9d5324-c197-462a-a0a5-31ffe3bf929a
basicProperties = BasicProperties(messageId=M-69e805c0-8298-4e8f-b737-001c340e18d5, routingKey=testQueue, deliverMode=1)
body = hello,TigerMQ
[消费数据]结束!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值