springboot整合各種訊息佇列(一):redis訊息佇列
本篇部落格將介紹使用redis作為訊息中介軟體和springboot的整合使用;
安裝配置redis
請參考筆者另一篇部落格,有詳細介紹: https://jsbintask.cn/2019/01/24/middleware/redis-install/#more
整合springboot
新建專案
新建一個springboot專案,並且修改application.yml檔案,pom如下:
spring: redis: host: youripaddress password: jsbintask
host和password修改成自己的伺服器使用者名稱密碼。pom:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.jsbintask</groupId> <artifactId>springboot-redis-learning</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-redis-learning</name> <description>Demo project for Spring Boot redis</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
訊息消費者(接收者)
編寫一個訊息消費者類:
@Log @Component public class RedisMessageReceiver { @Autowired private CountDownLatch countDownLatch; public void receivedMsg(String msg) { log.info("received msg: " + msg); // 計數,減一 countDownLatch.countDown(); } }
並且加入@Component註解,將其作為bean歸spring管理,並且通過@Autowried注入了一個CountdownLatch類。
將訊息消費者作為監聽器監聽 redis的訊息:
@Configuration public class RedisConfig { public static final String MSG_TOPIC = "chat"; @Bean public CountDownLatch countDownLatch() { return new CountDownLatch(1); } /** * 訊息消費者 介面卡,其中 receivedMsg為定義的消費者的消費方法,必須保持一致 */ @Bean MessageListenerAdapter listenerAdapter(RedisMessageReceiver receiver) { return new MessageListenerAdapter(receiver, "receivedMsg"); } @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { return new StringRedisTemplate(connectionFactory); } /** * 訊息監聽容器,將介面卡加入, 注意此處的 topic */ @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter, new PatternTopic(MSG_TOPIC)); return container; } }
啟動測試類,傳送訊息
@SpringBootApplication public class SpringbootRedisLearningApplication { public static void main(String[] args) throws Exception { ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootRedisLearningApplication.class, args); //從 spring中取出已經有的bean CountDownLatch countDownLatch = applicationContext.getBean(CountDownLatch.class); StringRedisTemplate stringRedisTemplate = applicationContext.getBean(StringRedisTemplate.class); stringRedisTemplate.convertAndSend(RedisConfig.MSG_TOPIC, "hello from jsbintask."); // 一直等待訊息被接收,沒接收不退出 countDownLatch.await(); } }
啟動,檢視控制檯:收到訊息並且列印:

這樣redis作為訊息佇列就成功了。 原始碼地址: https://github.com/jsbintask22/springboot-redis-learning
本文原創地址: https://jsbintask.cn/2019/01/25/springboot/springboot-redis-jms/ ,未經允許,禁止轉載。
謝謝你支援我分享知識
掃碼打賞,心意已收

開啟 微信 掃一掃,即可進行掃碼打賞哦