Pub/Sub 订阅模式
    1. 模式说明
    image.png
    image.png
    image.png
    image.png
    1.添加好交换机weather
    image.png
    2.发布者

    1. import com.baiqi.rabbitmq.utils.RabbitConstant;
    2. import com.baiqi.rabbitmq.utils.RabbitUtils;
    3. import com.rabbitmq.client.Channel;
    4. import com.rabbitmq.client.Connection;
    5. import java.util.Scanner;
    6. /**
    7. * 发布者
    8. */
    9. public class WeatherBureau {
    10. public static void main(String[] args) throws Exception {
    11. Connection connection = RabbitUtils.getConnection();
    12. String input = new Scanner(System.in).next();
    13. Channel channel = connection.createChannel();
    14. //第一个参数交换机名字 其他参数和之前的一样
    15. channel.basicPublish(RabbitConstant.EXCHANGE_WEATHER, "", null, input.getBytes());
    16. channel.close();
    17. connection.close();
    18. }
    19. }

    3.消费者BiaDu

    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. /**
    6. * 消费者
    7. */
    8. public class BiaDu {
    9. public static void main(String[] args) throws IOException {
    10. //获取TCP长连接
    11. Connection connection = RabbitUtils.getConnection();
    12. //获取虚拟连接
    13. final Channel channel = connection.createChannel();
    14. //声明队列信息
    15. channel.queueDeclare(RabbitConstant.QUEUE_BAIDU, false, false, false, null);
    16. //queueBind用于将队列与交换机绑定
    17. //参数1:队列名 参数2:交互机名 参数三:路由key(暂时用不到)
    18. channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER, "");
    19. channel.basicQos(1);
    20. channel.basicConsume(RabbitConstant.QUEUE_BAIDU, false, new DefaultConsumer(channel) {
    21. @Override
    22. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    23. System.out.println("新浪天气收到气象信息:" + new String(body));
    24. channel.basicAck(envelope.getDeliveryTag(), false);
    25. }
    26. });
    27. }
    28. }

    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. /**
    6. * 消费者
    7. */
    8. public class Sina {
    9. public static void main(String[] args) throws IOException {
    10. //获取TCP长连接
    11. Connection connection = RabbitUtils.getConnection();
    12. //获取虚拟连接
    13. final Channel channel = connection.createChannel();
    14. //声明队列信息
    15. channel.queueDeclare(RabbitConstant.QUEUE_SINA, false, false, false, null);
    16. //queueBind用于将队列与交换机绑定
    17. //参数1:队列名 参数2:交互机名 参数三:路由key(暂时用不到)
    18. channel.queueBind(RabbitConstant.QUEUE_SINA, RabbitConstant.EXCHANGE_WEATHER, "");
    19. channel.basicQos(1);
    20. channel.basicConsume(RabbitConstant.QUEUE_SINA, false, new DefaultConsumer(channel) {
    21. @Override
    22. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    23. System.out.println("新浪天气收到气象信息:" + new String(body));
    24. channel.basicAck(envelope.getDeliveryTag(), false);
    25. }
    26. });
    27. }
    28. }

    5.运行结果:
    image.png
    image.png
    image.png
    image.png
    image.png