In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article analyzes "what are the common models of springBoot integration rabbitmq testing?" The content is detailed and easy to understand, and friends who are interested in "what are the common models of springBoot integration rabbitmq testing" can follow the editor's idea to read it slowly and deeply. I hope it will be helpful to you after reading. Let's follow the editor to learn more about "what are the common models of springBoot integration rabbitmq testing".
In enterprise development, we are more likely to use spring framework to integrate other technologies, springboot is more convenient to provide a variety of starter to quickly add dependencies, complete integration, out of the box.
1. Add dependency org.springframework.boot spring-boot-starter-amqp2. Write configuration
The configuration information includes ip, port, virtual host, user name and password, which is consistent with the configuration information required by the native java code.
Spring: application: name: spirngboot-rabbitmq rabbitmq: host: 192.168.20.128 port: 5672 virtual-host: / vh username: wuwl password: 1234563. Write and test
The following is mainly aimed at the first five commonly used models, integrate rabbitmq on the basis of the spirngboot framework and test it.
(1) Hello World model
This is a simple direct connection model, in which the producer sends the message directly to the message queue, and the consumer binds the message queue and obtains it directly, one-to-one.
Spring-boot-starter-amqp provides us with an org.springframework.amqp.rabbit.core.RabbitTemplate class to make it easy for us to use rabbitmq, which can be injected automatically.
Producer test class:
@ SpringBootTest (classes = RabbitmqDemoApplication.class) @ RunWith (SpringRunner.class) public class RabbitmqDemoApplicationTests {@ Autowired private RabbitTemplate rabbitTemplate; @ Test public void testHelloQueues () {rabbitTemplate.convertAndSend ("hello", "hello world");}}
The producer sends messages to a queue named hello, but without a consumer, the producer makes no sense. In addition, the first parameter of the convertAndSend method does not mean message queue, but routingKey. If we find the originally defined interface based on the source code, we can see the following:
/ * * Convert a Java object to an Amqp {@ link Message} and send it to a default exchange * with a specific routing key. * * @ param routingKey the routing key * @ param message a message to send * @ throws AmqpException if there is a problem * / void convertAndSend (String routingKey, Object message) throws AmqpException
The second parameter is of type Object, that is, any type of object can be passed. This method converts the object into an Amqp message and sends it to a default switch, and routingKey is the content of the first parameter, without mentioning the message queue information, but we can analyze that the routingKey here should have the same name as queues.
Consumer category:
@ Component@RabbitListener (queuesToDeclare = @ Queue ("hello")) public class HelloQueuesConsumer {@ RabbitHandler public void consume (String msg) {System.out.println ("consumption message:" + msg + "" + System.currentTimeMillis ());}}
The above code is equivalent to:
@ Componentpublic class HelloQueuesConsumer {@ RabbitListener (queuesToDeclare = @ Queue ("hello")) public void consume (String msg) {System.out.println ("consumption message:" + msg + "" + System.currentTimeMillis () }} @ RabbitListener can be marked on the class. You need to use the @ RabbitListener annotation together with the @ RabbitHandler annotation to indicate that when a message is received, it will be handled by the @ RabbitHandler method. Which method should be used, according to the parameter type after MessageConverter conversion?
Directly starting the test method, that is, the producer, can see:
The consumer receives the information in the message queue and prints it.
(2) work queues model
Producer testing method, the class is consistent with the first model
@ Testpublic void testWorkQueues () {for (int I = 0; I < 20; iTunes +) {rabbitTemplate.convertAndSend ("work", "work index" + I);}}
Consumer category:
@ Componentpublic class WorkQueuesConsumer {@ RabbitListener (queuesToDeclare = @ Queue ("work")) public void consume1 (String msg) {System.out.println ("consumer1 consumption message:" + msg);} @ RabbitListener (queuesToDeclare = @ Queue ("work") public void consume2 (String msg) {System.out.println ("consumer2 consumption message:" + msg);}}
Start the producer test method:
Consumer one and consumer two evenly distribute the message tasks in the queue, even if the execution efficiency of the two is not consistent.
(3) Publish/Subscribe model
Producer testing methods:
For (int I = 0; I < 20; iTunes +) {rabbitTemplate.convertAndSend ("amq.fanout", "", "fanout msg" + I);}
Consumer category:
@ Componentpublic class FanoutQueuesConsumer {@ RabbitListener (bindings = {@ QueueBinding (value = @ Queue, exchange = @ Exchange (value = "amq.fanout", type = "fanout")}) public void consume1 (String msg) {System.out.println ("consumer1 consumption message:" + msg) } @ RabbitListener (bindings = {@ QueueBinding (value = @ Queue, exchange = @ Exchange (value = "amq.fanout", type = "fanout"))}) public void consume2 (String msg) {System.out.println ("consumer2 consumption message:" + msg);}}
Note the switch information here
Start the producer test method:
Only part of the printed information is pasted here, and the two consumers get the same message, and the producer sends the message to the switch, which sends it to all temporary message queues that have been registered with the switch, and the consumer gets the message in the queue.
(4) Routing model
Producer testing methods:
Testpublic void testDirectQueues () {rabbitTemplate.convertAndSend ("amq.direct", "info", "routingKey is info"); rabbitTemplate.convertAndSend ("amq.direct", "warn", "routingKey is warn"); rabbitTemplate.convertAndSend ("amq.direct", "error", "routingKey is error");}
Routing also becomes the fanout model, and the corresponding switch type is direct.
Consumer category:
@ Componentpublic class DirectQueuesConsumer {@ RabbitListener (bindings = {@ QueueBinding (value = @ Queue, exchange = @ Exchange (value = "amq.direct", type = "direct"), key = {"info", "warn" "error"}) public void consume1 (String msg) {System.out.println ("consumer1 consumption message:" + msg) } @ RabbitListener (bindings = {@ QueueBinding (value = @ Queue, exchange = @ Exchange (value = "amq.direct", type = "direct") Key = "error")}) public void consume2 (String msg) {System.out.println ("consumer2 consumption message:" + msg) }}
Start the producer test class:
Consumer one is configured with three types of routingKey, so all three types of messages can be received, while consumer two can only receive messages of type error.
(5) Topic model
Producer testing methods:
Testpublic void testTopicQueues () {rabbitTemplate.convertAndSend ("amq.topic", "file.info", "routingKey is info"); rabbitTemplate.convertAndSend ("amq.topic", "file.warn", "routingKey is warn"); rabbitTemplate.convertAndSend ("amq.topic", "file.error", "routingKey is error");}
Consumer category:
@ Componentpublic class TopicQueuesConsumer {@ RabbitListener (bindings = {@ QueueBinding (value = @ Queue, exchange = @ Exchange (value = "amq.topic", type = "topic") Key = {"#"}) public void consume1 (String msg) {System.out.println ("consumer1 consumption message:" + msg) } @ RabbitListener (bindings = {@ QueueBinding (value = @ Queue, exchange = @ Exchange (value = "amq.topic", type = "topic") Key = "* .error")}) public void consume2 (String msg) {System.out.println ("consumer2 consumption message:" + msg) }}
Start the producer test method:
Consumer 1 can accept any type of message with a configured routingKey of #. Consumer 2 can accept any word plus .error as routingKey.
On the springBoot integration of rabbitmq testing commonly used models to share here, I hope the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.