RabbitMQ--Fanout(广播模型)

Fanout(广播模型)

生产者发送的消息,只能发送到交换机,交换机决定要发给哪个队列,生产者无法决定。交换机把消息发送给绑定过的所有队列,队列的消费者都能拿到消息,实现一条消息被多个消费者消费
在这里插入图片描述

RabbitMqUtils在RabbitMQ工作原理中已经介绍,这里不在赘述了

Provider

public class Provider {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取通道
        Channel channel = connection.createChannel();

        //将通道声明指定交换机
        //参数1:交换机名称 交换机不存在会去创建
        //参数2:交换机类型 fanout 广播类型
        channel.exchangeDeclare("logs", "fanout");

        //发送消息
        channel.basicPublish("logs", "", null, "fanout type message".getBytes());
        //关闭连接
        RabbitMqUtils.closeConnectionAndChannel(channel, connection);
    }

}

Consumer1

public class Consumer1 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取通道
        Channel channel = connection.createChannel();

        //将通道声明指定交换机
        //参数1:交换机名称 交换机不存在会去创建
        //参数2:交换机类型 fanout 广播类型
        channel.exchangeDeclare("logs", "fanout");
        //临时队列
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        channel.queueBind(queueName,"logs","");

        //消费消息
        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
    
    
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) {
    
    
                System.out.println("消费者1 " + new String(body));
            }
        });
    }
}

Consumer2

public class Consumer2 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取通道
        Channel channel = connection.createChannel();

        //将通道声明指定交换机
        //参数1:交换机名称 交换机不存在会去创建
        //参数2:交换机类型 fanout 广播类型
        channel.exchangeDeclare("logs", "fanout");
        //临时队列
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        channel.queueBind(queueName,"logs","");

        //消费消息
        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
    
    
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) {
    
    
                System.out.println("消费者2 " + new String(body));
            }
        });
    }
}

Consumer3

public class Consumer3 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //获取连接对象
        Connection connection = RabbitMqUtils.getConnection();
        //获取通道
        Channel channel = connection.createChannel();

        //将通道声明指定交换机
        //参数1:交换机名称 交换机不存在会去创建
        //参数2:交换机类型 fanout 广播类型
        channel.exchangeDeclare("logs", "fanout");
        //临时队列
        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列
        channel.queueBind(queueName,"logs","");

        //消费消息
        channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
    
    
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) {
    
    
                System.out.println("消费者3 " + new String(body));
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38530648/article/details/114036993