SpringAMQP消息隊(duì)列實(shí)戰(zhàn)教程
更新時(shí)間:2024年02月27日 11:46:42 作者:zhyaw56zhu
這篇文章主要介紹了SpringAMQP消息隊(duì)列的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
安裝RabbitMQ
在linux上安裝RabbitMQ,并運(yùn)行
docker run \ -e RABBITMQ_DEFAULT_USER=zywzy \ -e RABBITMQ_DEFAULT_PASS=123321 \ --name mq \ --hostname mq1 \ -p 15672:15672 \ -p 5672:5672 \ -d \ rabbitmq:3-management
http://ip:15672 訪問控制臺, 用戶名zywzy,密碼123321
引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>配置文件
spring:
rabbitmq:
host: 172.27.99.52 # rabbitMQ的ip地址
port: 5672 # 端口
username: abc
password: 123321
virtual-host: /發(fā)送消息
@SpringBootTest
public class SpringAmqpTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSendMessage2SimpleQueue() {
String queueName = "simple.queue";
String message = "hello, spring amqp!";
rabbitTemplate.convertAndSend(queueName, message);
}
}消費(fèi)消息
@RabbitListener(queues = "simple.queue")
public void listenWorkQueue1(String msg) throws InterruptedException {
System.out.println("消費(fèi)者1接收到消息:【" + msg + "】" + LocalTime.now());
}消息預(yù)取
每次只能取一條消息,處理完成才能取下一條消息
spring:
rabbitmq:
listener:
simple:
prefetch: 1到此這篇關(guān)于SpringAMQP消息隊(duì)列的文章就介紹到這了,更多相關(guān)SpringAMQP消息隊(duì)列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot3.x使用es進(jìn)行數(shù)據(jù)查詢的方式
SpringBoot3中,Elasticsearch的版本升級至8.x,相關(guān)寫法有所改變,SpringBoot2中的ElasticsearchRestTemplate已被移除,推薦使用ElasticsearchTemplate或ElasticsearchClient進(jìn)行交互,本文介紹這兩個(gè)類的使用方式,并說明了依賴引入和自動裝配的配置,感興趣的朋友一起看看吧2025-12-12
Java內(nèi)存分配與JVM參數(shù)詳解(推薦)
本文詳解JVM內(nèi)存結(jié)構(gòu)與參數(shù)調(diào)整,涵蓋堆分代、元空間、GC選擇及優(yōu)化策略,幫助開發(fā)者提升性能、避免內(nèi)存泄漏,本文給大家介紹Java內(nèi)存分配與JVM參數(shù)詳解,感興趣的朋友一起看看吧2025-06-06
intelliJ IDEA 多行選中相同內(nèi)容的快捷鍵分享
這篇文章主要介紹了intelliJ IDEA 多行選中相同內(nèi)容的快捷鍵分享,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
javaweb實(shí)現(xiàn)百度GPS定位接口(經(jīng)緯度)
這篇文章主要介紹了javaweb實(shí)現(xiàn)百度GPS定位接口(經(jīng)緯度),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

