RabbitMQ中的Fanout模型

fanout 模型(发布/订阅模型)

img

生产者只能向交换机发送消息,关于交换机,一方面,它接收来自生产者的消息,另一边是将它们推送到队列中。交换机必须确切地知道如何处理它收到的消息。应该把它追加到特定的队列中吗?应该追加到许多队列中吗?或者它应该被丢弃。由交换类型定义的‎‎规则‎‎。‎Fanout模型非常的简单,就是交换机把消息广播给所有已知的队列。

临时队列

为什么需要创建临时队列呢?因为我们希望听到所有日志消息,而不仅仅是其中一个子集。也只对当前流动的消息感兴趣, 而不是旧消息。在 Java 客户端中,当我们不向queueDeclare() 提供参数时,我们将会创建一个非持久、独占、自动删除队列,并生成名称。

队列和交换机绑定

channel.queueBind(queueName, "logs", "");

1.开发生产者

最重要的就是声明交换机的类型:channel.exchangeDeclare(“logs”, “fanout”);

package fanout;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import util.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * Author: sudi
 * Date: 2020/12/29
 * TIME: 10:58
 */
public class Send {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
        Channel channel = connection.createChannel();
        //将通道声明指定交换机
        channel.exchangeDeclare("logs", "fanout");
        //发送消息
        channel.basicPublish("logs", "", null, "hello fanout".getBytes());
        RabbitMQUtils.closeConnectionAndChannel(channel,connection);
    }
}

2.开发消费者1

package fanout;

import com.rabbitmq.client.*;
import util.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * Author: sudi
 * Date: 2020/12/29
 * TIME: 11:02
 */
public class Accept1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
        Channel channel = connection.createChannel();
        //通道绑定交换机
        channel.exchangeDeclare("logs", "fanout");
        //临时队列
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        channel.queueBind(queueName, "logs", "");
        //消费消息
        channel.basicConsume("", true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("Accept1" + new String(body));
            }
        });
    }
}

3.开发消费者2

package fanout;

import com.rabbitmq.client.*;
import util.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * Author: sudi
 * Date: 2020/12/29
 * TIME: 11:02
 */
public class Accept2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.getConnection("192.168.1.18", "/ems", 5672, "ems", "123");
        Channel channel = connection.createChannel();
        //通道绑定交换机
        channel.exchangeDeclare("logs", "fanout");
        //临时队列
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        channel.queueBind(queueName, "logs", "");
        //消费消息
        channel.basicConsume("", true, new DefaultConsumer(channel) {
            @Override
            public voi![在这里插入图片描述](https://img-blog.csdnimg.cn/20210102131243782.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxMTM1MDk3NTY5NA==,size_16,color_FFFFFF,t_70#pic_center)
d handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("Accept2" + new String(body));
            }
        });
    }
}

4.执行效果

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值