Introduce a,

The previous chapter talked about the ability of direct switches to selectively receive messages based on routing keys. But it still has limitations, it can not implement multi-conditional routing.

More flexible Topic switch: Topic

When using a topic switch, you cannot use arbitrary routing keys. The routing keys should take the form of words separated by dots. You can use any word you want, and it usually means something. The maximum word size is 255 bytes

Defining routing keys with topic switches requires two considerations

  1. *An asterisk represents a word
  2. #A pound sign stands for zero or more words
topic_exchange

Here, for example, a topic switch receives a message describing the product. The message will be sent with a routing key consisting of three words and two dots. The first word describes the brand, the second describes the type of product, and the third describes the color.

Create three bindings, queue Tom and key *.phone.* and queue Jerry and *.*. Green and apple.#

The three binding relationships are summarized as follows

  1. Tom is interested in mobile phones
  2. Jerry likes green and Apple-branded products
  • The key route forapple.phone.greenThe message will be passed toTomJerryThese two queues
  • The key route formi.phone.yellowThe message will be passed toTomThe queue
  • The key route forapple.watch.greenThe message will be passed toJerryThe queue
  • The key route forapple apple.laptop apple.mac.red.chinaIs passed to the Jerry queue because a match has been madeapple.#On this rule
  • The key route formi.watch.red Lenovo.laptop.blackIs discarded because no queue is a match

Topic switches are very powerful, why so bloated? 1. When a queue’s binding key is # (pound sign), the queue will ignore the routing key and receive all messages. 2. The behavior of the directly connected switch is owned by the topic switch when the special characters * (asterisk) and # (pound sign) are not present in the binding key. So the theme switch also realizes the function of the sector switch, and the function of the direct switch.

Web console operation

Creating a Switch

Creating a Switch

Create queue Tom, Jerry

Create queue Tom
Create queue Jerry

Bind the queue, fill in the corresponding routing key, and finally:

Routing binding

We publish a message in the switch. Specify a routing key

Send messages to switch rabbit_exchange_topic with the routing key mi.phone. Green

At this point, both queues receive messages

Both queues received the message
Jerry queue view

Iii. Code mode:

Declare queues, switches, route bindings

import org.springframework.amqp.core.Binding;

import org.springframework.amqp.core.BindingBuilder;

import org.springframework.amqp.core.Queue;

import org.springframework.amqp.core.TopicExchange;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;



@Configuration

public class Demo_05_Config {

    @Bean

    public Queue queueTom(a) {

        return new Queue("Tom");

    }

    @Bean

    public Queue queueJerry(a) {

        return new Queue("Jerry");

    }

    // Declare a topic switch

    @Bean

    public TopicExchange topicExchange(a) {

        return new TopicExchange("rabbit_exchange_topic");

    }

    @Bean

    public Binding bindingTom(a) {

        String routingKey = "*.phone.*";

        return BindingBuilder.bind(queueTom()).to(topicExchange()).with(routingKey);

    }

    @Bean

    public Binding bindingJerry1(a) {

        String routingKey = "*.*.green";

        return BindingBuilder.bind(queueJerry()).to(topicExchange()).with(routingKey);

    }

    @Bean

    public Binding bindingJerry2(a) {

        String routingKey = "apple.#";

        return BindingBuilder.bind(queueJerry()).to(topicExchange()).with(routingKey);

    }

}

Copy the code

consumers

import org.springframework.amqp.rabbit.annotation.RabbitListener;

import org.springframework.amqp.support.AmqpHeaders;

import org.springframework.messaging.handler.annotation.Header;

import org.springframework.stereotype.Component;



@Component

public class Demo_05_Consumer {

    @RabbitListener(queues = "Tom")

    public void receive(String msg, @Header(value = AmqpHeaders.RECEIVED_ROUTING_KEY) String routingKey) {

        System.out.println("Tom receives product information:" + msg + ", the routing key is: [" + routingKey + "】");

    }

    @RabbitListener(queues = "Jerry")

    public void receive2(String msg, @Header(value = AmqpHeaders.RECEIVED_ROUTING_KEY) String routingKey) {

        System.out.println("Jerry receives product information:" + msg + ", the routing key is: [" + routingKey + "】");

    }

}

Copy the code

Producers:

@Test

public void demo_05_Producer(a) {

    String exchange = "rabbit_exchange_topic";



    String routingKey1 = "apple.phone.green";

    String routingKey2 = "mi.phone.red";

    String routingKey3 = "apple.watch.green";

    String routingKey4 = "apple.laptop";

    String routingKey5 = "mi.watch.red";



    String msg1 = IPhone (green);

    String msg2 = "Xiaomi Phone (yellow)";

    String msg3 = "Apple Watch (green)";

    String msg4 = Apple Computer;

    String msg5 = Xiaomi Watch (red);



    rabbitTemplate.convertAndSend(exchange, routingKey1, msg1);

    rabbitTemplate.convertAndSend(exchange, routingKey2, msg2);

    rabbitTemplate.convertAndSend(exchange, routingKey3, msg3);

    rabbitTemplate.convertAndSend(exchange, routingKey4, msg4);

    rabbitTemplate.convertAndSend(exchange, routingKey5, msg5);

}

Copy the code

Output:

TomReceived product information: iPhone (green), routing key: [apple.phone.green

JerryReceived product information: iPhone (green), routing key: [apple.phone.green

JerryReceived product information: Apple Watch (green), routing key: [apple.watch.green

TomReceived product information: Xiaomi mobile phone (yellow), routing key: [mi.phone.red

JerryReceived product information: Apple computer, routing key is: [apple.laptop

Copy the code

Msg5’s message was discarded because it did not match the routing key.