美文网首页
RabbitMQ-主题模式

RabbitMQ-主题模式

作者: jiahzhon | 来源:发表于2020-07-20 18:13 被阅读0次
  • 将路由与某模式进行匹配
    • ·# 匹配一个或者多个
    • * 匹配一个
image.png image.png
  • 生产者:
public class Send {
    private static final String EXCHANGE_NAME = "test_exchange_topic";

    public static void main(String[] args) throws IOException {

        Connection connections = ConnectionUtils.getConnections();
        Channel channel = connections.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        String msg = new String("商品-----");
        String routingKey="goods.del";
        System.out.println("send ---  " + msg);
        channel.basicPublish(EXCHANGE_NAME,routingKey, null, msg.getBytes());
        channel.close();      
        connections.close();

    }
}
  • 消费者1:
public class Recv1 {
    private static final String QUEUE_NAME = "queue_topic_1";
    private static final String EXCHANGE_NAME = "test_exchange_topic";

    public static void main(String[] args) throws IOException {
        Connection connections = ConnectionUtils.getConnections();
        final Channel channel = connections.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "goods.add");
        channel.basicQos(1);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                    throws IOException {
                String msg = new String(body, "utf-8");
                System.out.println("recv1   " + msg);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            }
        };
        channel.basicConsume(QUEUE_NAME, false, defaultConsumer);

    }
}
  • 消费者2:
public class Recv2 {
    private static final String QUEUE_NAME = "queue_topic_2";
    private static final String EXCHANGE_NAME = "test_exchange_topic";

    public static void main(String[] args) throws IOException {
        Connection connections = ConnectionUtils.getConnections();
        final Channel channel = connections.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "goods.*");
        channel.basicQos(1);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                    throws IOException {
                String msg = new String(body, "utf-8");
                System.out.println("recv2   " + msg);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            }
        };
        channel.basicConsume(QUEUE_NAME, false, defaultConsumer);

    }
}

相关文章

  • RabbitMQ-主题模式

    将路由与某模式进行匹配·# 匹配一个或者多个* 匹配一个 生产者: 消费者1: 消费者2:

  • rabbitMq-主题模式topic-java

    工作原理:主题模式在路由模式的基础上再次升级,使用通配符的方式,匹配路由key(用来描述一部分类似的路由key进行...

  • RabbitMQ-路由模式

    生产者: 消费者1: 消费者2: 此时只有消费者2能收到

  • RabbitMQ—Android客户端

    工作原理 RabbitMQ-中文文档RabbitMQ-官网上面的网站详细的描述了整个框架。建议没接触过Rabbit...

  • RabbitMQ-发布订阅模式

    X:交换机、转发器 一个生产者,多个消费者 每一个消费者都有自己的队列 生产者没有直接将消息发送到队列。而是发送到...

  • RabbitMQ入门-消息派发那些事儿

    在上篇《RabbitMQ-高效的Work模式》中,我们了解了Work模型,该模型包括一个生产者,一个消息队列和多个...

  • rabbitMq-工作模式(双工模式)-java

    P:代表生产者,向队列中发送消息。队列:保存生产者提供的数据。(队列的特点,先进先出)C1,C2:代表消费者,负责...

  • 消息队列-RabbitMQ-集群模式

    一、概述 RabbitMQ本身是基于Erlang编写,Erlang语言天生具备分布式特性(通过同步Erlang集群...

  • rabbitMq-简单模式-java

    简单模式 P:代表生产者,向队列中发送消息。队列:保存生产者提供的数据。(队列的特点,先进先出)C:代表消费者,负...

  • RabbitMQ-消息可靠性&延迟消息

    前置文章:RabbitMQ-基础使用(Spring AMQP) [https://www.jianshu.com/...

网友评论

      本文标题:RabbitMQ-主题模式

      本文链接:https://www.haomeiwen.com/subject/hbbykktx.html