SpringBoot利用redis整合訊息佇列,springbootredis

來源:互聯網
上載者:User

SpringBoot利用redis整合訊息佇列,springbootredis
一、pom檔案依賴

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-redis</artifactId> 
</dependency>

 

二、建立訊息接收者
變數、方法及建構函式進行標註,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。@Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } public void receiveMessage(String message) { LOGGER.info("收到的訊息: <" + message + ">"); latch.countDown(); } }

 

以上基本條件達成後,以下是實現的三要素:
  • 一個串連工廠
  • 一個訊息監聽容器
  • 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); }

 四、注入訊息監聽容器
//必要的redis訊息佇列串連工廠@BeanReceiver receiver(CountDownLatch latch) {return new Receiver(latch);} //必要的redis訊息佇列串連工廠@BeanCountDownLatch latch() {return new CountDownLatch(1);} //redis模板@BeanStringRedisTemplate template(RedisConnectionFactory connectionFactory) {return new StringRedisTemplate(connectionFactory);} //注入訊息監聽器容器@BeanRedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);container.addMessageListener(listenerAdapter, new PatternTopic("msg"));return container;} //注入訊息監聽器容器@BeanMessageListenerAdapter 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 {@Autowiredprotected ApplicationContext ctx;private static final Logger logger = LoggerFactory.getLogger(MsgQueueTest.class);@Testpublic void SendMsg() {StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);CountDownLatch latch = ctx.getBean(CountDownLatch.class);logger.info("我要發送訊息咯...");template.convertAndSend("msg", "歡迎使用redis的訊息佇列!");try {//發送訊息串連等待中logger.info("訊息正在發送...");latch.await();} catch (InterruptedException e) {logger.info("訊息發送失敗...");}} }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.