zoukankan      html  css  js  c++  java
  • RabbitMQ 几种工作模式---(四)Routing

    生产者类:

    package com..routing;
    
    
    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.hunan.changsha.20201127", "中国湖南长沙20201127天气数据");
            area.put("china.hubei.wuhan.20201127", "中国湖北武汉20201127天气数据");
            area.put("china.hunan.changsha.20201127", "中国湖南长沙20201127天气数据");
            area.put("us.cal.lsj.20201127", "美国加州洛杉矶20201127天气数据");
    
            area.put("china.hebei.shijiazhuang.20201128", "中国河北石家庄20201128天气数据");
            area.put("china.hubei.wuhan.20201128", "中国湖北武汉20201128天气数据");
            area.put("china.henan.zhengzhou.20201128", "中国河南郑州20201128天气数据");
            area.put("us.cal.lsj.20201128", "美国加州洛杉矶20201128天气数据");
    
    
            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_ROUTING,me.getKey() , null , me.getValue().getBytes());
            }
    
            channel.close();
            connection.close();
        }
    }

    消费者1(百度):

    package com..routing;
    
    
    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_ROUTING, "china.hunan.changsha.20201127");
            channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.hebei.shijiazhuang.20201128");
            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..routing;
    
    
    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_ROUTING, "us.cal.lsj.20201127");
            channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.hubei.wuhan.20201127");
            channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "us.cal.lsj.20201128");
            channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.henan.zhengzhou.20991012");
    
            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);
                }
            });
        }
    }

    后台打印信息:

  • 相关阅读:
    electron项目打包成dmg
    到底什么是流?
    cent日常操作实践(二)
    pm2基本使用
    koa + sequelize + mysql 项目实践笔记
    cenos 磁盘操作
    MySQL基础操作汇总二
    Python调用shell
    Python常用模块之七 PIL
    random
  • 原文地址:https://www.cnblogs.com/lifan12589/p/14304118.html
Copyright © 2011-2022 走看看