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);
                }
            });
        }
    }

    后台打印信息:

  • 相关阅读:
    Vagrant box ubuntu/xenial64 添加vagrant用户解决没有登录密码的问题
    jQuery获取浏览器URL链接的值
    js防止客户端多触发
    Jquery实现一组复选框单选
    jQuery监听文本框值改变触发事件(propertychange)
    将http调用返回json中的有关中文的unicode转换为中文
    Visual Studio 2015 Bowser Link的功能不停的向服务端发送请求
    客户端向服务端传送特殊字符解决方法(检测到有潜在危险的 Request.Form 值)
    java集群之session共享解决方案
    阻止保存要求重新创建表的更改选项
  • 原文地址:https://www.cnblogs.com/lifan12589/p/14304118.html
Copyright © 2011-2022 走看看