zoukankan      html  css  js  c++  java
  • 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);
                }
            });
        }
    }

    后台打印信息:

     

  • 相关阅读:
    codeforces 650B
    2013 ACM区域赛长沙 H zoj 3733 (hdu 4798) Skycity
    2013 ACM区域赛长沙 I LIKE vs CANDLE(ZOJ3734) 很好的一道树形DP
    Codeforces Round #306 (Div. 2)——A——Two Substrings
    Codeforces Round #306 (Div. 2)——B暴力——Preparing Olympiad
    HDU5248——二分查找——序列变换
    HDU2255——KM算法——奔小康赚大钱
    匈牙利算法&KM算法
    HDU1059——多重部分和问题——Dividing
    HDU1058——Humble Numbers
  • 原文地址:https://www.cnblogs.com/lifan12589/p/14304151.html
Copyright © 2011-2022 走看看