From 2bf7cdc2df8482b775c05e783e49f3bb7ec58da5 Mon Sep 17 00:00:00 2001 From: ljm <1271952916@qq.com> Date: 星期四, 20 十月 2022 16:04:47 +0800 Subject: [PATCH] 【zlm心跳hook】重复查询redis了 变量也不需要多次定义 数据库有的话应该恢复zlm --- src/main/java/com/genersoft/iot/vmp/service/impl/StreamPushServiceImpl.java | 143 +++++++++++++++++++++++++++++------------------ 1 files changed, 88 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamPushServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamPushServiceImpl.java index 1e00faa..00980f9 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamPushServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamPushServiceImpl.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; +import com.genersoft.iot.vmp.conf.MediaConfig; import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.gb28181.bean.*; import com.genersoft.iot.vmp.gb28181.event.EventPublisher; @@ -13,14 +14,20 @@ import com.genersoft.iot.vmp.service.IGbStreamService; import com.genersoft.iot.vmp.service.IMediaServerService; import com.genersoft.iot.vmp.service.IStreamPushService; +import com.genersoft.iot.vmp.service.bean.StreamPushItemFromRedis; import com.genersoft.iot.vmp.storager.IRedisCatchStorage; import com.genersoft.iot.vmp.storager.dao.*; +import com.genersoft.iot.vmp.utils.DateUtil; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionStatus; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import java.util.*; @@ -36,6 +43,9 @@ @Autowired private StreamPushMapper streamPushMapper; + + @Autowired + private StreamProxyMapper streamProxyMapper; @Autowired private ParentPlatformMapper parentPlatformMapper; @@ -63,6 +73,16 @@ @Autowired private IMediaServerService mediaServerService; + + @Autowired + DataSourceTransactionManager dataSourceTransactionManager; + + @Autowired + TransactionDefinition transactionDefinition; + + @Autowired + private MediaConfig mediaConfig; + @Override public List<StreamPushItem> handleJSON(String jsonData, MediaServerItem mediaServerItem) { @@ -102,7 +122,7 @@ streamPushItem.setOriginType(item.getOriginType()); streamPushItem.setOriginTypeStr(item.getOriginTypeStr()); streamPushItem.setOriginUrl(item.getOriginUrl()); - streamPushItem.setCreateStamp(item.getCreateStamp() * 1000); + streamPushItem.setCreateTime(DateUtil.getNow()); streamPushItem.setAliveSecond(item.getAliveSecond()); streamPushItem.setStatus(true); streamPushItem.setStreamType("push"); @@ -127,31 +147,10 @@ public boolean saveToGB(GbStream stream) { stream.setStreamType("push"); stream.setStatus(true); - stream.setCreateStamp(System.currentTimeMillis()); + stream.setCreateTime(DateUtil.getNow()); + stream.setStreamType("push"); + stream.setMediaServerId(mediaConfig.getId()); int add = gbStreamMapper.add(stream); - - // 鏌ユ壘寮�鍚簡鍏ㄩ儴鐩存挱娴佸叡浜殑涓婄骇骞冲彴 - List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream(); - if (parentPlatforms.size() > 0) { - for (ParentPlatform parentPlatform : parentPlatforms) { - stream.setCatalogId(parentPlatform.getCatalogId()); - stream.setPlatformId(parentPlatform.getServerGBId()); - String streamId = stream.getStream(); - StreamProxyItem streamProxyItem = platformGbStreamMapper.selectOne(stream.getApp(), streamId, parentPlatform.getServerGBId()); - if (streamProxyItem == null) { - platformGbStreamMapper.add(stream); - eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); - }else { - if (!streamProxyItem.getGbId().equals(stream.getGbId())) { - // 姝ゆ祦浣跨敤鍙︿竴涓浗鏍嘔d宸茬粡涓庤骞冲彴鍏宠仈锛岀Щ闄ゆ璁板綍 - platformGbStreamMapper.delByAppAndStreamAndPlatform(stream.getApp(), streamId, parentPlatform.getServerGBId()); - platformGbStreamMapper.add(stream); - eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); - } - } - } - } - return add > 0; } @@ -177,7 +176,6 @@ @Override public StreamPushItem getPush(String app, String streamId) { - return streamPushMapper.selectOne(app, streamId); } @@ -211,7 +209,7 @@ Map<String, MediaItem> streamInfoPushItemMap = new HashMap<>(); if (pushList.size() > 0) { for (StreamPushItem streamPushItem : pushList) { - if (StringUtils.isEmpty(streamPushItem.getGbId())) { + if (ObjectUtils.isEmpty(streamPushItem.getGbId())) { pushItemMap.put(streamPushItem.getApp() + streamPushItem.getStream(), streamPushItem); } } @@ -284,7 +282,8 @@ streamPushMapper.deleteWithoutGBId(mediaServerId); gbStreamMapper.deleteWithoutGBId("push", mediaServerId); // 鍏朵粬鐨勬祦璁剧疆鏈惎鐢� - gbStreamMapper.updateStatusByMediaServerId(mediaServerId, false); + streamPushMapper.updateStatusByMediaServerId(mediaServerId, false); + streamProxyMapper.updateStatusByMediaServerId(mediaServerId, false); // 鍙戦�佹祦鍋滄娑堟伅 String type = "PUSH"; // 鍙戦�乺edis娑堟伅 @@ -317,7 +316,7 @@ streamPushItem.setStreamType("push"); streamPushItem.setStatus(true); streamPushItem.setGbId("34020000004111" + gbId); - streamPushItem.setCreateStamp(System.currentTimeMillis()); + streamPushItem.setCreateTime(DateUtil.getNow()); gbId ++; } int limitCount = 30; @@ -340,32 +339,8 @@ public void batchAdd(List<StreamPushItem> streamPushItems) { streamPushMapper.addAll(streamPushItems); gbStreamMapper.batchAdd(streamPushItems); - // 鏌ユ壘寮�鍚簡鍏ㄩ儴鐩存挱娴佸叡浜殑涓婄骇骞冲彴 - List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream(); - if (parentPlatforms.size() > 0) { - for (StreamPushItem stream : streamPushItems) { - for (ParentPlatform parentPlatform : parentPlatforms) { - stream.setCatalogId(parentPlatform.getCatalogId()); - stream.setPlatformId(parentPlatform.getServerGBId()); - String streamId = stream.getStream(); - StreamProxyItem streamProxyItem = platformGbStreamMapper.selectOne(stream.getApp(), streamId, parentPlatform.getServerGBId()); - if (streamProxyItem == null) { - platformGbStreamMapper.add(stream); - eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); - }else { - if (!streamProxyItem.getGbId().equals(stream.getGbId())) { - // 姝ゆ祦浣跨敤鍙︿竴涓浗鏍嘔d宸茬粡涓庤骞冲彴鍏宠仈锛岀Щ闄ゆ璁板綍 - platformGbStreamMapper.delByAppAndStreamAndPlatform(stream.getApp(), streamId, parentPlatform.getServerGBId()); - platformGbStreamMapper.add(stream); - eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); - stream.setGbId(streamProxyItem.getGbId()); - eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.DEL); - } - } - } - } - } } + @Override public void batchAddForUpload(List<StreamPushItem> streamPushItems, Map<String, List<String[]>> streamPushItemsForAll ) { @@ -453,7 +428,6 @@ platformId, platformForEvent.get(platformId), CatalogEvent.ADD); } } - } } @@ -476,4 +450,63 @@ } return true; } + + @Override + public void allStreamOffline() { + List<GbStream> onlinePushers = streamPushMapper.getOnlinePusherForGb(); + if (onlinePushers.size() == 0) { + return; + } + streamPushMapper.setAllStreamOffline(); + + // 鍙戦�侀�氱煡 + eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.OFF); + } + + @Override + public void offline(List<StreamPushItemFromRedis> offlineStreams) { + // 鏇存柊閮ㄥ垎璁惧绂荤嚎 + List<GbStream> onlinePushers = streamPushMapper.getOnlinePusherForGbInList(offlineStreams); + streamPushMapper.offline(offlineStreams); + // 鍙戦�侀�氱煡 + eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.OFF); + } + + @Override + public void online(List<StreamPushItemFromRedis> onlineStreams) { + // 鏇存柊閮ㄥ垎璁惧涓婄嚎streamPushService + List<GbStream> onlinePushers = streamPushMapper.getOfflinePusherForGbInList(onlineStreams); + streamPushMapper.online(onlineStreams); + // 鍙戦�侀�氱煡 + eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.ON); + } + + @Override + public boolean add(StreamPushItem stream) { + stream.setUpdateTime(DateUtil.getNow()); + stream.setCreateTime(DateUtil.getNow()); + stream.setServerId(userSetting.getServerId()); + + // 鏀惧湪浜嬪姟鍐呮墽琛� + boolean result = false; + TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition); + try { + int addStreamResult = streamPushMapper.add(stream); + if (!ObjectUtils.isEmpty(stream.getGbId())) { + stream.setStreamType("push"); + gbStreamMapper.add(stream); + } + dataSourceTransactionManager.commit(transactionStatus); + result = true; + }catch (Exception e) { + logger.error("鎵归噺绉婚櫎娴佷笌骞冲彴鐨勫叧绯绘椂閿欒", e); + dataSourceTransactionManager.rollback(transactionStatus); + } + return result; + } + + @Override + public List<String> getAllAppAndStream() { + return streamPushMapper.getAllAppAndStream(); + } } -- Gitblit v1.8.0