Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the Integration of message queuing RabbitMQ and Spring

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to analyze the integration of message queuing RabbitMQ and Spring? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Introduction to 1.RabbitMQ

RabbitMQ is a popular open source message queuing system developed in Erlang language. RabbitMQ is the standard implementation of AMQP (Advanced message queuing Protocol).

2.Spring integrated RabbitMQ2.1 maven configuration / / pom.xml com.rabbitmq amqp-client 3.5.1 org.springframework.amqp spring-rabbit 1.4.5.RELEASE 2.2 rabbmitmq configuration file / / rabbitmq-config.propertiesmq.host=127.0.0.1mq.username=testmq.password=123456mq.port=5672mq.vhost=testmq2.3 Spring configuration / / application-mq.xml rabbitmq connection service configuration 3. Declare a message queue Queue//application-mq.xml in Spring using RabbitMQ3.1

Description:

Durable: whether to persist or not

Exclusive: only private queues that can be used by creators are automatically deleted when disconnected

Auto_delete: whether to delete the queue automatically when all consumer clients are disconnected

3.2 switch definition / / application-mq.xml

Description:

Rabbit:direct-exchange: define the exchange schema as direct, which means that the message exactly matches a specific routing key before it is forwarded.

Rabbit:binding: sets the key that the message queue matches

Send messages Producer//MQProducer.javapublic interface MQProducer {/ * send messages to the specified queue * @ param queueKey * @ param object * / public void sendDataToQueue (String queueKey, Object object);} @ Servicepublic class MQProducerImpl implements MQProducer {@ Autowired private AmqpTemplate amqpTemplate; private final static Logger LOGGER = Logger.getLogger (MQProducerImpl.class) / * (non-Javadoc) * @ see com.stnts.tita.rm.api.mq.MQProducer#sendDataToQueue (java.lang.String, java.lang.Object) * / @ Override public void sendDataToQueue (String queueKey, Object object) {try {amqpTemplate.convertAndSend (queueKey, object);} catch (Exception e) {LOGGER.error (e);}

Description:

ConvertAndSend: converts the Java object into a message and sends it to the Exchange in the switch that matches the Key. Since the JSON transformation is configured, the Java object is converted into a JSON string.

Original text: Convert a Java object to an Amqp Message and send it to a default exchange with a specific routing key.

3.4 asynchronously receive message Consumer

Define listeners

/ / QueueListenter.java@Componentpublic class QueueListenter implements MessageListener {@ Override public void onMessage (Message msg) {try {System.out.print (msg.toString ());} catch (Exception e) {e.printStackTrace ();}

Monitoring configuration

/ / application-mq.xml

Description:

Queues: the queue for listening. Multiple words are separated by commas (,).

Ref: listener

3.5 JUnit Test / / TestQueue.java@RunWith (value = SpringJUnit4ClassRunner.class) @ ContextConfiguration (locations = {"classpath:/ApplicationContext/ApplicationContext-mq.xml"}) public class TestQueue {@ Autowired MQProducer mqProducer; final String queue_key = "test_queue_key"; @ Test public void send () {Map msg = new HashMap (); msg.put ("data", "hello,rabbmitmq!"); mqProducer.sendDataToQueue (query_key,msg);}}

Run the test program, Run with JUnit, and send a message to test_queue. The listener listens to the message and prints it out.

So far, the integration, configuration, and use of Spring and RabbmitMQ have been completed.

This is the answer to the question on how to analyze the integration of RabbitMQ and Spring in message queuing. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report