Redis的消息订阅/发布 Utils工具类
package cn.cicoding.utils; import org.json.JSONException;
import org.json.JSONObject; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisConnectionException; class MQClient {
public static final int MSG_REALTIME = 1;
public static final int MSG_CACHED = 2;
public static final int MSG_SERIALIZABLE = 3;
public static final String NOTIFY_CHANNEL = "ClientNotify"; private JedisPool pool;
private boolean exit;
private JedisPubSub pubsub; public MQClient(String ip, int port, JedisPubSub pubsub) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxWaitMillis(10 * 1000);
config.setMaxIdle(1000);
config.setTestOnBorrow(true);
pool = new JedisPool(config, ip, port, Protocol.DEFAULT_TIMEOUT, null);
exit = false;
this.pubsub = pubsub;
} public boolean publish(String channels, String message, String content) {
JSONObject obj = new JSONObject();
boolean ret = false;
Jedis jedis = null;
try {
jedis = pool.getResource();
if (message != null) {
obj.put("message", message);
}
try {
JSONObject objCon = new JSONObject(content);
obj.put("content", objCon);
} catch (JSONException e) {
obj.put("content", content);
}
String[] tmp = channels.split(";");
for (String channel : tmp) {
try {
if (jedis.publish(channel, obj.toString()) > 0) {
ret = true;
}
} catch (Exception e) {
break;
}
}
} catch (JSONException e) {
} finally {
if (jedis != null){
jedis.close();
}
} return ret;
} public boolean clientNotify(String clients, String message, String content, int type) {
if (type == MSG_REALTIME) {
return publish(clients, message, content);
} boolean ret = false; try {
JSONObject obj = new JSONObject();
obj.put("clients", clients);
obj.put("type", type);
if (message != null) {
obj.put("message", message);
}
try {
JSONObject objCon = new JSONObject(content);
obj.put("content", objCon);
} catch (JSONException e) {
obj.put("content", content);
} if (pool.getResource().publish(NOTIFY_CHANNEL, obj.toString()) > 0) {
ret = true;
}
} catch (JSONException e) {
} return ret;
} public boolean setValue(String key, String value) {
try {
String response = pool.getResource().set(key, value);
if (response != null && response.equals("OK")) {
return true;
}
} catch (JedisConnectionException e) {
e.printStackTrace();
} return false;
} public String getValue(String key) {
return pool.getResource().get(key);
} public void subscribe(String... channels) {
while (!exit) {
try {
pool.getResource().subscribe(pubsub, channels);
} catch (JedisConnectionException e) {
e.printStackTrace();
System.out.println("try reconnect");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}
Test测试函数:
package cn.cicoding.utils; import redis.clients.jedis.JedisPubSub; public class Test extends JedisPubSub {
@Override
public void onMessage(String channel, String message) {
System.out.println(channel + ":" + message);
} public static void main(String[] args) {
MQClient client = new MQClient("127.0.0.1", 6379, new Test());
client.setValue("abc", "java setted");
System.out.println(client.getValue("abc"));
System.out.println(client.clientNotify("nodeSubscriber", "message from java", "{\"debug\":0}", MQClient.MSG_REALTIME));
client.subscribe("testInitiativePerception");
}
}
Redis的消息订阅/发布 Utils工具类的更多相关文章
- 基于Redis的消息订阅/发布
在工业生产设计中,我们往往需要实现一个基于消息订阅的模式,用来对非定时的的消息进行监听订阅. 这种设计模式在 总线设计模式中得到体现.微软以前的WCF中实现了服务总线 ServiceBus的设计模式. ...
- [SpingBoot guides系列翻译]Redis的消息订阅发布
Redis的消息 部分参考链接 原文 CountDownLatch 概述 目的 这节讲的是用Redis来实现消息的发布和订阅,这里会使用Spring Data Redis来完成. 这里会用到两个东西, ...
- SpringBoot+Redis 实现消息订阅发布
什么是 Redis Redis 是一个开源的使用 ANSI C语言编写的内存数据库,它以 key-value 键值对的形式存储数据,高性能,读取速度快,也提供了持久化存储机制. Redis 通常在项目 ...
- redis实现消息队列&发布/订阅模式使用
在项目中用到了redis作为缓存,再学习了ActiveMq之后想着用redis实现简单的消息队列,下面做记录. Redis的列表类型键可以用来实现队列,并且支持阻塞式读取,可以很容易的实现一个高性 ...
- 基于redis的消息订阅与发布
Redis 的 SUBSCRIBE 命令可以让客户端订阅任意数量的频道, 每当有新信息发送到被订阅的频道时, 信息就会被发送给所有订阅指定频道的客户端. 作为例子, 下图展示了频道 channel1 ...
- Redis之Redis消息订阅发布简介
概念: Redis消息订阅发布是进程间的一种消息通信模式,发送者pub发送消息,订阅者sub接收消息. 使用须知: 需要先订阅后发布,才能接收到消息.在订阅时,相当于创建了可供发布的频道. 案例: ( ...
- Redis实现消息的发布/订阅
利用spring-boot结合redis进行消息的发布与订阅: 发布: class Publish { private static String topicName = “Topic:chat”; ...
- Redis的消息订阅及发布及事务机制
Redis的消息订阅及发布及事务机制 订阅发布 SUBSCRIBE PUBLISH 订阅消息队列及发布消息. # 首先要打开redis-cli shell窗口 一个用于消息发布 一个用于消息订阅 # ...
- spring boot: 用redis的消息订阅功能更新应用内的caffeine本地缓存(spring boot 2.3.2)
一,为什么要更新caffeine缓存? 1,caffeine缓存的优点和缺点 生产环境中,caffeine缓存是我们在应用中使用的本地缓存, 它的优势在于存在于应用内,访问速度最快,通常都不到1ms就 ...
随机推荐
- [转帖]Oracle dba_objects和all_objects 最大的区别
Oracle dba_objects和all_objects 最大的区别 原创 Oracle 作者:maohaiqing0304 时间:2015-08-14 15:07:18 9281 0 链 ...
- 【监控笔记】【1.2】监控事件系列——SQL Trace(默认跟踪与自定义跟踪)
目录: [1.1]概念与使用 [1.2]跟踪的基本操作 --[1.2.1]查看默认跟踪是否在运行 --[1.2.2]开启默认跟踪 --[1.2.3]关闭默认跟踪 --[1.2.4]查看跟踪文件/查看跟 ...
- HTML第一课(前期知识准备)
在正式的学习之前,我们先了解一些前端方面的常识. 一.前端是做什么? 如果有认真看过我写的预备程序员不得不知道的事儿这篇文章的同学应该清楚,前端的工作在整个项目开发中处于代码编写阶段,主要是用来做界面 ...
- 42. Trapping Rain Water (JAVA)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- raft协议-分布式环境下的数据一致性问题
阅读了一个有意思的ppt,是Standford大学发表的raft协议 网址:http://thesecretlivesofdata.com/raft/ 下面自己总结下咯: 1.raft是一个实现了解决 ...
- tp5 模板参数配置(模板静态文件路径)
tp5 模板参数配置(模板静态文件路径) // 模板页面使用 <link rel="stylesheet" type="text/css" href=&q ...
- AtCoder Regular Contest 092 Two Sequences AtCoder - 3943 (二进制+二分)
Problem Statement You are given two integer sequences, each of length N: a1,…,aN and b1,…,bN. There ...
- Qt带参数的信号和槽
在Qt的开发过程中,信号带参数是很常见的,在使用带参数的信号槽时,有以下几点需要注意. 当信号和槽函数的参数数量相同时,它们的参数类型要完全一致. 信号和槽函数的声明: signals: void i ...
- Eclipse开发工具的编码问题
乱码:文件有一个编码,打开文件的工具(Eclipse或者浏览器)有一个编码,当两个编码不同就会出现编码异常或乱码. 参考: Eclipse修改编码格式 背景:在Eclipse的开发使用中,我们经常使用 ...
- WPF导出发布安装包,无法验证发行者解决办法
右击工程项目点发布->完成 此时在工程目录下生成了安装文件setup.exe 同时又在工程下生成了临时证书WpfApp1_TemporaryKey.pfx 如果此时强行安装会弹出如下警告 接下来 ...