In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how SpringBoot integrates RabbitMQ in Java", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to integrate SpringBoot in Java".
If you want to do RabbitMQ integration, you must pay attention to the following concepts: swap space, virtual host, queue information. For convenience, the project is divided into two parts: RabbitMQ-Consumer and RabbitMQ-Producer.
1. [two projects] copy the dependency support package of rabbitmq to the project
Org.springframework.boot spring-boot-starter-amqp
2. [microboot-rabbitmq-producer, microboot-rabbitmq-consumer] modify the application.yml configuration file and append the relevant configuration items of rabbitmq:
Server: port: 80spring: messages: basename: i18n/Messages,i18n/Pages rabbitmq: addresses: rabbitmq-server username: studyjava password: hello virtual-host: /
3. [microboot-rabbitmq-producer] set up a message sending interface:
Package cn.study.microboot.producer;public interface IMessageProducerService {public void sendMessage (String msg);}
4. [microboot-rabbitmq-producer] in order to use RabbitMQ for message processing normally, you also need to do a message production configuration class
Package cn.study.microboot.config;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.DirectExchange;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ProducerConfig {public static final String EXCHANGE = "study.microboot.exchange"; / / swap space name public static final String ROUTINGKEY = "study.microboot.routingkey" / / set route key public static final String QUEUE_NAME = "study.microboot.queue"; / / queue name @ Bean public Binding bindingExchangeQueue (DirectExchange exchange,Queue queue) {return BindingBuilder.bind (queue) .to (exchange) .with (ROUTINGKEY);} @ Bean public DirectExchange getDirectExchange () {/ / use directly connected mode return new DirectExchange (EXCHANGE, true, true) } @ Bean public Queue queue () {/ / queue information to be created return new Queue (QUEUE_NAME);}}
5. [microboot-rabbitmq-producer] create the implementation subclass of the message service:
Package cn.study.microboot.producer.impl;import javax.annotation.Resource;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.stereotype.Service;import cn.study.microboot.config.ProducerConfig;import cn.study.microboot.producer.IMessageProducerService;@Servicepublic class MessageProducerServiceImpl implements IMessageProducerService {@ Resource private RabbitTemplate rabbitTemplate; @ Override public void sendMessage (String msg) {this.rabbitTemplate.convertAndSend (ProducerConfig.EXCHANGE, ProducerConfig.ROUTINGKEY, msg);}}
6. [microboot-rabbitmq-consumer] still needs to do a consumer configuration program class, and the main purpose of this program class is still to set swap space, route KEY and other information.
Package cn.study.microboot.config;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.DirectExchange;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ConsumerConfig {public static final String EXCHANGE = "study.microboot.exchange"; / / swap space name public static final String ROUTINGKEY = "study.microboot.routingkey" / / set route key public static final String QUEUE_NAME = "study.microboot.queue"; / / queue name @ Bean public Queue queue () {/ / queue information to be created return new Queue (QUEUE_NAME);} @ Bean public DirectExchange getDirectExchange () {/ / use directly connected mode return new DirectExchange (EXCHANGE, true, true) } @ Bean public Binding bindingExchangeQueue (DirectExchange exchange,Queue queue) {return BindingBuilder.bind (queue) .to (exchange) .with (ROUTINGKEY);}}
7. [microboot-rabbitmq-consumer] implement the listening processing class:
Package cn.study.microboot.consumer;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Service;@Servicepublic class MessageConsumerService {@ RabbitListener (queues= "study.microboot.queue") public void receiveMessage (String text) {/ / perform message reception processing System.err.println ("[* receive message * *]" + text);}}
8. [microboot-rabbitmq-producer] create a test class to implement message sending processing.
Package cn.study.microboot.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import cn.study.microboot.StartSpringBootMain;import cn.study.microboot.producer.IMessageProducerService;@SpringBootTest (classes = StartSpringBootMain.class) @ RunWith (SpringJUnit4ClassRunner.class) @ WebAppConfigurationpublic class TestActiveMQ {@ Resource private IMessageProducerService messageProducer @ Test public void testSend () throws Exception {for (int x = 0; x < 100; x +) {this.messageProducer.sendMessage ("study -" + x);}
9. [microboot-rabbitmq-consumer] write a message receiving test class, in which you don't need to write code, you just need to sleep:
Package cn.study.microboot;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;@SpringBootTest (classes = StartSpringBootMain.class) @ RunWith (SpringJUnit4ClassRunner.class) @ WebAppConfigurationpublic class AppTest {@ Test public void testStart () throws Exception {Thread.sleep (Long.MAX_VALUE);}}
The process of integration in the overall project development is still simple, but it is important to note that since it is the first time to integrate, the configuration classes of producers and consumers are separated. In fact, the functions of these two classes are exactly the same.
The above is all the contents of the article "how SpringBoot integrates RabbitMQ in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.