RabbitMQ 探究(三、fanout 广播)

fanout 广播:
生产者生产消息,并发送到交换机,由交换机进行消息发布,消费者订阅消息并生产临时消息队列来消费
在这里插入图片描述
rabbitMQ 连接工具

package Utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitMqUtil {

    //创建连接MQ的工厂对象
    private static ConnectionFactory connectionFactory;

    //静态代码块
    static{
        connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("192.168.56.10");
        connectionFactory.setPort(5672);
        //设置连接哪个虚拟主机
        connectionFactory.setVirtualHost("/ems");
        //设置访问虚拟主机的用户名和密码
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("123");
    }

    //启动连接
    public static Connection getConnection() throws IOException, TimeoutException {
        return connectionFactory.newConnection();
    }

    //关闭连接
    public static void closeConnectionAndChanel(Channel channel,Connection connection) throws IOException, TimeoutException {
        if(channel==null){
            channel.close();
            connection.close();
        }
    }
}

生产者:

package rabbitMq.fanout;

import Utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

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

//生产者
public class TestRabbitMqP {
    public static void main(String[] args) throws IOException, TimeoutException {

        //传递的消息
        String mes="HELLO WORD !!";

        //通过工具获取连接对象
        Connection connection = RabbitMqUtil.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();
        /**
         * 将通道声明指定交换机
         * 参数:
         * 1、交换机名称
         * 2、交换机类型:fanout 广播类型
         */
        channel.exchangeDeclare("logs","fanout");

        /**
         * 消息发布
         * 参数:
         * 1、交换机名称
         * 2、队列名称
         * 3、传递消息的额外设置
         * 4、消息内容
         */
        channel.basicPublish("logs","",null,"fanout type message".getBytes());

        //关闭连接, 释放资源
        RabbitMqUtil.closeConnectionAndChanel(channel,connection);
}

}

消费者1:

package rabbitMq.fanout;

import Utils.RabbitMqUtil;
import com.rabbitmq.client.*;

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

//消费者
public class TestRabbitMqC_1 {

    public static void main(String[] args) throws IOException, TimeoutException {

        //通过工具获取连接对象
        Connection connection = RabbitMqUtil.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();
        //每次只接受一条未确认的消息
        channel.basicQos(1);
        /**
         * 通道绑定交换机
         * 参数:
         * 1、交换机名称
         * 2、交换机类型
         */
        channel.exchangeDeclare("logs","fanout");

        //临时队列
        String queueName = channel.queueDeclare().getQueue();

        //绑定交换机和队列
        channel.queueBind(queueName,"logs","");

        /**
         * 消息订阅
         * 参数:
         * 1、需要消费的队列名称
         * 2、是否开启消息自动队列
         * 3、消费的回调接口
         */
        channel.basicConsume(queueName,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("==========================="+new String(body));
                //手动确认消息
                channel.basicAck(envelope.getDeliveryTag(),true);
            }
        });

    }

}

消费者2:

package rabbitMq.fanout;

import Utils.RabbitMqUtil;
import com.rabbitmq.client.*;

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

//消费者
public class TestRabbitMqC_2 {

    public static void main(String[] args) throws IOException, TimeoutException {

        //通过工具获取连接对象
        Connection connection = RabbitMqUtil.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();
        //每次只接受一条未确认的消息
        channel.basicQos(1);
        /**
         * 通道绑定交换机
         * 参数:
         * 1、交换机名称
         * 2、交换机类型
         */
        channel.exchangeDeclare("logs","fanout");

        //临时队列
        String queueName = channel.queueDeclare().getQueue();

        //绑定交换机和队列
        channel.queueBind(queueName,"logs","");

        /**
         * 消息订阅
         * 参数:
         * 1、需要消费的队列名称
         * 2、是否开启消息自动队列
         * 3、消费的回调接口
         */
        channel.basicConsume(queueName,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("==========================="+new String(body));
                //手动确认消息
                channel.basicAck(envelope.getDeliveryTag(),true);
            }
        });

    }

}

消费者3:

package rabbitMq.fanout;

import Utils.RabbitMqUtil;
import com.rabbitmq.client.*;

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

//消费者
public class TestRabbitMqC_3 {

    public static void main(String[] args) throws IOException, TimeoutException {

        //通过工具获取连接对象
        Connection connection = RabbitMqUtil.getConnection();
        //获取连接中通道
        Channel channel = connection.createChannel();
        //每次只接受一条未确认的消息
        channel.basicQos(1);
        /**
         * 通道绑定交换机
         * 参数:
         * 1、交换机名称
         * 2、交换机类型
         */
        channel.exchangeDeclare("logs","fanout");

        //临时队列
        String queueName = channel.queueDeclare().getQueue();

        //绑定交换机和队列
        channel.queueBind(queueName,"logs","");

        /**
         * 消息订阅
         * 参数:
         * 1、需要消费的队列名称
         * 2、是否开启消息自动队列
         * 3、消费的回调接口
         */
        channel.basicConsume(queueName,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("==========================="+new String(body));
                //手动确认消息
                channel.basicAck(envelope.getDeliveryTag(),true);
            }
        });

    }

}

当生产者运行之后,三个消费者都能监听到消息发布

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的小蜗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值