"Go" Spring Boot consolidation RABBITMQ (topic mode)

Source: Internet
Author: User
Tags rabbitmq
Spring Boot Consolidation RABBITMQ (topic mode)Reproduced September 07, 2017 14:33:46 259

Introduction to 1.Topic Converter

Topic Exchange forwarding messages are based primarily on wildcard characters. Under this switch, the binding of queues and switches defines a routing pattern, and the wildcard will have to be matched between this routing mode and the routing key before the switch can forward messages.
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 routing mode must contain an asterisk (*), which is used primarily to match a word in the specified location of the routing key, for example, a routing pattern is: agreements. b.*, then you can only match the routing key as follows: The first word is agreements, and the fourth Word is B. The Well sign (#) is the equivalent of one or more words, for example, a matching pattern is agreements.eu.berlin.#, so the routing keys that begin with Agreements.eu.berlin are OK.
The exact code sends the same time, the first parameter represents the switch, the second parameter represents routing key, and the third parameter is the message. As follows:
Rabbittemplate.convertandsend ("Testtopicexchange", "Key1.a.c.key2", "This is rabbitmq!");

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

As shown in the figure above: This type of switch enables messages from different sources to reach a pair of columns, which in fact is more clearly the meaning of a fuzzy match, for example: The Routekey of red columns in the image above is usa.#, #代表匹配任意字符, but to get the message to the right column, USA. Must match the back of the # 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 words. Therefore "usa.#" can match to "Usa.news.XXX", but "USA." will only match to "usa.xxx".
Note: The switch is ultimately a list of names tied to queues. When the message is released to the exchanger, it is actually the channel you are connecting 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";
creating queues
@Bean
Public Queue Queuemessage () {
return new Queue (rabbittopic.message);
}
creating queues
@Bean
Public Queue queuemessages () {
return new Queue (rabbittopic.messages);
}
Create a switch
@Bean
Topicexchange Exchange () {
return new Topicexchange ("Topicexchange");
}
Binds the column and associates it to the Routingkey
@Bean
Binding bindingexchangemessage (Queue queuemessage, Topicexchange Exchange) {
Return Bindingbuilder.bind (Queuemessage). to (Exchange). With ("Topic.message");
}
Binds the column and associates it to the Routingkey
@Bean
Binding bindingexchangemessages (Queue queuemessages, Topicexchange Exchange) {
Return Bindingbuilder.bind (Queuemessages). to (Exchange). With ("topic.#");//* represents a word, #表示零个或多个词
}
}
2). Message producer Production Message

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 ();
}
}

https://blog.csdn.net/liangwenmail/article/details/77881270

Related Article

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.