RABBITMQ Five Message Queuing Learning (vi)-wildcard mode (route type: Topic)

Source: Internet
Author: User
Tags bind sleep rabbitmq
RABBITMQ Five Message Queuing Learning (vi) – Wildcard mode (route type: Topic)

tags (space delimited): RabbitMQ

As the upgrade of a schema described in the previous article, if you need to listen to all the messages for a switch, you can bind them in the form of Message Queuing. Queue structure Diagram


Pattern matching through string wildcard for Message Queuing that needs to be distributed
Message path:

Realize:
1. Producers

Private final static String Exchange_name = "Test_exchange_topic";

    public static void Main (string[] argv) throws Exception {
        //Get to Connection and MQ channel
        Connection Connection = connectionutil.g Etconnection ();
        Channel channel = Connection.createchannel ();

        DECLARE EXCHANGE
        channel.exchangedeclare (exchange_name, "topic");

        Message content,
        String message = "Delete Product ID = +";
        Channel.basicpublish (Exchange_name, "Item.delete", NULL, Message.getbytes ());
        System.out.println ("[X] Sent '" + Message + "'");

        Channel.close ();
        Connection.close ();
    }

2. Consumer 1

 Private final static String queue_name = "Test_queue_topic_1";

    Private final static String Exchange_name = "Test_exchange_topic"; public static void Main (string[] argv) throws Exception {//Get to Connection and MQ channel Connection Connection = Connecti
        Onutil.getconnection ();

        Channel channel = Connection.createchannel ();

        Declaration Queue Channel.queuedeclare (Queue_name, False, False, false, NULL);
        Bind queue to switch Channel.queuebind (queue_name, Exchange_name, "item.update");

        Channel.queuebind (queue_name, Exchange_name, "Item.delete");

        At the same time the server will only send a message to the consumer Channel.basicqos (1);
        Define the queue's consumer queueingconsumer consumer = new Queueingconsumer (channel);

        Listen queue, manually return complete Channel.basicconsume (Queue_name, false, consumer);
            Gets the message while (true) {Queueingconsumer.delivery Delivery = Consumer.nextdelivery ();
         String message = new String (Delivery.getbody ());   System.out.println ("[X] Received '" + Message + "'");

            Thread.Sleep (10);
        Channel.basicack (Delivery.getenvelope (). Getdeliverytag (), false); }
    }

3, Consumer 2

 Private final static String queue_name = "test_queue_topic_2";

    Private final static String Exchange_name = "Test_exchange_topic"; public static void Main (string[] argv) throws Exception {//Get to Connection and MQ channel Connection Connection = Connecti
        Onutil.getconnection ();

        Channel channel = Connection.createchannel ();

        Declaration Queue Channel.queuedeclare (Queue_name, False, False, false, NULL);

        Bind queue to switch Channel.queuebind (queue_name, Exchange_name, "item.#");

        At the same time the server will only send a message to the consumer Channel.basicqos (1);
        Define the queue's consumer queueingconsumer consumer = new Queueingconsumer (channel);

        Listen queue, manually return complete Channel.basicconsume (Queue_name, false, consumer);
            Gets the message while (true) {Queueingconsumer.delivery Delivery = Consumer.nextdelivery ();
            String message = new String (Delivery.getbody ());
            System.out.println ("[X] Received '" + Message + "'"); ThreAd.sleep (10);
        Channel.basicack (Delivery.getenvelope (). Getdeliverytag (), false); }
    }

4. Test results
In consumer 1, it is clear what routing Key the queue needs to bind to the router

Bind queue to switch
        channel.queuebind (queue_name, Exchange_name, "item.update");
        Channel.queuebind (queue_name, Exchange_name, "Item.delete");

Consumer 2, because to receive all messages under item, the binding is:

Bind queue to switch
        channel.queuebind (queue_name, Exchange_name, "item.#");

All Consumers 2 can receive all messages, consumers 1 can only accept update\delete two kinds of messages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.