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 65dc646 commit 27883e1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

<properties>
<nutzwk.version>5.2.6</nutzwk.version>
<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>
<mysql-connector-java.version>5.1.48</mysql-connector-java.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<slf4j.version>1.7.25</slf4j.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 @@ -51,16 +53,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 @@ -73,10 +83,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 @@ -30,6 +30,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 @@ -93,9 +95,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
12 changes: 9 additions & 3 deletions src/test/java/cn/wizzer/test/WebTestDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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.Set;

Expand All @@ -34,14 +36,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 27883e1

Please sign in to comment.