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

SpringCloud distributed micro-service b2b2c e-commerce (13) Springboot integration RabbitMQ

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to integrate the RabbitMQ server and how to send and receive messages through it. I will build a springboot project through the

RabbitTemplate subscribes to a message of type POJO via MessageListenerAdapter.

Preparatory work

15min

IDEA

Maven 3.0

Before starting to build the project, the machine needs to install rabbitmq, you can go to the official website to download, http://www.rabbitmq.com/download.html, if

You use Mac (programmers should use mac). You can download it like this:

Brew install rabbitmq

Start the server after the installation is complete:

Rabbitmq-server

After starting the server successfully, you can see the following message:

Enable the server after the brew install rabbitmq installation is completed: if rabbitmq-server starts the server successfully, you can see the following message:

Construction project

To construct a SpringBoot project, its pom file dependence plus spring-boot-starter-amqp start-up dependence:

Org.springframework.boot spring-boot-starter-amqp

Create a message recipient

In any message queuing program, you need to create a message receiver to respond to the message sent.

@ Componentpublic class Receiver {private CountDownLatch latch = new CountDownLatch (1); public void receiveMessage (String message) {System.out.println ("Received"); latch.countDown ();} public CountDownLatch getLatch () {return latch;}}

The message receiver is a simple POJO class that defines a method to receive the message. When you register it to receive the message, you can give it any name. To understand the springcloud architecture, please add: three, six, two, four, seven, two, 59. Among them, it has a class such as CountDownLatch, which is used to tell the sender that the message has been received, you do not need to implement it in the application, you just need to latch.countDown ().

Create a message monitor and send a message

In the spring program, RabbitTemplate provides all the methods for sending and receiving messages. All you need is a simple configuration:

Need a message listening container

Declare a quene, an exchange, and bind them

A component to send a message

The code listing is as follows:

Package com.forezp;import com.forezp.message.Receiver;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.amqp.rabbit.connection.ConnectionFactory;import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean @ SpringBootApplicationpublic class SpringbootRabbitmqApplication {final static String queueName = "spring-boot"; @ Bean Queue queue () {return new Queue (queueName, false);} @ Bean TopicExchange exchange () {return new TopicExchange ("spring-boot-exchange");} @ Bean Binding binding (Queue queue, TopicExchange exchange) {return BindingBuilder.bind (queue) .to (exchange) .with (queueName) } @ Bean SimpleMessageListenerContainer container (ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {SimpleMessageListenerContainer container = new SimpleMessageListenerContainer (); container.setConnectionFactory (connectionFactory); container.setQueueNames (queueName); container.setMessageListener (listenerAdapter); return container;} @ Bean MessageListenerAdapter listenerAdapter (Receiver receiver) {return new MessageListenerAdapter (receiver, "receiveMessage") } public static void main (String [] args) {SpringApplication.run (SpringbootRabbitmqApplication.class, args);}}

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

Internet Technology

Wechat

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

12
Report