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就 ...
随机推荐
- servlet3.0文件上传与下载
描述:文件上传与下载是在JavaEE中常见的功能,实现文件上传与下载的方式有多种,其中文件上传的方式有: (1)commons-fileupload: (2)Servlet 3.0 实现文件上传 (3 ...
- Kotlin学习(5)类型系统
可空性(避免空指针异常) /* *这个函数的参数代表传入一个String类型变量的实例,这代表它不可以为空 */ fun a(str:String){ println(str) } //这样调用a() ...
- 通过编写串口助手工具学习MFC过程——(二)通过“打开串口”按钮了解基本操作
通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...
- linux的管道 |和grep命令以及一些其他命令(diff,echo,cat,date,time,wc,which,whereis,gzip,zcat,unzip,sort)
linux提供管道符号“|”,作用是命令1的输出内容作为命令2的输入内容.通常与grep命令一起使用. 格式:命令1 |命令2 grep命令:全称为global regular expression ...
- ukhj
SQL解析顺序: 七种Join图:
- LabWindows/CVI 下载
LabWindows/CVI 是National Instruments 公司(美国国家仪器公司,简称NI 公司)推出的交互式C 语言开发平台.LabWindows/CVI 将功能强大.使用灵活的C ...
- nroff - 用 groff 模拟 nroff 命令
总览 (SYNOPSIS) nroff [ -h ] [ -i ] [ -mname ] [ -nnum ] [ -olist ] [ -rcn ] [ -Tname ] [ file... ] 描述 ...
- .net Core——SqlSugar使用
一.DbContext配置 public class DbContext { public DbContext() { Db = new SqlSugarClient(new ConnectionCo ...
- error: undefined reference to `cv::imread(std::string const&, int)'
g++ main.cpp -o main `pkg-config --libs --cflags opencv`注意--libs的位置https://stackoverflow.com/questio ...
- 基于iview使用jsx扩展成可编辑的表格
<template> <div> <Table :columns="columns" :data="data"></T ...