SpringBoot利用redis集成消息隊列的方法
一、pom文件依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
二、創(chuàng)建消息接收者
變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。
@Autowired public Receiver(CountDownLatch latch) {
this.latch = latch;
}
public void receiveMessage(String message) {
LOGGER.info("收到的消息: <" + message + ">"); latch.countDown(); } }
以上基本條件達(dá)成后,以下是實現(xiàn)的三要素:
一個連接工廠
一個消息監(jiān)聽容器
Redis template
三、在application.java注入消息接收者
@Bean Receiver receiver(CountDownLatch latch) {
return new Receiver(latch); }
@Bean CountDownLatch latch() {
return new CountDownLatch(1); }
@Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory); }
四、注入消息監(jiān)聽容器
//必要的redis消息隊列連接工廠
@Bean
Receiver receiver(CountDownLatch latch) {
return new Receiver(latch);
}
//必要的redis消息隊列連接工廠
@Bean
CountDownLatch latch() {
return new CountDownLatch(1);
}
//redis模板
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
//注入消息監(jiān)聽器容器
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("msg"));
return container;
}
//注入消息監(jiān)聽器容器
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
五、單元測試
import java.util.concurrent.CountDownLatch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import com.Application;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MsgQueueTest {
@Autowired
protected ApplicationContext ctx;
private static final Logger logger = LoggerFactory.getLogger(MsgQueueTest.class);
@Test
public void SendMsg() {
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
CountDownLatch latch = ctx.getBean(CountDownLatch.class);
logger.info("我要發(fā)送消息咯...");
template.convertAndSend("msg", "歡迎使用redis的消息隊列!");
try {
//發(fā)送消息連接等待中
logger.info("消息正在發(fā)送...");
latch.await();
} catch (InterruptedException e) {
logger.info("消息發(fā)送失敗...");
}
}
}
總結(jié)
以上所述是小編給大家介紹的SpringBoot利用redis集成消息隊列的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
在啟動后臺 jar包時,使用指定的 application.yml操作
這篇文章主要介紹了在啟動后臺 jar包時,使用指定的 application.yml操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Java讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法
下面小編就為大家?guī)硪黄狫ava讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
java線程池ExecutorService超時處理小結(jié)
使用ExecutorService時,設(shè)置子線程執(zhí)行超時是一個常見需求,本文就來詳細(xì)的介紹一下ExecutorService超時的三種方法,感興趣的可以了解一下2024-09-09
Spring Cloud Feign實現(xiàn)動態(tài)URL
本文主要介紹了Spring Cloud Feign實現(xiàn)動態(tài)URL,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
SpringBoot排除不需要的自動配置類DataSourceAutoConfiguration問題
這篇文章主要介紹了SpringBoot排除不需要的自動配置類DataSourceAutoConfiguration問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07

