First, review
Let's review what we've said in the last few chapters. Summarized as follows:
- "Janet: Looking at the IT architecture in the Big Data era (1) Industry Message Queuing comparison"
- Janet: Looking at the IT architecture in the Big Data Era (2) Message Queuing rabbitmq-Basic concept detailed Introduction
- Janet: Looking at the IT architecture in the Big Data Era (3) rabbitmq-installation, configuration and monitoring of Message Queuing
- "Janet: Looking at the IT architecture in the Big Data era (4) rabbitmq--case of Message Queuing (Helloword set sail)"
- "Janet: Looking at the IT architecture in the Big Data era (5) rabbitmq--case of Message Queuing (work Queues set sail)"
- "Janet: Looking at the IT architecture in the Big Data era (6) rabbitmq--case of Message Queuing (Publish/subscribe set sail)"
Second, Routing (using the Java client) in the previous study, built a simple logging system, able to broadcast all the logs to multiple receivers, in this part of the learning, will add a new feature is that you can only subscribe to a specific message source, This means that the key error log messages can be sent directly to the log file to save, the unimportant log information file is not saved in the disk, but still able to output in the console, then this is our part of the message to learn the route distribution mechanism. third, Bindings (binding)The binding (bindings) has been created in the previous study and the code is as follows:
< span class= "n" style= "Color:rgb (0,128,128)" > channel queuebind ( queuename " Span class= "o" style= "Font-weight:bold", exchange_name Span class= "o" style= "Font-weight:bold");
A binding is a relationship between Exchange and queue, which can simply be understood as: The queue is getting messages from this exchange.
Bindings can take an additional routingkey parameter, in order to avoid collisions with the Basicpublish parameter, called a binding key, which is how to create a binding with Routingkey.
Channel . Queuebind (queuename,exchange_name,"Black");
a binding key relies on the type of exchange, which, like previous exchange with the fanout type, completely ignores the value of the binding key. Iv. Direct Exchange (directly switch)
The previously implemented logging system broadcasts all the messages to all consumers and now expands them to allow filtering of messages based on the severity of the information, such as a log message that expects a program to write to disk to receive only the wrong message, rather than wasting the disk saving all the log messages.
In order to achieve this goal, using an fanout type of exchange is obviously not able to meet this requirement because it can only broadcast all messages.
For this purpose, you will use a direct exchange instead of Fanout Exchange,direct Exchange to use a simple routing algorithm that matches the message to the queue that will be reached through the bound key.
As you can see from the structure diagram above, direct Exchange X is bound to two queue (Q1,Q2), the first queue bound Routingkey is orange, the second has two routingkey bound, One routingkey is black and the other Routingkey green.
Note: Sending messages with Routingkey Orange to X (Exchange), x routing the message to Q1, sending messages with Routingkey black and green will be routed to Q2, all other messages will be discarded.
Five,multiple bindings(multi-binding) Multiple queue bindings with the same routingkey are allowed, in the above example, X and Q1 can be bound together with Routingkey:black, in which case the direct Exchange will broadcast the message to all matching queues, like fanout type Exchange, where a message Routingkey black will be sent to Q1 and Q2.Vi. emitting logs (sent logs)Using direct instead of the fanout type of exchange, sending a message to a direct exchange will be based on the importance of the message as Routingkey, so that the receiver can select the log information it wants to receive. You must first create an exchange.
Channel. Exchangedeclare (exchange_name,"direct");
Next, send a message:
< span class= "n" style= "Color:rgb (0,128,128)" > channel basicpublish ( exchange_name severity , null , message getbytes ());
to simplify the program, severity is set to one of the three types of info, warning, and error. Vi. Subscribing (Subscribe to messages) The recipient creates a new binding based on the severity of interest.
String QueueName = Channel.Queuedeclare().Getqueue(); for(String severity : argv){ Channel.Queuebind(QueueName, Exchange_name, severity);}
Seven,putting it all together(Code implementation)The Emitlogdirect.java code listing is as follows:
public class Emitlogdirect { private static final String Exchange_name = "Direct_logs"; public static void Main (string[] argv) throws java.io.IOException { ConnectionFactory factory = new ConnectionFactory (); Factory.sethost ("localhost"); Connection Connection = Factory.newconnection (); Channel channel = Connection.createchannel (); Channel.exchangedeclare (Exchange_name, "direct"); String severity = getseverity (argv); String message = GetMessage (argv); Channel.basicpublish (exchange_name, severity, NULL, message.getbytes ()); System.out.println ("[X] Sent '" + Severity + "': '" + Message + "'"); Channel.close (); Connection.close (); } //..}
Receivelogsdirect CodeThe list is as follows:
public class Receivelogsdirect {private static final String Exchange_name = "Direct_logs"; public static void Main (string[] argv) throws Java.io.IOException, java.lang.Interrupted Exception {ConnectionFactory factory = new ConnectionFactory (); Factory.sethost ("localhost"); Connection Connection = Factory.newconnection (); Channel channel = Connection.createchannel (); Channel.exchangedeclare (Exchange_name, "direct"); String queuename = Channel.queuedeclare (). Getqueue (); if (Argv.length < 1) {System.err.println ("Usage:receivelogsdirect [info] [WARNING] [ERROR]"); System.exit (1); } for (String severity:argv) {channel.queuebind (queuename, exchange_name, severity); } System.out.println ("[*] waiting for messages. To exit Press CTRL + C "); Queueingconsumer consumer = new Queueingconsumer (channel); Channel.basicconsume (queuename, true, consumer); while (true) {Queueingconsumer.delivery Delivery = Consumer.nextdelivery (); String message = new String (Delivery.getbody ()); String Routingkey = Delivery.getenvelope (). Getroutingkey (); System.out.println ("[X] Received '" + Routingkey + "': '" + Message + "'"); } }}
Compile as usual (see previous tutorials for compiling and classpath recommendations). Now, for the sake of convenience, we will use an example of the run-time classpath of an environment variable $CP (%cp% on Windows).
If you only want to save "warning" and "error" (instead of "message") log messages to a file, open a console and type:
$CP Receivelogsdirect Warning Error > Logs_from_rabbit.log
If you want to see all the log messages on your screen, open a new terminal and do:
$CP [*] Press CTRL + C
For example, publish an error log message
$CP "Run. Run. Or it'll explode. " [x] ' ERROR ':' Run. Run. Or it'll explode. '
Janet: Looking at the IT architecture in the Big Data era (7) rabbitmq--case of Message Queuing (routing set sail)