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就 ...
随机推荐
- 循环Gray码的生成(递归)
#!/usr/bin/env python #coding:utf-8 import sys def gray_code(num, array): if num < 1: return if n ...
- 悼念512汶川大地震遇难同胞——选拔志愿者 HDU 2188 博弈论 巴什博奕
悼念512汶川大地震遇难同胞--选拔志愿者 HDU 2188 博弈论 巴什博奕 题意 对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每个省市都派出了大量的救援人员,这其中包括抢险救灾的武警部队 ...
- 卸载yum-mysql
注意事项:1. 卸载yum MYSQLsystemctl status mysqlsystemctl stop mysqlsystemctl disable mysqld rpm -qa | grep ...
- Django @csrf_exempt不适用于基于通用视图的类(Django @csrf_exempt does not work on generic view based class)
class ChromeLoginView(View): def get(self, request): return JsonResponse({'status': request.user.is_ ...
- css3实现颤动的动画
需求 页面要做一个活动入口,不能太显眼,但是又要用户能一眼就看出来. 演示 https://jsfiddle.net/vtsxc18q/ 实现 (部分动画代码) @keyframes chanDong ...
- 第三方模块:gulp模块
一.Gulp的使用 1. 使用npm install gulp 下载gulp库文件 2. 在项目根目录下简历gulpfile.js文件 3. 重构项目的文件夹架构src目录放置源代码文件,dist ...
- 虚拟机的网卡基本配置和基本linux命令
1.切换到/etc/sysconfig/network-script目录 cd /etc/sysconfig/network-scripts 2.将ifcfg-eth0备份成ifcfg-eth0. c ...
- Apache 配置IP站点
配置临时生效 IP: [root@Nagios-Server extra]# ifconfigeth0:0 192.168.1.126/24 up [root@Nagios-Server extra] ...
- JavaEE高级-SpringMVC学习笔记
*SpringMVC概述 - Spring为展示层提供的基于MVC设计理念的优秀Web框架,是目前最主流的MVC框架之一 - Spring3.0后全面超越Struts2,成为最优秀的MVC框架 - S ...
- NNIE(待尝试)
马克 https://blog.csdn.net/ywcpig/article/details/85260752 https://blog.csdn.net/u011728480/article/de ...