Spring RedisTemplate操作-发布订阅操作(8)
@Component("sub")
public class Sub implements MessageListener{
@Autowired
private StringRedisSerializer stringRedisSerializer;
/* (非 Javadoc)
* Description:
* @see org.springframework.data.redis.connection.MessageListener#onMessage(org.springframework.data.redis.connection.Message, byte[])
*/
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] body = message.getBody();//请使用valueSerializer
byte[] channel = message.getChannel();
String msg = (String)stringRedisSerializer.deserialize(body);
String topic = (String)stringRedisSerializer.deserialize(channel);
System.out.println("我是sub,监听"+topic+",我收到消息:"+msg);
}
}
@Component("sub2")
public class Sub2 implements MessageListener{
@Autowired
private StringRedisSerializer stringRedisSerializer;
@Autowired
private JdkSerializationRedisSerializer jdkSerializationRedisSerializer;
/* (非 Javadoc)
* Description:
* @see org.springframework.data.redis.connection.MessageListener#onMessage(org.springframework.data.redis.connection.Message, byte[])
*/
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] body = message.getBody();//请使用valueSerializer
byte[] channel = message.getChannel();
String msg = (String)stringRedisSerializer.deserialize(body);
String topic = (String)stringRedisSerializer.deserialize(channel);
System.out.println("我是sub2,监听"+topic+",我收到消息:"+msg);
}
}
@Component("sub3")
public class Sub3 implements MessageListener{
@Autowired
private GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer;
@Autowired
private StringRedisSerializer stringRedisSerializer;
@Autowired
private JdkSerializationRedisSerializer jdkSerializationRedisSerializer;
/* (非 Javadoc)
* Description:
* @see org.springframework.data.redis.connection.MessageListener#onMessage(org.springframework.data.redis.connection.Message, byte[])
*/
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] body = message.getBody();//请使用valueSerializer
byte[] channel = message.getChannel();
User u = jackson2JsonRedisSerializer.deserialize(body,User.class);
String topic = (String)stringRedisSerializer.deserialize(channel);
System.out.println("我是sub3,监听"+topic+",我收到消息:"+u.getId()+"--"+u.getName());
}
}
@Service
public class Pub { @Autowired
@Resource(name="redisTemplate")
private RedisTemplate<String, String> rt; @Autowired
private GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer; @Autowired
private JdkSerializationRedisSerializer jdkSerializationRedisSerializer; public void sendMessage(String channel, String message) {
rt.convertAndSend(channel, message);
}
public void sendMessage(String channel, User user) {
byte[] msg =jackson2JsonRedisSerializer.serialize(user);
rt.convertAndSend(channel, new String(msg)); // rt.convertAndSend(channel, user);
} }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:conf/spring/spring*.xml")
public class PubSubTest { @Autowired
private Pub pub; @Test
public void test() throws InterruptedException{
User u = new User();
u.setId("1");
u.setName("wzg");
pub.sendMessage("dddchannel", "我发消息了");
pub.sendMessage("cccchannel", u);
Thread.sleep(100);//jackson 反向序列化慢 } }
Spring RedisTemplate操作-发布订阅操作(8)的更多相关文章
- [Redis-CentOS7]Redis发布订阅操作(七)
发布订阅 发布:打电话 订阅:接电话 订阅频道 127.0.0.1:6379> SUBSCRIBE msg Reading messages... (press Ctrl-C to quit) ...
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
- RedisRepository封装—Redis发布订阅以及StackExchange.Redis中的使用
本文版权归博客园和作者本人吴双共同所有,转载请注明本Redis系列分享地址.http://www.cnblogs.com/tdws/tag/NoSql/ Redis Pub/Sub模式 基本介绍 Re ...
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- redis(3)发布订阅
一.发布/订阅模式 在软件工程里面,发布/订阅是一种消息模式,这种模式旨在将消息发送者和消息接收者解耦.发送者不需要关心将消息发送给谁,接收者也不需要知道消息的发送者是谁.发送者将消息发布以后就结束动 ...
- sqlserver关于发布订阅replication_subscription的总结
(转载)sqlserver关于发布订阅replication_subscription的总结 来自 “ ITPUB博客 ” ,原文地址:http://blog.itpub.net/30126024/v ...
- Redis 发布订阅,小功能大用处,真没那么废材!
今天小黑哥来跟大家介绍一下 Redis 发布/订阅功能. 也许有的小伙伴对这个功能比较陌生,不太清楚这个功能是干什么的,没关系小黑哥先来举个例子. 假设我们有这么一个业务场景,在网站下单支付以后,需要 ...
- Spring RedisTemplate操作-xml配置(1)
网上没能找到全的spring redistemplate操作例子,故特意化了点时间做了接口调用练习,基本包含了所有redistemplate方法. 该操作例子是个系列,该片为spring xml配置, ...
- 第三百零一节,python操作redis缓存-管道、发布订阅
python操作redis缓存-管道.发布订阅 一.管道 redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pi ...
随机推荐
- win10引导错误的修复(内容系转载)
#!尊重原作者,再此声明此内容属于网络转载,只是为了能保留下来方便日后查阅!!! win10误删引导文件,0xc0000098的解决方案,bcd引导文件受损情况分析 一.※相对简单的解决方法,对应的情 ...
- LeetCode 551. Student Attendance Record I (C++)
题目: You are given a string representing an attendance record for a student. The record only contains ...
- #Linux第四周学习总结——扒开系统调用的三层皮(上)
Linux第四周学习总结--扒开系统调用的三层皮(上) 一.用户态.内核态和中断 系统调用通过库函数. 1.用户态和内核态 区分(不同的指令执行级别): 用户态:在相应的低执行状态下,代码的掌控范围受 ...
- 使用Visual Studio 2013进行单元测试的过程与感想
首先是安装Visual Studio 2013这个软件,尽管安装过程不复杂,但是安装的时间实在是太长了,经过2个多小时的安装终于装完了. 由于时间紧凑,没来得及装语言包,于是,我用了原装的进行了单元测 ...
- ElasticSearch5.6.1 + 中文分词(IK)
在安装ElasticSearch 需要先安装java 的 jdk 可以通过 命令来确认是否已经安装了 java -version 如果没有安装可以使用 yum 直接安装通过. 先通过命令查看yum源 ...
- ElasticSearch 2 (19) - 语言处理系列之故事开始
ElasticSearch 2 (19) - 语言处理系列之故事开始 摘要 全文搜索是精度(尽可能少的返回不相关文档)和召回(尽可能多的返回相关文档)的战场.尽管只精确匹配用户查询的词肯定会是精确的, ...
- nodejs的事件驱动理解
// 引入 events 模块 var events = require('events'); // 创建 eventEmitter 对象 var eventEmitter = new events. ...
- CentOS 简单学习 firewalld的使用
1. centos7 开始 使用firewalld 代替了 iptables 命令工具为 firewall-cmd 帮助信息非常长,简单放到文末 2. 简单使用 首先开启 httpd 一般都自带安装了 ...
- 微信 小程序组件 加入购物车全套 one wxml
<!--pages/shop/shop.wxml--> <view wx:if="{{hasList}}"> <view class="co ...
- final 发布 领跑衫获奖感想
时间 :2016年5月20日 13:30--14:00 final发布中,我们团队严一格演示了最终版本的四则运算项目, 修正了beta版本发布时弹幕部分无法消除的bug,总体效果达到预期. 遗憾的是没 ...