springboot接入mq的方法示例
下面以 RabbitMQ 為例,介紹如何在 Spring Boot 中接入 MQ。
1、添加依賴:在 pom.xml 文件中添加 RabbitMQ 的相關(guān)依賴??梢愿鶕?jù)需要選擇合適的版本,例如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>2、配置 RabbitMQ 連接信息:在 application.properties(或 application.yml)中配置 RabbitMQ 的連接信息,例如:
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
3、創(chuàng)建消息發(fā)送者:創(chuàng)建一個類來發(fā)送消息,例如 MessageSender。
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
? ? private final AmqpTemplate amqpTemplate;
? ? @Autowired
? ? public MessageSender(AmqpTemplate amqpTemplate) {
? ? ? ? this.amqpTemplate = amqpTemplate;
? ? }
? ? public void sendMessage(String exchange, String routingKey, Object message) {
? ? ? ? amqpTemplate.convertAndSend(exchange, routingKey, message);
? ? }
}在 MessageSender 類中,通過自動注入 AmqpTemplate 對象,使用 convertAndSend() 方法發(fā)送消息到指定的交換機和路由鍵。
4、創(chuàng)建消息接收者:創(chuàng)建一個類來接收消息,例如 MessageReceiver。
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiver {
? ? @RabbitListener(queues = "myQueue")
? ? public void receiveMessage(String message) {
? ? ? ? // 處理接收到的消息
? ? ? ? System.out.println("Received message: " + message);
? ? }
}在 MessageReceiver 類中,使用 @RabbitListener 注解指定需要監(jiān)聽的隊列,然后定義一個方法來處理接收到的消息。
5、配置交換機和隊列:如果需要自定義交換機和隊列,可以創(chuàng)建一個配置類來進行配置,例如:
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
? ? @Bean
? ? public Queue myQueue() {
? ? ? ? return new Queue("myQueue");
? ? }
? ? @Bean
? ? public FanoutExchange fanoutExchange() {
? ? ? ? return new FanoutExchange("myExchange");
? ? }
? ? @Bean
? ? public Binding binding(Queue myQueue, FanoutExchange fanoutExchange) {
? ? ? ? return BindingBuilder.bind(myQueue).to(fanoutExchange);
? ? }
}可以在配置類中使用 @Bean 注解來創(chuàng)建隊列、交換機和綁定規(guī)則。
6、使用消息發(fā)送者發(fā)送消息:在需要發(fā)送消息的地方,通過依賴注入 MessageSender,然后調(diào)用 sendMessage() 方法發(fā)送消息,例如:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
? ? private final MessageSender messageSender;
? ? @Autowired
? ? public MyController(MessageSender messageSender) {
? ? ? ? this.messageSender = messageSender;
? ? }
? ? @GetMapping("/send-message")
? ? public void sendMessage() {
? ? ? ? String exchange = "myExchange";
? ? ? ? String routingKey = "";
? ? ? ? Object message = "Hello, RabbitMQ!";
? ? ? ? messageSender.sendMessage(exchange, routingKey, message);
? ? }
}我們通過依賴注入 MessageSender 對象,在需要發(fā)送消息的方法中調(diào)用 sendMessage() 方法發(fā)送消息。
我們可以在 Spring Boot 中接入 RabbitMQ,并使用消息發(fā)送者發(fā)送消息,消息接收者監(jiān)聽隊列并處理接收到的消息。同時,還可以根據(jù)需要進行交換機和隊列的配置。
到此這篇關(guān)于springboot接入mq的方法示例的文章就介紹到這了,更多相關(guān)springboot接入mq內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring的UnexpectedRollbackException事務(wù)嵌套示例解析
這篇文章主要為大家介紹了spring的UnexpectedRollbackException事務(wù)嵌套示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
java 將byte中的有效長度轉(zhuǎn)換為String的實例代碼
下面小編就為大家?guī)硪黄猨ava 將byte中的有效長度轉(zhuǎn)換為String的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
Java 如何繞過迭代器遍歷時的數(shù)據(jù)修改異常
這篇文章主要介紹了Java 繞過迭代器遍歷時的數(shù)據(jù)修改異常的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-02-02
IDEA集成DeepSeek通過離線安裝解決無法安裝Proxy?AI插件問題(最新推薦)
許多開發(fā)者嘗試通過安裝Proxy?AI等插件將AI能力引入IDEA,但在實際使用中常遭遇插件安裝失敗、網(wǎng)絡(luò)連接不穩(wěn)定或兼容性沖突等問題,本文給大家介紹IDEA集成DeepSeek通過離線安裝解決無法安裝Proxy?AI插件問題,感興趣的朋友一起看看吧2019-12-12
Java中IO流 RandomAccessFile類實例詳解
這篇文章主要介紹了Java中IO流 RandomAccessFile類實例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
IDEA2020如何打開Run Dashboard的方法步驟
這篇文章主要介紹了IDEA2020如何打開Run Dashboard的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

