环境依赖

创建一个新的springboot工程,在其pom文件,加入spring-boot-starter-data-redis依赖:

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

创建一个消息接收者

REcevier类,它是一个普通的类,需要注入到springboot中。

public class Receiver {
private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class); private CountDownLatch latch; @Autowired
public Receiver(CountDownLatch latch) {
this.latch = latch;
} public void receiveMessage(String message) {
LOGGER.info("Received <" + message + ">");
latch.countDown();
}
}

  

注入消息接收者

@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);
}

  

注入消息监听容器

在spring data redis中,利用redis发送一条消息和接受一条消息,需要三样东西:

  • 一个连接工厂
  • 一个消息监听容器
  • Redis template

上述1、3步已经完成,所以只需注入消息监听容器即可:

@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("chat")); return container;
} @Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}

  

测试

在springboot入口的main方法:

public static void main(String[] args) throws Exception{
ApplicationContext ctx = SpringApplication.run(SpringbootRedisApplication.class, args); StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
CountDownLatch latch = ctx.getBean(CountDownLatch.class); LOGGER.info("Sending message...");
template.convertAndSend("chat", "Hello from Redis!"); latch.await(); System.exit(0);
}

  

先用redisTemplate发送一条消息,接收者接收到后,打印出来。启动springboot程序,控制台打印:

2017-04-20 17:25:15.536 INFO 39148 — [ main] com.forezp.SpringbootRedisApplication : Sending message…
2017-04-20 17:25:15.544 INFO 39148 — [ container-2] com.forezp.message.Receiver : 》Received

  

源码来源

最新文章

  1. 下载aptana插件jar包
  2. Linux/Mac/Shell常用命令
  3. 技海拾贝 - Java
  4. 转 nutch网页快照乱码解决方法
  5. 【WP开发】正确理解页面缓存
  6. 【转】面向对象设计的SOLID原则
  7. “奥特曼攻打小怪兽”java学习打怪升级第一步
  8. CSS中!important的优先级
  9. bower入门
  10. C Primer Plus学习笔记
  11. hdu 1257 最少拦截系统(简单贪心)
  12. 基于web的项目管理软件Redmine
  13. 会话数据的保存——cookie
  14. HDU4003 Find Metal Mineral
  15. Ubuntu Server 安装部署 Cacti 服务器监控
  16. HDU 1017 - A Mathematical Curiosity
  17. 从数据库中,绑定JQuery Accordion控件---Repeater control
  18. 懒人小工具:T4生成实体类Model,Insert,Select,Delete以及导出Excel的方法
  19. 【Nginx】均衡负载权重模式实现session数据同步
  20. js Array数组对象常见方法总结

热门文章

  1. 好问题:count(1)、count(*)、count(列)有什么区别?
  2. win7启动tomcat失败处理
  3. 安装VUE教程
  4. Jade学习(四)之结合node如何编译执行
  5. vue 中 @click.native.prevent 事件
  6. iOS 支付 [支付宝、银联、微信]
  7. Ubuntu18.04 安装redis
  8. 三、Ubuntu16.04 安装Jira8.2.2(自带中文包)和破解
  9. linux基础命令--lsof
  10. collections queue、os、datetime,序列化(json和pickle)模块