• RabbitMQ 几种工作模式---(五)Topics


    生产者类:

    package com..topic;
    
    
    import com..utils.RabbitConstant;
    import com..utils.RabbitUtils;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.concurrent.TimeoutException;
    
    public class WeatherBureau {
        public static void main(String[] args) throws IOException, TimeoutException {
            Map area = new LinkedHashMap<String, String>();
            area.put("china.hebei.shijiazhuang.20991011", "中国河北石家庄20991011天气数据");
            area.put("china.shandong.qingdao.20991011", "中国山东青岛20991011天气数据");
            area.put("china.henan.zhengzhou.20991011", "中国河南郑州20991011天气数据");
            area.put("us.cal.la.20991011", "美国加州洛杉矶20991011天气数据");
    
            area.put("china.hebei.shijiazhuang.20991012", "中国河北石家庄20991012天气数据");
            area.put("china.shandong.qingdao.20991012", "中国山东青岛20991012天气数据");
            area.put("china.henan.zhengzhou.20991012", "中国河南郑州20991012天气数据");
            area.put("us.cal.la.20991012", "美国加州洛杉矶20991012天气数据");
    
            Connection connection = RabbitUtils.getConnection();
            Channel channel = connection.createChannel();
            Iterator<Map.Entry<String, String>> itr = area.entrySet().iterator();
            while (itr.hasNext()) {
                Map.Entry<String, String> me = itr.next();
                //Routing key 第二个参数相当于数据筛选的条件
                channel.basicPublish(RabbitConstant.EXCHANGE_WEATHER_TOPIC,me.getKey() , null , me.getValue().getBytes());
            }
    
            channel.close();
            connection.close();
        }
    }

    消费者1(百度):

    package com..topic;
    
    
    import com..utils.RabbitConstant;
    import com..utils.RabbitUtils;
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    
    public class Baidu {
        public static void main(String[] args) throws IOException {
            Connection connection = RabbitUtils.getConnection();
            final Channel channel = connection.createChannel();
            channel.queueDeclare(RabbitConstant.QUEUE_BAIDU, false, false, false, null);
            //queueBind用于将队列与交换机绑定
            //参数1:队列名 参数2:交互机名  参数三:路由key
            channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "*.*.*.20991011");
            //channel.queueUnbind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "*.*.*.20991011");
            //*.hebei.*.*
            channel.basicQos(1);
            channel.basicConsume(RabbitConstant.QUEUE_BAIDU , 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() , false);
                }
            });
        }
    }

    后台打印信息:

    消费者2(新浪):

    package com..topic;
    
    
    import com..utils.RabbitConstant;
    import com..utils.RabbitUtils;
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    
    public class Sina {
        public static void main(String[] args) throws IOException {
            Connection connection = RabbitUtils.getConnection();
            final Channel channel = connection.createChannel();
            channel.queueDeclare(RabbitConstant.QUEUE_SINA, false, false, false, null);
    
            channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "us.#");
    
            channel.basicQos(1);
            channel.basicConsume(RabbitConstant.QUEUE_SINA , 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() , false);
                }
            });
        }
    }

    后台打印信息:

     

  • 相关阅读:
    Windows Phone 8 开发环境搭建
    常用正则表达式大全分享
    ios 使用NSRegularExpression解析正则表达式
    大整数类BIGN的设计与实现 C++高精度模板
    CODEVS_1227 方格取数2 网络流 最小费用流 拆点
    CODEVS_1034 家园 网络流 最大流
    CODEVS_1033 蚯蚓的游戏问题 网络流 最小费用流 拆点
    HDU_4770 Lights Against Dudely 状压+剪枝
    CODEVS_2144 砝码称重 2 折半搜索+二分查找+哈希
    CODEVS_1074 食物链
  • 原文地址:https://www.cnblogs.com/lifan12589/p/14304151.html
Copyright © 2020-2023  润新知