From 8f2ce95165f90e51d41b083610d00ae346f3a852 Mon Sep 17 00:00:00 2001 From: kunlong-luo Date: Thu, 2 Feb 2023 14:36:19 +0800 Subject: [PATCH 1/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=A9=BA=E6=8C=87?= =?UTF-8?q?=E9=92=88=E5=92=8C=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/service/impl/MediaServerServiceImpl.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java index 64105a63..c005e640 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java @@ -228,7 +228,11 @@ public class MediaServerServiceImpl implements IMediaServerService { String onlineKey = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); for (Object mediaServerKey : mediaServerKeys) { String key = (String) mediaServerKey; - MediaServerItem mediaServerItem = (MediaServerItem) RedisUtil.get(key); + JSONObject jsonObject = (JSONObject) RedisUtil.get(key); + if (Objects.isNull(jsonObject)) { + continue; + } + MediaServerItem mediaServerItem = JSON.parseObject(jsonObject.toJSONString(), MediaServerItem.class); // 检查状态 Double aDouble = RedisUtil.zScore(onlineKey, mediaServerItem.getId()); if (aDouble != null) { From b89e46871ab8560d23c77a778663e48b50966707 Mon Sep 17 00:00:00 2001 From: kunlong-luo Date: Thu, 2 Feb 2023 16:19:02 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修复空指针异常 2. 修复类型转换异常 3. 封装 JsonUtil 工具类支持类型转换 --- .../session/VideoStreamSessionManager.java | 3 +- .../service/impl/MediaServerServiceImpl.java | 14 ++++--- .../storager/impl/RedisCatchStorageImpl.java | 25 +++++++------ .../com/genersoft/iot/vmp/utils/JsonUtil.java | 37 +++++++++++++++++++ 4 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/session/VideoStreamSessionManager.java b/src/main/java/com/genersoft/iot/vmp/gb28181/session/VideoStreamSessionManager.java index d9b9cc5a..f90de880 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/session/VideoStreamSessionManager.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/session/VideoStreamSessionManager.java @@ -4,6 +4,7 @@ import com.genersoft.iot.vmp.common.VideoManagerConstants; import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo; import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction; +import com.genersoft.iot.vmp.utils.JsonUtil; import com.genersoft.iot.vmp.utils.redis.RedisUtil; import gov.nist.javax.sip.message.SIPResponse; import org.springframework.beans.factory.annotation.Autowired; @@ -133,7 +134,7 @@ public class VideoStreamSessionManager { List result= new ArrayList<>(); for (int i = 0; i < ssrcTransactionKeys.size(); i++) { String key = (String)ssrcTransactionKeys.get(i); - SsrcTransaction ssrcTransaction = (SsrcTransaction)RedisUtil.get(key); + SsrcTransaction ssrcTransaction = JsonUtil.redisJsonToObject(key, SsrcTransaction.class); result.add(ssrcTransaction); } return result; diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java index c005e640..c93e449d 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java @@ -22,6 +22,7 @@ import com.genersoft.iot.vmp.service.bean.SSRCInfo; import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; import com.genersoft.iot.vmp.utils.DateUtil; +import com.genersoft.iot.vmp.utils.JsonUtil; import com.genersoft.iot.vmp.utils.redis.RedisUtil; import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; import okhttp3.OkHttpClient; @@ -228,11 +229,10 @@ public class MediaServerServiceImpl implements IMediaServerService { String onlineKey = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); for (Object mediaServerKey : mediaServerKeys) { String key = (String) mediaServerKey; - JSONObject jsonObject = (JSONObject) RedisUtil.get(key); - if (Objects.isNull(jsonObject)) { + MediaServerItem mediaServerItem = JsonUtil.redisJsonToObject(key, MediaServerItem.class); + if (Objects.isNull(mediaServerItem)) { continue; } - MediaServerItem mediaServerItem = JSON.parseObject(jsonObject.toJSONString(), MediaServerItem.class); // 检查状态 Double aDouble = RedisUtil.zScore(onlineKey, mediaServerItem.getId()); if (aDouble != null) { @@ -284,7 +284,7 @@ public class MediaServerServiceImpl implements IMediaServerService { return null; } String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerId; - return (MediaServerItem)RedisUtil.get(key); + return JsonUtil.redisJsonToObject(key, MediaServerItem.class); } @Override @@ -400,8 +400,10 @@ public class MediaServerServiceImpl implements IMediaServerService { SsrcConfig ssrcConfig = new SsrcConfig(zlmServerConfig.getGeneralMediaServerId(), null, sipConfig.getDomain()); serverItem.setSsrcConfig(ssrcConfig); }else { - MediaServerItem mediaServerItemInRedis = (MediaServerItem)RedisUtil.get(key); - serverItem.setSsrcConfig(mediaServerItemInRedis.getSsrcConfig()); + MediaServerItem mediaServerItemInRedis = JsonUtil.redisJsonToObject(key, MediaServerItem.class); + if (Objects.nonNull(mediaServerItemInRedis)) { + serverItem.setSsrcConfig(mediaServerItemInRedis.getSsrcConfig()); + } } RedisUtil.set(key, serverItem); resetOnlineServerItem(serverItem); diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java index 8cf5293b..55ebf7a0 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java @@ -17,6 +17,7 @@ import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper; import com.genersoft.iot.vmp.storager.dao.dto.PlatformRegisterInfo; import com.genersoft.iot.vmp.utils.DateUtil; +import com.genersoft.iot.vmp.utils.JsonUtil; import com.genersoft.iot.vmp.utils.SystemInfoUtils; import com.genersoft.iot.vmp.utils.redis.RedisUtil; import org.slf4j.Logger; @@ -157,7 +158,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { } for (Object player : players) { String key = (String) player; - StreamInfo streamInfo = (StreamInfo) RedisUtil.get(key); + StreamInfo streamInfo = JsonUtil.redisJsonToObject(key, StreamInfo.class); + if (Objects.isNull(streamInfo)) { + continue; + } streamInfos.put(streamInfo.getDeviceID() + "_" + streamInfo.getChannelId(), streamInfo); } return streamInfos; @@ -624,8 +628,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @Override public ThirdPartyGB queryMemberNoGBId(String queryKey) { String key = VideoManagerConstants.WVP_STREAM_GB_ID_PREFIX + queryKey; - JSONObject jsonObject = (JSONObject)RedisUtil.get(key); - return jsonObject.to(ThirdPartyGB.class); + return JsonUtil.redisJsonToObject(key, ThirdPartyGB.class); } @Override @@ -664,7 +667,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @Override public Device getDevice(String deviceId) { String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId; - return (Device)RedisUtil.get(key); + return JsonUtil.redisJsonToObject(key, Device.class); } @Override @@ -676,7 +679,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @Override public GPSMsgInfo getGpsMsgInfo(String gbId) { String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetting.getServerId() + "_" + gbId; - return (GPSMsgInfo)RedisUtil.get(key); + return JsonUtil.redisJsonToObject(key, GPSMsgInfo.class); } @Override @@ -686,9 +689,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { List keys = RedisUtil.scan(scanKey); for (Object o : keys) { String key = (String) o; - GPSMsgInfo gpsMsgInfo = (GPSMsgInfo) RedisUtil.get(key); - if (!gpsMsgInfo.isStored()) { // 只取没有存过得 - result.add((GPSMsgInfo) RedisUtil.get(key)); + GPSMsgInfo gpsMsgInfo = JsonUtil.redisJsonToObject(key, GPSMsgInfo.class); + if (Objects.nonNull(gpsMsgInfo) && !gpsMsgInfo.isStored()) { // 只取没有存过得 + result.add(JsonUtil.redisJsonToObject(key, GPSMsgInfo.class)); } } @@ -710,7 +713,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @Override public StreamAuthorityInfo getStreamAuthorityInfo(String app, String stream) { String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY + userSetting.getServerId() + "_" + app+ "_" + stream ; - return (StreamAuthorityInfo) RedisUtil.get(key); + return JsonUtil.redisJsonToObject(key, StreamAuthorityInfo.class); } @@ -721,7 +724,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { List keys = RedisUtil.scan(scanKey); for (Object o : keys) { String key = (String) o; - result.add((StreamAuthorityInfo) RedisUtil.get(key)); + result.add(JsonUtil.redisJsonToObject(key, StreamAuthorityInfo.class)); } return result; } @@ -735,7 +738,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { List keys = RedisUtil.scan(scanKey); if (keys.size() > 0) { String key = (String) keys.get(0); - result = (OnStreamChangedHookParam)RedisUtil.get(key); + result = JsonUtil.redisJsonToObject(key, OnStreamChangedHookParam.class); } return result; diff --git a/src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java b/src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java new file mode 100644 index 00000000..6ae56347 --- /dev/null +++ b/src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java @@ -0,0 +1,37 @@ +package com.genersoft.iot.vmp.utils; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import com.genersoft.iot.vmp.utils.redis.RedisUtil; + +import java.util.Objects; + +/** + * JsonUtil + * + * @author KunLong-Luo + * @version 1.0.0 + * @since 2023/2/2 15:24 + */ +public final class JsonUtil { + + private JsonUtil() { + } + + /** + * safe json type conversion + * + * @param key redis key + * @param clazz cast type + * @param + * @return result type + */ + public static T redisJsonToObject(String key, Class clazz) { + JSONObject jsonObject = (JSONObject) RedisUtil.get(key); + if (Objects.isNull(jsonObject)) { + return null; + } + return JSON.parseObject(jsonObject.toJSONString(), clazz); + } + +} \ No newline at end of file From 0a380c6ff916015b434c398448ae08746b6b0a4b Mon Sep 17 00:00:00 2001 From: "gaoxun250@sina.com" Date: Fri, 3 Feb 2023 14:19:10 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=88=AA=E5=9B=BE?= =?UTF-8?q?=E8=A2=AB=E5=8D=A0=E7=94=A8=E6=97=A0=E6=B3=95=E6=89=93=E5=BC=80?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java index 99a695eb..56af6049 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java @@ -186,6 +186,7 @@ public class ZLMRESTfulUtils { FileOutputStream outStream = new FileOutputStream(snapFile); outStream.write(Objects.requireNonNull(response.body()).bytes()); + outStream.flush(); outStream.close(); } else { logger.error(String.format("[ %s ]请求失败: %s %s", url, response.code(), response.message())); From d50dd3fe8465df67986ab49bab008c7e371f63cb Mon Sep 17 00:00:00 2001 From: xiaoQQya Date: Sat, 4 Feb 2023 18:12:25 +0800 Subject: [PATCH 4/5] =?UTF-8?q?perf(ZLMRESTfulUtils):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?OkHttpClient=20=E8=AF=BB=E5=8F=96=E8=B6=85=E6=97=B6=E6=97=B6?= =?UTF-8?q?=E9=97=B4=EF=BC=8C=E8=A7=A3=E5=86=B3=E7=82=B9=E6=92=AD=E6=97=B6?= =?UTF-8?q?=E6=88=AA=E5=9B=BE=E5=BF=AB=E7=85=A7=E8=8E=B7=E5=8F=96=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java index 56af6049..3610f319 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java @@ -36,7 +36,7 @@ public class ZLMRESTfulUtils { // 设置连接超时时间 httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS); // 设置读取超时时间 - httpClientBuilder.readTimeout(5,TimeUnit.SECONDS); + httpClientBuilder.readTimeout(10,TimeUnit.SECONDS); // 设置连接池 httpClientBuilder.connectionPool(new ConnectionPool(16, 5, TimeUnit.MINUTES)); if (logger.isDebugEnabled()) { From a5ed284658d70875e74c4c8ca2924de933e7d9af Mon Sep 17 00:00:00 2001 From: xiaoQQya Date: Sat, 4 Feb 2023 18:13:47 +0800 Subject: [PATCH 5/5] =?UTF-8?q?fix(JsonUtil):=20=E4=BF=AE=E5=A4=8D=20Redis?= =?UTF-8?q?=20=E4=B8=AD=20Object=20=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=8E?= =?UTF-8?q?=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=8D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java b/src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java index 6ae56347..60e2dbe2 100644 --- a/src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java +++ b/src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java @@ -27,11 +27,10 @@ public final class JsonUtil { * @return result type */ public static T redisJsonToObject(String key, Class clazz) { - JSONObject jsonObject = (JSONObject) RedisUtil.get(key); + Object jsonObject = RedisUtil.get(key); if (Objects.isNull(jsonObject)) { return null; } - return JSON.parseObject(jsonObject.toJSONString(), clazz); + return clazz.cast(jsonObject); } - } \ No newline at end of file