RabbitMQ订阅模式Pub/Sub订阅模式

 1.生产者代码编写

/**
 * 发送消息
 */
public class Producer_PubSub {
    public static void main(String[] args) throws IOException, TimeoutException {

        // 1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2.设置参数
        factory.setHost("192.168.10.11");// ip 默认值 localhost
        factory.setPort(5672); // 端口 默认值 5672
        factory.setVirtualHost("/lei404"); // 虚拟机 默认值/
        factory.setUsername("lei"); // 用户名 默认值 guest
        factory.setPassword("lei404"); // 密码 默认值 guest
        // 3.创建连接connection
        Connection connection = factory.newConnection();
        // 4.创建channel
        Channel channel = connection.createChannel();
        // 5.创建交换机
//        exchangeDeclare(String exchange,交换机名称
//                BuiltinExchangeType type,交换机类型 DIRECT("direct")定向, FANOUT("fanout")广播, TOPIC("topic") 通配符方式, HEADERS("headers") 参数匹配;
//        boolean durable,是否持久化
//        boolean autoDelete, 自动删除
//        boolean internal,内部使用,一般false
//        Map<String, Object> arguments) throws IOException;参数列表
        String exchangeName= "test_fanout";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT, true, false, false, null);
        // 6.创建队列
        String queue1Name = "test_fanout_queue1";
        String queue2Name = "test_fanout_queue2";
        channel.queueDeclare(queue1Name, true, false, false, null);
        channel.queueDeclare(queue2Name, true, false, false, null);
        // 7.绑定队列和交换机
//        queueBind(String queue,队列名称
//        String exchange, 交换机名称
//        String routingKey 路由键 如果交换机的类型为fanout,routingKey设置为""
        channel.queueBind(queue1Name, exchangeName, "");
        channel.queueBind(queue2Name, exchangeName, "");
        // 8.发送消息
        String body = "日志信息";
        channel.basicPublish(exchangeName, "", null, body.getBytes());
        // 9.释放资源
        channel.close();
        connection.close();
    }
}

2.消费者代码编写

public class Consumer_PubSub1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 2.设置参数
        factory.setHost("192.168.10.11");// ip 默认值 localhost
        factory.setPort(5672); // 端口 默认值 5672
        factory.setVirtualHost("/lei404"); // 虚拟机 默认值/
        factory.setUsername("lei"); // 用户名 默认值 guest
        factory.setPassword("lei404"); // 密码 默认值 guest
        // 3.创建连接connection
        Connection connection = factory.newConnection();
        // 4.创建channel
        Channel channel = connection.createChannel();

        // 6.创建队列
        String queue1Name = "test_fanout_queue1";
        String queue2Name = "test_fanout_queue2";

        // 6.接收消息
//        basicConsume(
//        String queue, 队列名称
//        boolean autoAck, 是否自动确认
//        Consumer callback 回调函数)
        Consumer consumer = new DefaultConsumer(channel){
            /**
             * 回调方法,当收到消息后会自动执行该方法
             * @param consumerTag 消息的标识
             * @param envelope 获取一些信息,交换机的信息,路由key的信息
             * @param properties 配置信息
             * @param body 真实的数据
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                System.out.println("consumerTag:" + consumerTag);
//                System.out.println("Exchange:" + envelope.getExchange());
//                System.out.println("RoutingKey:" + envelope.getRoutingKey());
//                System.out.println("properties:" + properties);
                System.out.println("body:" + new String(body));
                System.out.println("123");
            }
        };
        channel.basicConsume("queue1Name", true, consumer);
        // 释放资源?不要关闭资源,实时监听,不能关闭资源
//        channel.close();
//        connection.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值