This is the 13th day of my participation in the August More text Challenge. For details, see: August More Text Challenge


Related articles

MyBatis series summary: MyBatis series


preface

  • Simulate a scenario

    • There are now three levels of logs: info, Wearing, error
    • Now I want to save the error log locally, info and wearing will just discard it.
    • With that in mind, could we define one consumer binding specifically for storing error logs and another consumer binding info and wearing direct consumption without any processing

I. Producers

  • public static void publishMessageIndividually() throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(ChangeNameConstant.DIRECT_MODEL, BuiltinExchangeType.DIRECT); BindingKeyMap = new HashMap<>(); // Create multiple bindingKey Maps <String, String> bindingKeyMap = new HashMap<>(); Bindingkeymap. put("info"," info"); Bindingkeymap.put ("warning"," Warning warning information "); Bindingkeymap. put("error"," error message "); Bindingkeymap. put("debug"," debug debug message "); for (Map.Entry<String, String> bindingKeyEntry: bindingKeyMap.entrySet()){ String bindingKey = bindingKeyEntry.getKey(); String message = bindingKeyEntry.getValue(); channel.basicPublish(ChangeNameConstant.DIRECT_MODEL,bindingKey, null, message.getBytes("UTF-8")); System.out.println(" producer sends message :" + message); }}Copy the code
  • As you can see, three routes are set on the Direct_PATTERN switch

Ii. Consumers

  • Consumers A
  • /** * This is a test consumer *@author DingYongJun *@date 2021/8/1 */ public class DyConsumerTest_direct01 {public static void Main (String[] args) throws Exception{// Use the tool class to create a Channel. Channel = Rabbitmqutils.getChannel (); String queueName = "disk"; channel.queueDeclare(queueName, false, false, false, null); / / the designed to handle the error log, save it to local channel. QueueBind (queueName, ChangeNameConstant DIRECT_MODEL, "error"); System.out.println("A waits for the message to be received and prints the received message on the screen.....") ); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(message+" saved locally "); }; Channel.basicconsume (queueName, true, deliverCallback, consumerTag -> {system.out.println (" message break ~"); }); }}Copy the code
  • Consumers B
  • /** * This is a test consumer *@author DingYongJun *@date 2021/8/1 */ public class DyConsumerTest_direct02 {public static void Main (String[] args) throws Exception{// Use the tool class to create a Channel. Channel = Rabbitmqutils.getChannel (); String queueName = "console"; channel.queueDeclare(queueName, false, false, false, null); / / the designed to handle the error log, save it to local channel. QueueBind (queueName, ChangeNameConstant DIRECT_MODEL, "warning"); channel.queueBind(queueName, ChangeNameConstant.DIRECT_MODEL, "info"); System.out.println("B waits for the message to be received and prints the received message on the screen.....") ); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println(message+" consumed and discarded "); }; Channel.basicconsume (queueName, true, deliverCallback, consumerTag -> {system.out.println (" message break ~"); }); }}Copy the code
  • Consumer AB is ready.
  • The execution result

    • producers
    • Consumers A
    • Consumers B
    • Perfect for our simulated scenario requirements! Vnice!

Third, summary

  • Multiple bindings

    • In this binding case, the producer publishes a message to Exchange, and the message with bound key orange is published to queue Q1.
    • Messages with binding keys black, green, and are published to queue Q2, and messages of other message types are discarded.
  • Is it smarter than publishing and subscribing?

  • Of course, if Exchange is bound to a direct type, but it is bound to multiple queues with the same key.

  • In this case, even though the binding type is Direct, it behaves a little bit like fanout, just like broadcast.

  • That is, this thing is a complex mode can be backward compatible with simple mode!


I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah