In the previous tutorial we created a fanout switch. We can deliver the message to multiple consumers in the form of broadcast.
What to do? Routing
In this tutorial, add a new feature where we can subscribe to only part of the message. For example, only the colors we are interested in ("orange", "black", "green") will be connected and all messages will be printed on the console.
Bind
A switch and a queue are a binding relationship. A simple understanding is that the queue is interested in the information from this exchange.
The binding can be added with an extra parameter routingKey. Spring-amqp uses a simple and easy-to-understand API (Builder Mode) to make the relationship between them very clear. Putting the switch and queue into the BindingBuilder and binding the queue to the switch easily with the routing key (routingKey).
@Beanpublic Binding binding0a(DirectExchange directExchange, Queue autoDeleteQueue0) { return BindingBuilder.bind(autoDeleteQueue0).to(directExchange).with("orange");}This means that the binding key depends on the switch type, and the fanout switch cannot do it without options that can be bound.
Direct connection switch
In the previous tutorial, our messaging system was delivered to all consumers in the form of broadcast. We want to expand on features to include filters based on color types. For example, we want a program to receive detailed error messages and write them to the hard disk as a log, without receiving Info or warning logs.
Three routing keys: orange, black and green
As shown in the figure above, there are 2 queues bound to the direct-connect exchange x. The first queue uses the routing keys is orange, and the second has 2 routing keys, black and green.
In this setting, when a message using the routing key is pushed to the switch, the message will be routed to queue Q1. When the routing key used by the message is black or green, it will be routed to Q2. The remaining messages that do not use the routing key will be discarded.
Parallel binding
Parallel binding
This can implement functions similar to fanout switches.
Almost, look at the code
Config.java
package com.zb.rabbitMQtest.t4routing.config;import org.springframework.amqp.core.*;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author Zhang Bo*/@Configuration(value = "t4Config")public class Config { /** * Created by: Zhang Bo* Time: 2018/3/5 10:45 am * @apiNote Define direct-connected exchange*/ @Bean public DirectExchange directExchange() { return new DirectExchange("direct-exchange"); } /** * Creator: Zhang Bo* Time: 2018/3/5 10:48 am * @apiNote Definition to automatically delete anonymous queue*/ @Bean public Queue autoDeleteQueue0() { return new AnonymousQueue(); } /** * Creator: Zhang Bo* Time: 2018/3/5 10:48 am * @apiNote Definition to automatically delete anonymous queue*/ @Bean public Queue autoDeleteQueue1() { return new AnonymousQueue(); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param directExchange Direct-connected switch* @param autoDeleteQueue0 Automatically delete queue* @apiNote Binding using the routing key orange queue to the direct-connected switch* @return Binding */ @Bean public Binding binding0a(DirectExchange directExchange, Queue autoDeleteQueue0) { return BindingBuilder.bind(autoDeleteQueue0).to(directExchange).with("orange"); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param directExchange DirectExchange * @param autoDeleteQueue0 Automatically delete queue* @apiNote Binding using the routing key black queue to the direct switch* @return Binding */ @Bean public Binding binding0b(DirectExchange directExchange, Queue autoDeleteQueue0) { return BindingBuilder.bind(autoDeleteQueue0).to(directExchange).with("black"); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param directExchange DirectExchange * @param autoDeleteQueue1 Automatically delete queue* @apiNote Binding using the autoDeleteQueue1 queue with the routing key black to the direct switch* @return Binding */ @Bean public Binding binding1a(DirectExchange directExchange, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(directExchange).with("black"); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param directExchange DirectExchange * @param autoDeleteQueue1 Automatically delete queue* @apiNote Binding using the routing key green queue to the direct switch* @return Binding */ @Bean public Binding binding1b(DirectExchange directExchange, Queue autoDeleteQueue1) { return BindingBuilder.bind(autoDeleteQueue1).to(directExchange).with("green"); }} Receiver.java
package com.zb.rabbitMQtest.t4routing.receiver;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;/** * @author Zhang Bo*/@Component(value = "t4Receiver")public class Receiver { @RabbitListener(queues = "#{autoDeleteQueue0.name}") public void receiver0(String str) { System.out.println("receiver0++++++++++:" + str); } @RabbitListener(queues = "#{autoDeleteQueue1.name}") public void receiver1(String str) { System.out.println("receiver1++++++++++:" + str); }} Send.java
package com.zb.rabbitMQtest.t4routing.send;import org.springframework.amqp.core.DirectExchange;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * @author Zhang Bo【[email protected]】 */@Component(value = "t4Send")public class Send { @Autowired private DirectExchange directExchange; @Autowired private RabbitTemplate rabbitTemplate; private String[] keys = {"orange", "black", "green"}; public void send() { String message = "Hahaha"; for (int i = 0; i < 5; i++) { System.out.println("send++++++++++++:".concat(message)); rabbitTemplate.convertAndSend(directExchange.getName(), keys[2], message); } }}
SendTest.java
package com.zb.rabbitMQtest.t4routing.send;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/** * @author Zhang Bo*/@RunWith(SpringRunner.class)@SpringBootTestpublic class SendTest { @Autowired private Send send; @Test public void send() throws Exception { send.send(); }} Test results: if it is keys[0], then there is only receiver0. If it is keys[1], it is similar to broadcast, with receive0 and receive1. If it is keys[2], then there is only receive1.
When keys[0]
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
receiver0++++++++++++++: Hahahaha
receiver0++++++++++++++: Hahahaha
receiver0++++++++++++++: Hahahaha
receiver0++++++++++++++: Hahahaha
receiver0++++++++++++++: HahahahaWhen keys[1]
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
receiver1++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
receiver0++++++++++++++: Hahahaha
receiver0++++++++++++++: Hahahaha
receiver0++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
receiver0++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
receiver0++++++++++++++: HahahahaWhen keys[2]
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
send++++++++++++++: Hahaha
receiver1++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
receiver1++++++++++++++: Hahahaha
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.