Spring?Boot實(shí)現(xiàn)消息的發(fā)送和接收使用實(shí)戰(zhàn)指南
正文
當(dāng)涉及到消息發(fā)送和接收的場(chǎng)景時(shí),可以使用Spring Boot和消息中間件RabbitMQ來(lái)實(shí)現(xiàn)。下面是一個(gè)簡(jiǎn)單的示例代碼,展示了如何在Spring Boot應(yīng)用程序中創(chuàng)建消息發(fā)送者和接收者,并發(fā)送和接收一條消息
準(zhǔn)備工作
首先,你需要進(jìn)行以下準(zhǔn)備工作
- 確保你已經(jīng)安裝了Java和Maven,并設(shè)置好相應(yīng)的環(huán)境變量。
- 選擇一個(gè)消息中間件作為你的消息代理,并確保已經(jīng)安裝和配置好該消息中間件。
- 創(chuàng)建一個(gè)新的Spring Boot項(xiàng)目,并添加相應(yīng)的依賴(lài)項(xiàng)。
編寫(xiě)代碼
現(xiàn)在,讓我們來(lái)編寫(xiě)代碼
- 創(chuàng)建一個(gè)名為
MessageSender
的類(lèi),用于發(fā)送消息。
import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MessageSender { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("queue_email", message); System.out.println("Message sent: " + message); } }
- 創(chuàng)建一個(gè)名為
MessageReceiver
的類(lèi),用于接收消息。
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageReceiver { @RabbitListener(queues = "queue_email") public void receiveMessage(String message) { System.out.println("Message received: " + message); } }
- 創(chuàng)建一個(gè)名為
Application
的類(lèi),作為啟動(dòng)類(lèi)。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
- 創(chuàng)建一個(gè)名為
application.properties
的配置文件,并添加以下配置:
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
以上代碼示例中使用了RabbitMQ作為消息中間件,你可以根據(jù)自己的需求選擇其他消息中間件,并相應(yīng)地更改配置。
配置指定的隊(duì)列
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 queue() { return new Queue("queue_email"); } }
現(xiàn)在你可以在應(yīng)用程序的其他地方使用MessageSender
類(lèi)發(fā)送消息,例如在某個(gè)控制器中:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MessageController { @Autowired private MessageSender messageSender; @GetMapping("/send-message") public String sendMessage() { messageSender.sendMessage("Hello, World!"); return "Message sent"; } }
當(dāng)你運(yùn)行這個(gè)Spring Boot應(yīng)用程序時(shí),可以通過(guò)訪(fǎng)問(wèn)/send-message
端點(diǎn)來(lái)發(fā)送一條消息。這條消息將被發(fā)送到名為queue_email
的隊(duì)列中,并由MessageReceiver
類(lèi)中的receiveMessage
方法接收和處理。
這是一個(gè)簡(jiǎn)單的示例,用于演示如何在Spring Boot應(yīng)用程序中發(fā)送和接收消息??梢愿鶕?jù)實(shí)際需求進(jìn)行修改和擴(kuò)展,添加更多的功能和業(yè)務(wù)邏輯。
相關(guān)文章
idea快捷鍵生成getter和setter,有構(gòu)造參數(shù),無(wú)構(gòu)造參數(shù),重寫(xiě)toString方式
這篇文章主要介紹了java之idea快捷鍵生成getter和setter,有構(gòu)造參數(shù),無(wú)構(gòu)造參數(shù),重寫(xiě)toString方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11SpringBoot+Vue+Flowable模擬實(shí)現(xiàn)請(qǐng)假審批流程
這篇文章主要為大家詳細(xì)介紹了如何利用SpringBoot+Vue+Flowable模擬實(shí)現(xiàn)一個(gè)請(qǐng)假審批流程,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-08-08Java數(shù)組常用排序算法實(shí)例小結(jié)
這篇文章主要介紹了Java數(shù)組常用排序算法,結(jié)合實(shí)例形式總結(jié)分析了java數(shù)組常用的4種排序算法,包括冒泡排序、數(shù)組遞增排序、快速排序及選擇排序,需要的朋友可以參考下2017-12-12java實(shí)現(xiàn)發(fā)送郵件的示例代碼
這篇文章主要介紹了java如何實(shí)現(xiàn)發(fā)送郵件,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法
Spring?Boot是一個(gè)用于構(gòu)建Web應(yīng)用程序的框架,通過(guò)AOP可以實(shí)現(xiàn)防止表單重復(fù)提交,本文介紹了在Spring?Boot應(yīng)用程序中使用AOP和Redis來(lái)防止表單重復(fù)提交的方法,需要的朋友可以參考下2023-04-04Springboot項(xiàng)目長(zhǎng)時(shí)間不進(jìn)行接口操作,提示HikariPool-1警告的解決
這篇文章主要介紹了Springboot項(xiàng)目長(zhǎng)時(shí)間不進(jìn)行接口操作,提示HikariPool-1警告的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán)
這篇文章主要介紹了詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03SpringBoot獲取yml和properties配置文件的內(nèi)容
這篇文章主要為大家詳細(xì)介紹了SpringBoot獲取yml和properties配置文件的內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot的監(jiān)控(Actuator)功能用法詳解
這篇文章主要介紹了SpringBoot的監(jiān)控(Actuator)功能用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03