RabbitMq-Demo-03-订阅模式

1、 代码编写:

1、生产者代码:
/**
 * @className Producer_PubSub
 * @author Echo
 * @description 订阅模式-生产者
 * @updateTime 2021/12/27 20:12
 * @version 1.0
 */
public class Producer_PubSub {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、获取连接
        ConnUtils cs = new ConnUtils();
        Connection connection = cs.getConnection();

        // 2、创建通道
        Channel channel = connection.createChannel();

        // 3、创建交换机
        /**
         * @author Echo
         * exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments)
         * 参数解释:
         *      exchange:交换机的名字
         *      type:交换机的类型
         *           DIRECT("direct"),:定向
         *           FANOUT("fanout"),:扇形(广播),发送消息到每一个与之绑定队列。
         *           TOPIC("topic"),通配符的方式
         *           HEADERS("headers");参数匹配
         *      durable:是否持久化
         *      autoDelete:是否自动删除
         *      internal:一般使用 false,如果设置为true,则表示是内置的交换器,客户端程序无法直接发送消息到这个交换器中,只能通过交换器路由到交换器这种方式
         *      arguments:参数
         *
         */
        String exchangeName = "test_fanout";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT,true,false,false,null);

        // 4、创建队列
        String queue1Name = "test_fanout_queue1";
        String queue2Name = "test_fanout_queue2";
        channel.queueDeclare(queue1Name,true,false,false,null);
        channel.queueDeclare(queue2Name,true,false,false,null);

        // 5、绑定队列和交换机
        /**
         * @author Echo
         * queueBind(String queue, String exchange, String routingKey, Map<String, Object> arguments)
         * 参数解释:
         *      queue:队列名称
         *      exchange:交换机名称
         *      routingKey:路由的规则
         *          如果交换机类型为 fanout,routingKey设置为 ""
         *      arguments:参数
         */
        channel.queueBind(queue1Name,exchangeName,"");
        channel.queueBind(queue2Name,exchangeName,"");

        // 6、发送消息
        String body = "fanout test";
        channel.basicPublish(exchangeName,"",null,body.getBytes());

        // 7、关闭资源
        channel.close();
        connection.close();
    }
}

2、消费者01代码:
/**
 * @className Consumer_PubSub1
 * @author Echo
 * @description 订阅模式-消费者01
 * @updateTime 2021/12/27 20:27
 * @version 1.0
 */
public class Consumer_PubSub1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、获取连接
        ConnUtils cs = new ConnUtils();
        Connection connection = cs.getConnection();

        // 2、创建通道
        Channel channel = connection.createChannel();

        String queue1Name = "test_fanout_queue1";

        // 3、创建队列
        channel.queueDeclare(queue1Name, true, false, false, null);

        // 4、接收消息
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body:"+new String(body));
            }
        };

        // 5、监听消息
        channel.basicConsume(queue1Name,true,consumer);
    }
}

3、消费者02代码:
/**
 * @className Consumer_PubSub2
 * @author Echo
 * @description 订阅模式-消费者02
 * @updateTime 2021/12/27 20:34
 * @version 1.0
 */
public class Consumer_PubSub2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、获取连接
        ConnUtils cs = new ConnUtils();
        Connection connection = cs.getConnection();

        // 2、创建通道
        Channel channel = connection.createChannel();

        String queue2Name = "test_fanout_queue2";

        // 3、创建队列
        channel.queueDeclare(queue2Name, true, false, false, null);

        // 4、接收消息
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body:"+new String(body));
            }
        };
        // 5、监听消息
        channel.basicConsume(queue2Name,true,consumer);
    }
}

2、测试

rm控制页面:

在这里插入图片描述

消费者01控制台:
	body:fanout test
消费者02控制台:
	body:fanout test

项目代码链接:https://github.com/Mbm7280/rabbitmq_demo

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值