Skip to content

Commit

Permalink
update: nutz/nutzboot 版本 & redis keys使用scan全部替换
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizzercn committed Oct 16, 2020
1 parent 04ede98 commit 4ed35b0
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 50 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<name>NutzWk-V5-Zookeeper</name>

<properties>
<nutzboot.version>2.4.0.v20200427</nutzboot.version>
<nutz.version>1.r.68.v20200309</nutz.version>
<nutzboot.version>2.4.1.v20201014</nutzboot.version>
<nutz.version>1.r.68.v20201011</nutz.version>
<zkclient.version>0.11</zkclient.version>
<mysql-connector-java.version>5.1.48</mysql-connector-java.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -54,16 +56,24 @@ public void notify(Sys_msg innerMsg, String rooms[]) {
map.put("url", url);
String msg = Json.toJson(map, JsonFormat.compact());
if ("system".equals(innerMsg.getType())) {//系统消息发送给所有在线用户
Set<String> keys = redisService.keys("wsroom:*");
for (String room : keys) {
pubSubService.fire(room, msg);
}
ScanParams match = new ScanParams().match("wsroom:*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String room : scan.getResult()) {
pubSubService.fire(room, msg);
}
} while (!scan.isCompleteIteration());
} else if ("user".equals(innerMsg.getType())) {//用户消息发送给指定在线用户
for (String room : rooms) {
Set<String> keys = redisService.keys("wsroom:" + room + ":*");
for (String key : keys) {
pubSubService.fire(key, msg);
}
ScanParams match = new ScanParams().match("wsroom:" + room + ":*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String key : scan.getResult()) {
pubSubService.fire(key, msg);
}
} while (!scan.isCompleteIteration());
}
}
}
Expand All @@ -76,10 +86,14 @@ public void innerMsg(String room, int size, List<NutMap> list) {
map.put("list", list);//最新3条消息列表 type--系统消息/用户消息 title--标题 time--时间戳
String msg = Json.toJson(map, JsonFormat.compact());
log.debug("msg::::" + msg);
Set<String> keys = redisService.keys("wsroom:" + room + ":*");
for (String key : keys) {
pubSubService.fire(key, msg);
}
ScanParams match = new ScanParams().match("wsroom:" + room + ":*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String key : scan.getResult()) {
pubSubService.fire(key, msg);
}
} while (!scan.isCompleteIteration());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import org.nutz.plugins.mvc.websocket.WsHandler;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import javax.websocket.EndpointConfig;
import javax.websocket.Session;
Expand All @@ -36,16 +38,20 @@ public void init() {
roomPrefix = "wsroom:";
roomProvider = new WkJedisRoomProvider(jedisAgent);
try (Jedis jedis = jedisAgent.getResource()) {
for (String key : jedis.keys(roomPrefix + "*")) {
switch (jedis.type(key)) {
case "none":
break;
case "set":
break;
// default:
// jedis.del(key);
ScanParams match = new ScanParams().match(roomPrefix + "*");
ScanResult<String> scan = null;
do {
scan = jedis.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String key : scan.getResult()) {
switch (jedis.type(key)) {
case "none":
case "set":
break;
default:
jedis.del(key);
}
}
}
} while (!scan.isCompleteIteration());
}
pubSubService.reg(roomPrefix + "*", this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -54,16 +56,24 @@ public void notify(Sys_msg innerMsg, String rooms[]) {
map.put("url", url);
String msg = Json.toJson(map, JsonFormat.compact());
if ("system".equals(innerMsg.getType())) {//系统消息发送给所有在线用户
Set<String> keys = redisService.keys("wsroom:*");
for (String room : keys) {
pubSubService.fire(room, msg);
}
ScanParams match = new ScanParams().match("wsroom:*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String room : scan.getResult()) {
pubSubService.fire(room, msg);
}
} while (!scan.isCompleteIteration());
} else if ("user".equals(innerMsg.getType())) {//用户消息发送给指定在线用户
for (String room : rooms) {
Set<String> keys = redisService.keys("wsroom:" + room + ":*");
for (String key : keys) {
pubSubService.fire(key, msg);
}
ScanParams match = new ScanParams().match("wsroom:" + room + ":*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String key : scan.getResult()) {
pubSubService.fire(key, msg);
}
} while (!scan.isCompleteIteration());
}
}
}
Expand All @@ -76,10 +86,14 @@ public void innerMsg(String room, int size, List<NutMap> list) {
map.put("list", list);//最新3条消息列表 type--系统消息/用户消息 title--标题 time--时间戳
String msg = Json.toJson(map, JsonFormat.compact());
log.debug("msg::::" + msg);
Set<String> keys = redisService.keys("wsroom:" + room + ":*");
for (String key : keys) {
pubSubService.fire(key, msg);
}
ScanParams match = new ScanParams().match("wsroom:" + room + ":*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String key : scan.getResult()) {
pubSubService.fire(key, msg);
}
} while (!scan.isCompleteIteration());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import org.nutz.plugins.mvc.websocket.WsHandler;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import javax.websocket.EndpointConfig;
import javax.websocket.Session;
Expand All @@ -36,16 +38,20 @@ public void init() {
roomPrefix = "wsroom:";
roomProvider = new WkJedisRoomProvider(jedisAgent);
try (Jedis jedis = jedisAgent.getResource()) {
for (String key : jedis.keys(roomPrefix + "*")) {
switch (jedis.type(key)) {
case "none":
break;
case "set":
break;
// default:
// jedis.del(key);
ScanParams match = new ScanParams().match(roomPrefix + "*");
ScanResult<String> scan = null;
do {
scan = jedis.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String key : scan.getResult()) {
switch (jedis.type(key)) {
case "none":
case "set":
break;
default:
jedis.del(key);
}
}
}
} while (!scan.isCompleteIteration());
}
pubSubService.reg(roomPrefix + "*", this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -97,9 +99,13 @@ public Object data(@Param("hostName") String hostName) {
@SuppressWarnings("unchecked")
public Object osData(@Param("hostName") String hostName) {
try {
List<NutMap> hostList = new ArrayList<>();
Set<String> set = redisService.keys("logback:deploy:" + hostName + ":*");
List<String> list = new ArrayList<>(set);
List<String> list = new ArrayList<>();
ScanParams match = new ScanParams().match("logback:deploy:" + hostName + ":*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
list.addAll(scan.getResult());//增量式迭代查询,可能还有下个循环,应该是追加
} while (!scan.isCompleteIteration());
Collections.sort(list);
List<NutMap> dataList = new ArrayList<>();
for (String key : list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,14 +40,18 @@ public void init() {

@Test
public void test_service_inject() {
Set<String> set = redisService.keys("logback:loglevel:list:*");
NutMap map = NutMap.NEW();
for (String key : set) {
NutMap map = NutMap.NEW();
ScanParams match = new ScanParams().match("logback:loglevel:list:*");
ScanResult<String> scan = null;
do {
scan = redisService.scan(scan == null ? ScanParams.SCAN_POINTER_START : scan.getStringCursor(), match);
for (String key : scan.getResult()) {
String[] keys = key.split(":");
String name = keys[3];
LoglevelProperty loglevelProperty = Json.fromJson(LoglevelProperty.class, redisService.get(key));
map.addv2(name, loglevelProperty);
}
} while (!scan.isCompleteIteration());
System.out.println(Json.toJson(map));
}

Expand Down

0 comments on commit 4ed35b0

Please sign in to comment.