【转】 使用Redis的Pub/Sub来实现类似于JMS的消息持久化
http://blog.csdn.net/canot/article/details/52040415
关于个人对Redis提供的Pub/Sub机制的认识在上一篇博客中涉及到了,也提到了关于如何避免Redis的Pub/Sub的一个最大的缺陷的思路—消息的持久化(http://blog.csdn.net/canot/article/details/51975566)。这篇文章主要是关于其思路(Redis的Pub/Sub的消息持久化)的代码实现:
Pub/Sub机制中最核心的Listener的实现:
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
import redis.clients.util.RedisInputStream;
public class PubSubListener extends JedisPubSub {
private String clientId;
private HandlerRedis handlerRedis;
// 生成PubSubListener的时候必须制定一个id
public PubSubListener(String cliendId, Jedis jedis) {
this.clientId = cliendId;
jedis.auth("xxxx");
handlerRedis = new HandlerRedis(jedis);
}
@Override
public void onMessage(String channel, String message) {
if ("quit".equals(message)) {
this.unsubscribe(channel);
}
handlerRedis.handler(channel, message);
}
// 真正处理接受的地方
private void message(String channel, String message) {
System.out.println("message receive:" + message + ",channel:" + channel + "...");
}
@Override
public void onPMessage(String pattern, String channel, String message) {
// TODO Auto-generated method stub
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {
// 将订阅者保存在一个"订阅活跃者集合中"
handlerRedis.subscribe(channel);
System.out.println("subscribe:" + channel);
}
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
handlerRedis.ubsubscribe(channel);
System.out.println("unsubscribe:" + channel);
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
// TODO Auto-generated method stub
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
// TODO Auto-generated method stub
}
class HandlerRedis {
private Jedis jedis;
public HandlerRedis(Jedis jedis) {
this.jedis = jedis;
}
public void handler(String channel, String message) {
int index = message.indexOf("/");
if (index < 0) {
// 消息不合法,丢弃
return;
}
Long txid = Long.valueOf(message.substring(0, index));
String key = clientId + "/" + channel;
while (true) {
String lm = jedis.lindex(key, 0);// 获取第一个消息
if (lm == null) {
break;
}
int li = lm.indexOf("/");
// 如果消息不合法,删除并处理
if (li < 0) {
String result = jedis.lpop(key);// 删除当前message
// 为空
if (result == null) {
break;
}
message(channel, lm);
continue;
}
Long lxid = Long.valueOf(lm.substring(0, li));// 获取消息的txid
// 直接消费txid之前的残留消息
if (txid >= lxid) {
jedis.lpop(key);// 删除当前message
message(channel, lm);
continue;
} else {
break;
}
}
}
// 持久化订阅操作
public void subscribe(String channel) {
// 保证在订阅者集合中的格式为 唯一标识符/订阅的通道
String key = clientId + "/" + channel;
// 判断该客户端是否在集合中存在
boolean isExist = jedis.sismember("PERSIS_SUB", key);
if (!isExist) {
// 不存在则添加
jedis.sadd("PERSIS_SUB", key);
}
}
public void ubsubscribe(String channel) {
String key = clientId + "/" + channel;
// 从“活跃订阅者”集合中
jedis.srem("PERSIS_SUB", key);
// 删除“订阅者消息队列”
jedis.del(channel);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
Listener中定义了一个内部类HandlerRedis。Listener类将onMessage以及onSubscribe两个方法交付于HandlerRedis。Handler处理这个方法的时候也即进行着队列的维护。Listener类中定义了一个message()方法,该方法是handler的回调方法,即真正的处理消息的地方。
通道的订阅客户端类:
import redis.clients.jedis.Jedis;
public class SubClient {
private Jedis jedis;
private PubSubListener listener;
public SubClient(String host,PubSubListener pubSubListener){
jedis = new Jedis(host);
jedis.auth("XXXXX");
this.listener = pubSubListener;
}
public void sub(String channel){
jedis.subscribe(listener, channel);
}
public void unsubscribe(String channel){
listener.unsubscribe(channel);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
通道的消息发布的客户端:
import java.util.Set;
import redis.clients.jedis.Jedis;
public class PubClient {
private Jedis jedis;
public PubClient(String host) {
jedis = new Jedis(host);
jedis.auth("wx950709");
}
/**
* 发布的每条消息,都需要在“订阅者消息队列”中持久
*
* @param message
*/
public void put(String message) {
//获取所有活跃的消息接收者客户端 clientID/channel
Set<String> subClients = jedis.smembers("PERSIS_SUB");
for (String subs : subClients) {
// 保存每个客户端的消息
jedis.rpush(subs, message);
}
}
public void publish(String channel, String message) {
// 每个消息,都有具有一个全局唯一的id
// txid为了防止订阅端在数据处理时“乱序”,这就要求订阅者需要解析message
Long txid = jedis.incr("MESSAGE_TXID");
String content = txid + "/" + message;
this.put(content);
jedis.publish(channel, content);//为每个消息设定id,最终消息格式1000/messageContent
}
public void close(String channel){
jedis.publish(channel, "quit");
jedis.del(channel);//删除
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
测试引导类:
import redis.clients.jedis.Jedis;
public class Main {
public static void main(String[] args) throws Exception{
PubClient pubClient = new PubClient("127.0.0.1");
final String channel = "pubsub-channel222";
PubSubListener listener = new PubSubListener("client_one", new Jedis("127.0.0.1"));
SubClient subClient = new SubClient("127.0.0.1", listener);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
//在API级别,此处为轮询操作,直到unsubscribe调用,才会返回
subClient.sub(channel);
}
});
t1.setDaemon(true);
t1.start();
int i = 0;
while(i < 2){
pubClient.publish(channel, "message"+i);
i++;
Thread.sleep(1000);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29

【转】 使用Redis的Pub/Sub来实现类似于JMS的消息持久化的更多相关文章
- redis的Pub/Sub
redis的Pub/Sub机制类似于广播架构,Subscriber相当于收音机,可以收听多个channel(频道),Publisher(电台)可以在channel中发布信息. 命令介绍 PUBLISH ...
- Redis的Pub/Sub机制存在的问题以及解决方案
Redis的Pub/Sub机制使用非常简单的方式实现了观察者模式,但是在使用过程中我们发现,它仅仅是实现了发布订阅机制,但是很多的场景没有考虑到.例如一下的几种场景: 1.数据可靠性无法保证 一个re ...
- Redis的Pub/Sub客户端实现
前言 在学习T-io框架,从写一个Redis客户端开始一文中,已经简单介绍了Redis客户端的实现思路,并且基础架构已经搭建完成,只不过支持的命令不全,不过后期在加命令就会很简单了.本篇就要实现P ...
- Redis实战——Redis的pub/Sub(订阅与发布)在java中的实现
借鉴:https://blog.csdn.net/canot/article/details/51938955 1.什么是pub/sub Pub/Sub功能(means Publish, Subscr ...
- 分布式缓存技术redis学习系列(三)——redis高级应用(主从、事务与锁、持久化)
上文<详细讲解redis数据结构(内存模型)以及常用命令>介绍了redis的数据类型以及常用命令,本文我们来学习下redis的一些高级特性. 安全性设置 设置客户端操作秘密 redis安装 ...
- 分布式缓存技术redis学习(三)——redis高级应用(主从、事务与锁、持久化)
上文<详细讲解redis数据结构(内存模型)以及常用命令>介绍了redis的数据类型以及常用命令,本文我们来学习下redis的一些高级特性.目录如下: 安全性设置 设置客户端操作秘密 客户 ...
- 分布式缓存技术redis系列(三)——redis高级应用(主从、事务与锁、持久化)
上文<详细讲解redis数据结构(内存模型)以及常用命令>介绍了redis的数据类型以及常用命令,本文我们来学习下redis的一些高级特性. 安全性设置 设置客户端操作秘密 redis安装 ...
- Linux07 /redis的配置、五大数据类型、发布订阅、持久化、主从复制、哨兵配置、集群搭建
Linux07 /redis的配置.五大数据类型.发布订阅.持久化.主从复制.哨兵配置.集群搭建 目录 Linux07 /redis的配置.五大数据类型.发布订阅.持久化.主从复制.哨兵配置.集群搭建 ...
- redis的pub/sub命令
Redis 发布订阅 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. 下图展示了频道 cha ...
随机推荐
- Android APK反编译详解(附图)(转)
这段时间在学Android应用开发,在想既然是用Java开发的应该很好反编译从而得到源代码吧,google了一下,确实很简单,以下是我的实践过程. 在此郑重声明,贴出来的目的不是为了去破解人家的软件, ...
- .net winform软件自动更新
转载自 http://dotnet.chinaitlab.com/DotNetFramework/914178.html 关于.NET windows软件实现自动更新,本人今天写了一个DEMO,供大家 ...
- [hive小技巧]同一份数据多种处理
其实就是from表时,可以插入到多个表. sql语句的模板如下: from history insert overwrite sales select * where actino='purchase ...
- android native开发时:java.lang.UnsatisfiedLinkError: Native method not found的处理
这个异常一般是由于JNI的链接器不能正常识别C++的函数名造成的.处理的方法是用exern "C" {},来包裹需要export的C++的native方法. 如果native的方法 ...
- js:数据结构笔记1---数组
JS中数组: 只是一种特殊的对象,比其他语言中效率低: 属性是用来表示偏移量的索引:在JS中,数字索引在内部被转化为字符串类型(这也是为什么写对象属性的时候可以不叫引号),因为对象中的属性必须是字符串 ...
- javascript操作cookies
1.读取cookies getCookie: function(c_name){ if (document.cookie.length > 0) { var c_start = document ...
- Eval有什么功能?
它的功能是把对应的字符串解析成JS代码并运行.应该尽量避免使用eval,因为不安全,非常耗性能.解析成JS代码要耗能,执行时也要耗能.
- Revit二次开发示例:ModelessForm_ExternalEvent
使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...
- ural 1283. Dwarf
1283. Dwarf Time limit: 1.0 secondMemory limit: 64 MB Venus dwarfs are rather unpleasant creatures: ...
- BZOJ3836 : [Poi2014]Tourism
对于一个连通块,取一个点进行dfs,得到一棵dfs搜索树,则这棵树的深度不超过10,且所有额外边都是前向边. 对于每个点x,设S为三进制状态,S第i位表示根到x路径上深度为i的点的状态: 0:选了 1 ...