项目下载: Demo下载
本小节将教你如何通过Spring Data Redis模块来在Redis上发布、接收消息。
你会建立一个利用StringRedisTemplate来发布消息且利用MessageListenerAdapter来订阅消息的应用。
也许使用Spring Data Redis来发布消息听起来很陌生,但是你会发现,Redis和消息系统有个共同点:noSql。
1、建立redis数据库。
window下的Redis下载可以参照我的随笔
《WINDOW版本Redis下载》
2、新建一个Redis消息接收者。
在大多数消息基础应用中,都包含消息发布者和消息接收者。
我们建立、实现一个接收者类来应答消息。
1 package cn.tiny77.guide07; 2 3 import java.util.concurrent.CountDownLatch; 4 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.beans.factory.annotation.Autowired; 8 9 public class Receiver { 10 private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class); 11 12 private CountDownLatch latch; 13 14 @Autowired 15 public Receiver(CountDownLatch latch) { 16 this.latch = latch; 17 } 18 19 public void receiveMessage(String message) { 20 LOGGER.info("Received <" + message + ">"); 21 latch.countDown(); 22 } 23 }
消息的消费者仅仅是一个很正常的类而已。接下来你会发现当你注册消息的接收者的时候,应答消息的方法名是可以自定义的。
3、注册监听者并发布消息
Spring Data Redis提供你需要通过Redis发送和接收消息的所有模块,你只需要配置:
一个连接Redis的工厂
一个消息监听容器
一个Redis模板
你可以通过Redis模板来发送信息,同时你会通过消息监听容器注册一个接收者使得它可以接收消息。连接Redis的工厂会帮助Redis消息模板和消息监听者连接到Redis服务。
本例使用Spring Boot 默认的连接工厂RedisConnectionFactory 而不是JedisConnectionFactory ,该工厂是建立于Jedis Redis library的基础上的。
工厂同时入Redis模板和消息监听容器中。
1 package cn.tiny77.guide07; 2 3 import java.util.concurrent.CountDownLatch; 4 5 import org.slf4j.Logger; 6 import org.slf4j.LoggerFactory; 7 import org.springframework.boot.SpringApplication; 8 import org.springframework.boot.autoconfigure.SpringBootApplication; 9 import org.springframework.context.ApplicationContext; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.data.redis.connection.RedisConnectionFactory; 12 import org.springframework.data.redis.core.StringRedisTemplate; 13 import org.springframework.data.redis.listener.PatternTopic; 14 import org.springframework.data.redis.listener.RedisMessageListenerContainer; 15 import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; 16 17 @SpringBootApplication 18 public class App { 19 20 private static final Logger LOGGER = LoggerFactory.getLogger(App.class); 21 22 @Bean 23 RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, 24 MessageListenerAdapter listenerAdapter) { 25 26 RedisMessageListenerContainer container = new RedisMessageListenerContainer(); 27 container.setConnectionFactory(connectionFactory); 28 container.addMessageListener(listenerAdapter, new PatternTopic("chat")); 29 30 return container; 31 } 32 33 @Bean 34 MessageListenerAdapter listenerAdapter(Receiver receiver) { 35 return new MessageListenerAdapter(receiver, "receiveMessage"); 36 } 37 38 @Bean 39 Receiver receiver(CountDownLatch latch) { 40 return new Receiver(latch); 41 } 42 43 @Bean 44 CountDownLatch latch() { 45 return new CountDownLatch(1); 46 } 47 48 @Bean 49 StringRedisTemplate template(RedisConnectionFactory connectionFactory) { 50 return new StringRedisTemplate(connectionFactory); 51 } 52 53 public static void main(String[] args) throws InterruptedException { 54 55 ApplicationContext ctx = SpringApplication.run(App.class, args); 56 57 StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class); 58 CountDownLatch latch = ctx.getBean(CountDownLatch.class); 59 60 LOGGER.info("Sending message..."); 61 template.convertAndSend("chat", "Hello from Redis!"); 62 63 latch.await(); 64 System.exit(0); 65 } 66 }
方法listenerAdapter定义的Bean会被消息容器所注册,而消息容器又在方法container中定义并监听"chat"主题的消息。
因为Receiver 是一个POJO类,因此它需要被打包进一个继承与MessageListener接口的消息适配器以便被方法addMessageListener调用。
同事消息是配置器的参数二配置 了当消息到达时需要调用消息接收者的receiveMessage方法。
你监听消息时候需要Redis连接工厂和消息监听容器,发送消息,你则需要一个Redis模板。
在这里,Redis模板是一个StringRedisTemplate对象,该类继承于RedisTemplate,它仅仅专注与操作Redis的字符串操作(key 和 value 都是字符串的操作)。
在main函数创建application context的时候注入了所有东西。然后application context启动消息监听容器使它开始监听消息。
接着main函数从application context获取StringRedisTemplate句柄并用它发送一个消息到chat主题。
最后,关闭应用程序。