最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot接入mq的方法示例

 更新時間:2023年09月21日 09:45:23   作者:葉秋i  
本文主要介紹了springboot接入mq的方法示例,主要實現(xiàn)配置以及實現(xiàn)一個簡單的發(fā)送、接收消息的例子,具有一定的參考價值,感興趣的可以了解一下

下面以 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依賴注入的兩種方式(根據(jù)實例詳解)

    Spring依賴注入的兩種方式(根據(jù)實例詳解)

    這篇文章主要介紹了Spring依賴注入的兩種方式(根據(jù)實例詳解),非常具有實用價值,需要的朋友可以參考下
    2017-05-05
  • B/S與C/S架構(gòu)的區(qū)別介紹

    B/S與C/S架構(gòu)的區(qū)別介紹

    本文詳細講解了B/S與C/S架構(gòu)的區(qū)別,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • JavaMe開發(fā)自適應滾動顯示

    JavaMe開發(fā)自適應滾動顯示

    我們??吹揭恍L動顯示的實例,比如UC瀏覽器中,顯示網(wǎng)頁的內(nèi)容。當內(nèi)容比較多時,采用滾動分頁顯示是合理的。在Canvas中繪圖中,多余的內(nèi)容被截斷了。如何實現(xiàn)滾動分頁顯示呢?
    2015-09-09
  • Java流程控制語句最全匯總(中篇)

    Java流程控制語句最全匯總(中篇)

    這篇文章主要介紹了Java流程控制語句最全匯總(中篇),本文章內(nèi)容詳細,通過案例可以更好的理解數(shù)組的相關(guān)知識,本模塊分為了三部分,本次為中篇,需要的朋友可以參考下
    2023-01-01
  • Java Lambda List轉(zhuǎn)Map代碼實例

    Java Lambda List轉(zhuǎn)Map代碼實例

    這篇文章主要介紹了Java Lambda List轉(zhuǎn)Map代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • IDEA編輯器兩個豎線顯示位置方式

    IDEA編輯器兩個豎線顯示位置方式

    這篇文章主要介紹了IDEA編輯器兩個豎線顯示位置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2026-05-05
  • springboot+angular4前后端分離 跨域問題解決詳解

    springboot+angular4前后端分離 跨域問題解決詳解

    這篇文章主要介紹了springboot+angular4前后端分離 跨域問題解決詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解

    Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解

    這篇文章主要介紹了Java中基于Nacos實現(xiàn)Sentinel規(guī)則持久化詳解,Sentinel Dashboard中添加的規(guī)則數(shù)據(jù)存儲在內(nèi)存,微服務停掉規(guī)則數(shù)據(jù)就消失,在?產(chǎn)環(huán)境下不合適,我們可以將Sentinel規(guī)則數(shù)據(jù)持久化到Nacos配置中?,讓微服務從Nacos獲取規(guī)則數(shù)據(jù),需要的朋友可以參考下
    2023-09-09
  • java WebSocket客戶端斷線重連的實現(xiàn)方法

    java WebSocket客戶端斷線重連的實現(xiàn)方法

    在工作中是否會遇到實用websocket客戶端連接服務端的時候,網(wǎng)絡波動,服務端斷連的情況,本文可以直接使用的斷線重連,感興趣的可以了解一下
    2021-10-10
  • SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn)

    SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn)

    本文介紹了SpringBoot+Spring Security基于內(nèi)存用戶認證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-11-11

最新評論

海林市| 江川县| 光山县| 韶山市| 藁城市| 永和县| 四平市| 东宁县| 禄丰县| 大埔区| 拜泉县| 楚雄市| 沙雅县| 南京市| 临汾市| 称多县| 邮箱| 耿马| 吴忠市| 唐海县| 衡东县| 丹凤县| 长顺县| 资溪县| 枣强县| 安陆市| 耒阳市| 台中县| 长寿区| 定州市| 安远县| 深水埗区| 水城县| 宁安市| 江陵县| 南投市| 金寨县| 长葛市| 余庆县| 乌拉特后旗| 共和县|