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