首页 > 其他分享 >Publish/Subscribe(发布/订阅),一次向多个消费者发送消息

Publish/Subscribe(发布/订阅),一次向多个消费者发送消息

时间:2022-11-20 11:22:36浏览次数:57  
标签:订阅 String rabbitmq Subscribe client Publish import com channel

image

生产者

package org.example.pubsub;

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

public class EmitLog {
    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.121.200");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);

            for (int i = 0; i < 10; i++) {
                String message = "发布消息" + i;

                channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
                System.out.println(" [生产者] Sent '" + message + "'");
            }

        }
    }
}

消费者1

package org.example.pubsub;

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

public class ReceiveLogs1 {
    private static final String EXCHANGE_NAME = "logs";
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.121.200");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();


         channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();

        // queueBind用于将队列与交换机绑定
        // 参数1:队列名 参数2:交互机名  参数三:路由key(暂时用不到)
        channel.queueBind(queueName, EXCHANGE_NAME, "");

        System.out.println(" [生产者1] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [生产者1] Received '" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
    }
}

消费者2

package org.example.pubsub;

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

public class ReceiveLogs2 {
    private static final String EXCHANGE_NAME = "logs";
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.121.200");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

         channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();

        // queueBind用于将队列与交换机绑定
        // 参数1:队列名 参数2:交换机名  参数三:路由key(暂时用不到)
        channel.queueBind(queueName, EXCHANGE_NAME, "");

        System.out.println(" [生产者2] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [生产者2] Received '" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> { });
    }
}

标签:订阅,String,rabbitmq,Subscribe,client,Publish,import,com,channel
From: https://www.cnblogs.com/xl4ng/p/16908086.html

相关文章

  • Jenkins 使用Publish over SSH远程执行命令无法停止
    Jenkins使用PublishoverSSH,远程执行命令时,如果执行的命令中有nohupjava-jartest.jar类似的命令,可能会导致一直在打印日志。Jenkins无法停止,直到超时。解决方案:......
  • uniapp小程序通过wx.requestSubscribeMessage实现订阅消息
    wx.requestSubscribeMessage官网地址:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html#%E8%AE%A2%E9%98%85%E6%B6%88%E......
  • 订阅发布者模式-消息中心
    单例模板publicabstractclassSingleton<T>whereT:new(){privatestaticTinstance;publicstaticTInstance{get{if(insta......
  • SpringBoot事件监听机制及发布订阅模式详解
    业务需求:用户注册成功之后,系统会给用户发放优惠券,发送邮件,发送短信等操作。作为开发人员,很容易写出如下代码:/***用户注册逻辑**@authorLynch*/@GetMapping(......
  • 4、发布和订阅
    1、什么是发布和订阅Redis发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。Redis客户端可以订阅任意数量的频道。2、Redis的发布......
  • 美区id苹果arcade订阅教程
    首先,国内是不支持苹果arcade订阅的,目前普遍都是使用美区账号订阅,所以,大家如果想开通苹果arcade订阅,需要先准备一个美区账号,下面给大家介绍美区账号苹果arcade订阅流程。......
  • 基于Spring的发布订阅模式 EventListener
    基于Spring的发布订阅模式在我们使用spring开发应用时,经常会碰到要去解耦合一些依赖调用,比如我们在做代码的发布流程中,需要去通知相关的测试,开发人员关注发布中的错误信息......
  • Affinity Publisher 永久版(设计排版软件)v2.0.0中文版mac/win
    AffinityPublisher中文版是一款版面排版设计工具!AffinityPublisher拥有母版页、对页跨页、网格、表格、高级排版、文本流、全专业打印输出和其他惊人功能等基本功能,无论......
  • error: GH007: Your push would publish a private email address.
    背景在新电脑上推送时出现这个邮件地址提示。解决方法简单的方法是直接把.gitconfig这个文件中的用户名和地址都改掉,改为网站提供的保密邮件地址。如果涉及到gith......
  • 【设计模式】692- TypeScript 设计模式之发布-订阅模式
    前言在之前两篇自测清单中,和大家分享了很多JavaScript基础知识,大家可以一起再回顾下~本文是我在我们团队内部“「现代JavaScript突击队」”分享的一篇内容,第二期学习内......