Spring Boot Integrated RABBITMQ (topic mode)

Source: Internet
Author: User
Tags bind rabbitmq

1.Topic Converter Introduction

Topic Exchange forwarding messages are primarily based on wildcard characters. Under this switch, the queue and switch bindings define a route pattern, so the wildcard must match the routing pattern and the routing key before the switch can forward the message.
In this switch mode:
The routing key must be a string of characters separated by a period (.), such as agreements.us, or Agreements.eu.stockholm.
The route pattern must contain an asterisk (*), which is used primarily to match a word in the location specified by the routing key, for example, a route pattern like this: agreements. B.*, then only the routing key is matched: the first word is agreements, and the fourth Word is B. The pound sign (#) is equivalent to one or more words, such as a matching pattern is agreements.eu.berlin.#, then the routing key starting with Agreements.eu.berlin is OK.
The exact code is sent at the same time, the first parameter represents the switch, the second parameter represents routing key, and the third parameter is a message. As follows:
Rabbittemplate.convertandsend ("Testtopicexchange", "Key1.a.c.key2", "This is rabbitmq!");

Topic is similar to direct, except that "mode" is supported on the match, and in the Routing_key form of "dot", two wildcard characters can be used:
* Denotes a word.
#表示零个或多个词.

As shown in the image above: This type of exchanger allows messages from different sources to reach a pair of columns, in fact, it is more clear that the meaning of the fuzzy match, for example: The Routekey of the red column in the above image is usa.#, #代表匹配任意字符, but to want the message to reach this pair of columns, USA. Must match the following # Good to be free. The usa.news,usa.weather can find the red queue, the symbol "#" matches one or more words, the symbol "" matches not many a word. So "usa.#" can be matched to "Usa.news.XXX", but "USA." matches only to "usa.xxx".
Note: The exchanger is ultimately a list of names bound to the queue. When a message is published to a switch, it is actually the channel you are connected to, comparing the message routing key to the list bound by the exchanger, and finally routing the message

2. Sample Code

1). RABBITMQ's Topic bean configuration

Rabbittopic.java class:

Package com.example.rabbitmqtopic;

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 Rabbittopic {
Final static String message = "Topic.message";
Final static String messages = "Topic.messages";
Create a queue
@Bean
Public Queue Queuemessage () {
return new Queue (rabbittopic.message);
}
Create a queue
@Bean
Public Queue queuemessages () {
return new Queue (rabbittopic.messages);
}
Create a switch
@Bean
Topicexchange Exchange () {
return new Topicexchange ("Topicexchange");
}
Binding to columns and associating to Routingkey
@Bean
Binding bindingexchangemessage (Queue queuemessage, Topicexchange Exchange) {
Return Bindingbuilder.bind (Queuemessage). to (Exchange). With ("Topic.message");
}
Binding to columns and associating to Routingkey
@Bean
Binding bindingexchangemessages (Queue queuemessages, Topicexchange Exchange) {
Return Bindingbuilder.bind (Queuemessages). to (Exchange). With ("topic.#");//* represents a word, #表示零个或多个词
}
}
2). Message producer Production messages

Topicsender.java class:

Package COM.EXAMPLE.RABBITMQTOPIC.RABBITMQ;

Import Org.springframework.amqp.core.AmqpTemplate;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;

@Component
public class Topicsender {
@Autowired
Private Amqptemplate rabbittemplate;

public void Send () {
String context = "Hi, I am message all";
System.out.println ("Sender:" + context);
This.rabbitTemplate.convertAndSend ("Topicexchange", "topic.1", context);
}

public void Send1 () {
String context = "Hi, I am message 1";
System.out.println ("Sender:" + context);
This.rabbitTemplate.convertAndSend ("Topicexchange", "topic.message", context);
}

public void Send2 () {
String context = "Hi, I am messages 2";
System.out.println ("Sender:" + context);
This.rabbitTemplate.convertAndSend ("Topicexchange", "topic.messages", context);
}
}

3). Message Consumers

Topicreceiver.java class:
Package COM.EXAMPLE.RABBITMQTOPIC.RABBITMQ;

Import Org.springframework.amqp.rabbit.annotation.RabbitHandler;
Import Org.springframework.amqp.rabbit.annotation.RabbitListener;
Import org.springframework.stereotype.Component;

@Component
@RabbitListener (queues = "Topic.message")
public class Topicreceiver {
@RabbitHandler
public void process (String message) {
System.out.println ("Topic Receiver1:" + message);
}
}

Topicreceiver2.java class:
Package COM.EXAMPLE.RABBITMQTOPIC.RABBITMQ;

Import Org.springframework.amqp.rabbit.annotation.RabbitHandler;
Import Org.springframework.amqp.rabbit.annotation.RabbitListener;
Import org.springframework.stereotype.Component;

@Component
@RabbitListener (queues = "Topic.messages")
public class TopicReceiver2 {
@RabbitHandler
public void process (String message) {
System.out.println ("Topic Receiver2:" + message);
}
}

4). Test

Rabbitmqtopictest.java class:


Package COM.EXAMPLE.RABBITMQTOPIC.RABBITMQ;

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;

@RunWith (Springrunner.class)
@SpringBootTest
public class Rabbitmqtopictest {
@Autowired
Private Topicsender sender;

@Test
public void topic () throws Exception {
Sender.send ();
}

@Test
public void Topic1 () throws Exception {
Sender.send1 ();
}

@Test
public void Topic2 () throws Exception {
Sender.send2 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.