image.png
    image.png
    1.还是以通配符模式的配置继续来进行代码演示
    2.发布者

    1. import com.baiqi.rabbitmq.utils.RabbitConstant;
    2. import com.baiqi.rabbitmq.utils.RabbitUtils;
    3. import com.rabbitmq.client.*;
    4. import java.io.IOException;
    5. import java.util.Iterator;
    6. import java.util.LinkedHashMap;
    7. import java.util.Map;
    8. import java.util.concurrent.TimeoutException;
    9. public class WeatherBureau {
    10. public static void main(String[] args) throws IOException, TimeoutException {
    11. Map area = new LinkedHashMap<String, String>();
    12. area.put("china.hunan.changsha.20201127", "中国湖南长沙20201127天气数据");
    13. area.put("china.hubei.wuhan.20201127", "中国湖北武汉20201127天气数据");
    14. area.put("china.hunan.zhuzhou.20201127", "中国湖南株洲20201127天气数据");
    15. area.put("us.cal.lsj.20201127", "美国加州洛杉矶20201127天气数据");
    16. area.put("china.hebei.shijiazhuang.20201128", "中国河北石家庄20201128天气数据");
    17. area.put("china.hubei.wuhan.20201128", "中国湖北武汉20201128天气数据");
    18. area.put("china.henan.zhengzhou.20201128", "中国河南郑州20201128天气数据");
    19. area.put("us.cal.lsj.20201128", "美国加州洛杉矶20201128天气数据");
    20. Connection connection = RabbitUtils.getConnection();
    21. Channel channel = connection.createChannel();
    22. //开启confirm监听模式
    23. channel.confirmSelect();
    24. channel.addConfirmListener(new ConfirmListener() {
    25. public void handleAck(long l, boolean b) throws IOException {
    26. //第二个参数代表接收的数据是否为批量接收,一般我们用不到。
    27. System.out.println("消息已被Broker接收,Tag:" + l);
    28. }
    29. public void handleNack(long l, boolean b) throws IOException {
    30. System.out.println("消息已被Broker拒收,Tag:" + l);
    31. }
    32. });
    33. channel.addReturnListener(new ReturnCallback() {
    34. public void handle(Return r) {
    35. System.err.println("===========================");
    36. System.err.println("Return编码:" + r.getReplyCode() + "-Return描述:" + r.getReplyText());
    37. System.err.println("交换机:" + r.getExchange() + "-路由key:" + r.getRoutingKey());
    38. System.err.println("Return主题:" + new String(r.getBody()));
    39. System.err.println("===========================");
    40. }
    41. });
    42. Iterator<Map.Entry<String, String>> itr = area.entrySet().iterator();
    43. while (itr.hasNext()) {
    44. Map.Entry<String, String> me = itr.next();
    45. //Routing key 第二个参数相当于数据筛选的条件
    46. //第三个参数为:mandatory true代表如果消息无法正常投递则return回生产者,如果false,则直接将消息放弃。
    47. channel.basicPublish(RabbitConstant.EXCHANGE_WEATHER_TOPIC, me.getKey(), true, null, me.getValue().getBytes());
    48. }
    49. //如果关闭则无法进行监听,因此此处不需要关闭
    50. /*channel.close();
    51. connection.close();*/
    52. }
    53. }

    3.消费者Baidu

    1. import com.baiqi.rabbitmq.utils.RabbitConstant;
    2. import com.baiqi.rabbitmq.utils.RabbitUtils;
    3. import com.rabbitmq.client.*;
    4. import java.io.IOException;
    5. public class Baidu {
    6. public static void main(String[] args) throws IOException {
    7. Connection connection = RabbitUtils.getConnection();
    8. final Channel channel = connection.createChannel();
    9. channel.queueDeclare(RabbitConstant.QUEUE_BAIDU, false, false, false, null);
    10. //queueBind用于将队列与交换机绑定
    11. //参数1:队列名 参数2:交互机名 参数三:路由key
    12. channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "*.*.*.20201127");
    13. //channel.queueUnbind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "*.*.*.20201127");
    14. //*.hebei.*.*
    15. channel.basicQos(1);
    16. channel.basicConsume(RabbitConstant.QUEUE_BAIDU, false, new DefaultConsumer(channel) {
    17. @Override
    18. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    19. System.out.println("百度天气收到气象信息:" + new String(body));
    20. channel.basicAck(envelope.getDeliveryTag(), false);
    21. }
    22. });
    23. }
    24. }

    4.消费者Sina

    1. import com.baiqi.rabbitmq.utils.RabbitConstant;
    2. import com.baiqi.rabbitmq.utils.RabbitUtils;
    3. import com.rabbitmq.client.*;
    4. import java.io.IOException;
    5. public class Sina {
    6. public static void main(String[] args) throws IOException {
    7. Connection connection = RabbitUtils.getConnection();
    8. final Channel channel = connection.createChannel();
    9. channel.queueDeclare(RabbitConstant.QUEUE_SINA, false, false, false, null);
    10. channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "us.#");
    11. channel.basicQos(1);
    12. channel.basicConsume(RabbitConstant.QUEUE_SINA , false , new DefaultConsumer(channel){
    13. @Override
    14. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    15. System.out.println("腾讯天气收到气象信息:" + new String(body));
    16. channel.basicAck(envelope.getDeliveryTag() , false);
    17. }
    18. });
    19. }
    20. }

    5.运行结果
    image.png