优化订阅机制,需要重新订阅时,取消命令发送后再发送订阅命令 #1273

pull/1336/head
648540858 2024-02-20 11:11:10 +08:00
parent b079039f81
commit a9ab5c28e9
10 changed files with 75 additions and 68 deletions

View File

@ -52,7 +52,7 @@ public class SubscribeHolder {
Runnable runnable = dynamicTask.get(taskOverdueKey); Runnable runnable = dynamicTask.get(taskOverdueKey);
if (runnable instanceof ISubscribeTask) { if (runnable instanceof ISubscribeTask) {
ISubscribeTask subscribeTask = (ISubscribeTask) runnable; ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
subscribeTask.stop(); subscribeTask.stop(null);
} }
// 添加任务处理订阅过期 // 添加任务处理订阅过期
dynamicTask.stop(taskOverdueKey); dynamicTask.stop(taskOverdueKey);
@ -87,7 +87,7 @@ public class SubscribeHolder {
Runnable runnable = dynamicTask.get(taskOverdueKey); Runnable runnable = dynamicTask.get(taskOverdueKey);
if (runnable instanceof ISubscribeTask) { if (runnable instanceof ISubscribeTask) {
ISubscribeTask subscribeTask = (ISubscribeTask) runnable; ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
subscribeTask.stop(); subscribeTask.stop(null);
} }
// 添加任务处理订阅过期 // 添加任务处理订阅过期
dynamicTask.stop(taskOverdueKey); dynamicTask.stop(taskOverdueKey);

View File

@ -1,10 +1,10 @@
package com.genersoft.iot.vmp.gb28181.task; package com.genersoft.iot.vmp.gb28181.task;
import javax.sip.DialogState; import com.genersoft.iot.vmp.common.CommonCallback;
/** /**
* @author lin * @author lin
*/ */
public interface ISubscribeTask extends Runnable{ public interface ISubscribeTask extends Runnable{
void stop(); void stop(CommonCallback<Boolean> callback);
} }

View File

@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.gb28181.task.impl; package com.genersoft.iot.vmp.gb28181.task.impl;
import com.genersoft.iot.vmp.common.CommonCallback;
import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.conf.DynamicTask;
import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask; import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
@ -7,14 +8,13 @@ import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
import gov.nist.javax.sip.message.SIPRequest; import gov.nist.javax.sip.message.SIPRequest;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.sip.*; import javax.sip.DialogState;
import javax.sip.InvalidArgumentException;
import javax.sip.ResponseEvent;
import javax.sip.SipException;
import javax.sip.header.ToHeader; import javax.sip.header.ToHeader;
import java.text.ParseException; import java.text.ParseException;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* *
@ -71,7 +71,7 @@ public class CatalogSubscribeTask implements ISubscribeTask {
} }
@Override @Override
public void stop() { public void stop(CommonCallback<Boolean> callback) {
/** /**
* dialog * dialog
* EARLY-> Early state- * EARLY-> Early state-
@ -94,6 +94,9 @@ public class CatalogSubscribeTask implements ISubscribeTask {
// 成功 // 成功
logger.info("[取消目录订阅]成功: {}", device.getDeviceId()); logger.info("[取消目录订阅]成功: {}", device.getDeviceId());
} }
if (callback != null) {
callback.run(event.getResponse().getRawContent() != null);
}
},eventResult -> { },eventResult -> {
// 失败 // 失败
logger.warn("[取消目录订阅]失败,信令发送失败: {}-{} ", device.getDeviceId(), eventResult.msg); logger.warn("[取消目录订阅]失败,信令发送失败: {}-{} ", device.getDeviceId(), eventResult.msg);

View File

@ -1,20 +1,9 @@
package com.genersoft.iot.vmp.gb28181.task.impl; package com.genersoft.iot.vmp.gb28181.task.impl;
import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.common.CommonCallback;
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask; import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
import com.genersoft.iot.vmp.service.IPlatformService; import com.genersoft.iot.vmp.service.IPlatformService;
import com.genersoft.iot.vmp.service.bean.GPSMsgInfo;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import com.genersoft.iot.vmp.utils.SpringBeanFactory; import com.genersoft.iot.vmp.utils.SpringBeanFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import javax.sip.DialogState;
import java.util.List;
/** /**
* ()MobilePosition * ()MobilePosition
@ -38,7 +27,7 @@ public class MobilePositionSubscribeHandlerTask implements ISubscribeTask {
} }
@Override @Override
public void stop() { public void stop(CommonCallback<Boolean> callback) {
} }
} }

View File

@ -1,21 +1,19 @@
package com.genersoft.iot.vmp.gb28181.task.impl; package com.genersoft.iot.vmp.gb28181.task.impl;
import com.genersoft.iot.vmp.common.CommonCallback;
import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.conf.DynamicTask;
import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask; import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander; import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
import gov.nist.javax.sip.message.SIPRequest; import gov.nist.javax.sip.message.SIPRequest;
import gov.nist.javax.sip.message.SIPResponse;
import org.dom4j.Element;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import javax.sip.*; import javax.sip.InvalidArgumentException;
import javax.sip.ResponseEvent;
import javax.sip.SipException;
import javax.sip.header.ToHeader; import javax.sip.header.ToHeader;
import java.text.ParseException; import java.text.ParseException;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* *
@ -70,7 +68,7 @@ public class MobilePositionSubscribeTask implements ISubscribeTask {
} }
@Override @Override
public void stop() { public void stop(CommonCallback<Boolean> callback) {
/** /**
* dialog * dialog
* EARLY-> Early state- * EARLY-> Early state-
@ -92,6 +90,9 @@ public class MobilePositionSubscribeTask implements ISubscribeTask {
// 成功 // 成功
logger.info("[取消移动位置订阅]成功: {}", device.getDeviceId()); logger.info("[取消移动位置订阅]成功: {}", device.getDeviceId());
} }
if (callback != null) {
callback.run(event.getResponse().getRawContent() != null);
}
},eventResult -> { },eventResult -> {
// 失败 // 失败
logger.warn("[取消移动位置订阅]失败,信令发送失败: {}-{} ", device.getDeviceId(), eventResult.msg); logger.warn("[取消移动位置订阅]失败,信令发送失败: {}-{} ", device.getDeviceId(), eventResult.msg);

View File

@ -82,8 +82,9 @@ public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent imp
device.setIp(remoteAddressInfo.getIp()); device.setIp(remoteAddressInfo.getIp());
// 设备地址变化会引起目录订阅任务失效,需要重新添加 // 设备地址变化会引起目录订阅任务失效,需要重新添加
if (device.getSubscribeCycleForCatalog() > 0) { if (device.getSubscribeCycleForCatalog() > 0) {
deviceService.removeCatalogSubscribe(device); deviceService.removeCatalogSubscribe(device, result->{
deviceService.addCatalogSubscribe(device); deviceService.addCatalogSubscribe(device);
});
} }
} }
if (device.getKeepaliveTime() == null) { if (device.getKeepaliveTime() == null) {

View File

@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.service; package com.genersoft.iot.vmp.service;
import com.genersoft.iot.vmp.common.CommonCallback;
import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo; import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo;
@ -39,7 +40,7 @@ public interface IDeviceService {
* @param device * @param device
* @return * @return
*/ */
boolean removeCatalogSubscribe(Device device); boolean removeCatalogSubscribe(Device device, CommonCallback<Boolean> callback);
/** /**
* *
@ -53,7 +54,7 @@ public interface IDeviceService {
* @param device * @param device
* @return * @return
*/ */
boolean removeMobilePositionSubscribe(Device device); boolean removeMobilePositionSubscribe(Device device, CommonCallback<Boolean> callback);
/** /**
* *

View File

@ -1,6 +1,7 @@
package com.genersoft.iot.vmp.service.impl; package com.genersoft.iot.vmp.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DS;
import com.genersoft.iot.vmp.common.CommonCallback;
import com.genersoft.iot.vmp.common.VideoManagerConstants; import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.conf.DynamicTask;
import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.conf.UserSetting;
@ -231,8 +232,8 @@ public class DeviceServiceImpl implements IDeviceService {
} }
} }
// 移除订阅 // 移除订阅
removeCatalogSubscribe(device); removeCatalogSubscribe(device, null);
removeMobilePositionSubscribe(device); removeMobilePositionSubscribe(device, null);
} }
@Override @Override
@ -251,7 +252,7 @@ public class DeviceServiceImpl implements IDeviceService {
} }
@Override @Override
public boolean removeCatalogSubscribe(Device device) { public boolean removeCatalogSubscribe(Device device, CommonCallback<Boolean> callback) {
if (device == null || device.getSubscribeCycleForCatalog() < 0) { if (device == null || device.getSubscribeCycleForCatalog() < 0) {
return false; return false;
} }
@ -261,7 +262,7 @@ public class DeviceServiceImpl implements IDeviceService {
Runnable runnable = dynamicTask.get(taskKey); Runnable runnable = dynamicTask.get(taskKey);
if (runnable instanceof ISubscribeTask) { if (runnable instanceof ISubscribeTask) {
ISubscribeTask subscribeTask = (ISubscribeTask) runnable; ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
subscribeTask.stop(); subscribeTask.stop(callback);
} }
} }
dynamicTask.stop(taskKey); dynamicTask.stop(taskKey);
@ -284,7 +285,7 @@ public class DeviceServiceImpl implements IDeviceService {
} }
@Override @Override
public boolean removeMobilePositionSubscribe(Device device) { public boolean removeMobilePositionSubscribe(Device device, CommonCallback<Boolean> callback) {
if (device == null || device.getSubscribeCycleForCatalog() < 0) { if (device == null || device.getSubscribeCycleForCatalog() < 0) {
return false; return false;
} }
@ -294,7 +295,7 @@ public class DeviceServiceImpl implements IDeviceService {
Runnable runnable = dynamicTask.get(taskKey); Runnable runnable = dynamicTask.get(taskKey);
if (runnable instanceof ISubscribeTask) { if (runnable instanceof ISubscribeTask) {
ISubscribeTask subscribeTask = (ISubscribeTask) runnable; ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
subscribeTask.stop(); subscribeTask.stop(callback);
} }
} }
dynamicTask.stop(taskKey); dynamicTask.stop(taskKey);
@ -522,39 +523,54 @@ public class DeviceServiceImpl implements IDeviceService {
if (!ObjectUtils.isEmpty(device.getStreamMode())) { if (!ObjectUtils.isEmpty(device.getStreamMode())) {
deviceInStore.setStreamMode(device.getStreamMode()); deviceInStore.setStreamMode(device.getStreamMode());
} }
// 目录订阅相关的信息 // 目录订阅相关的信息
if (deviceInStore.getSubscribeCycleForCatalog() != device.getSubscribeCycleForCatalog()) { if (deviceInStore.getSubscribeCycleForCatalog() != device.getSubscribeCycleForCatalog()) {
if (device.getSubscribeCycleForCatalog() > 0) { if (device.getSubscribeCycleForCatalog() > 0) {
// 若已开启订阅,但订阅周期不同,则先取消 // 若已开启订阅,但订阅周期不同,则先取消
if (deviceInStore.getSubscribeCycleForCatalog() != 0) { if (deviceInStore.getSubscribeCycleForCatalog() != 0) {
removeCatalogSubscribe(deviceInStore); removeCatalogSubscribe(deviceInStore, result->{
}
// 开启订阅 // 开启订阅
deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog()); deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
addCatalogSubscribe(deviceInStore); addCatalogSubscribe(deviceInStore);
}else if (device.getSubscribeCycleForCatalog() == 0) { // 因为是异步执行,需要在这里更新下数据
// 取消订阅 deviceMapper.updateCustom(deviceInStore);
redisCatchStorage.updateDevice(deviceInStore);
});
}else {
// 开启订阅
deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog()); deviceInStore.setSubscribeCycleForCatalog(device.getSubscribeCycleForCatalog());
removeCatalogSubscribe(deviceInStore); addCatalogSubscribe(deviceInStore);
}
} }
}else if (device.getSubscribeCycleForCatalog() == 0) {
// 取消订阅
deviceInStore.setSubscribeCycleForCatalog(0);
removeCatalogSubscribe(deviceInStore, null);
}
}
// 移动位置订阅相关的信息 // 移动位置订阅相关的信息
if (deviceInStore.getSubscribeCycleForMobilePosition() != device.getSubscribeCycleForMobilePosition()) {
if (device.getSubscribeCycleForMobilePosition() > 0) { if (device.getSubscribeCycleForMobilePosition() > 0) {
if (deviceInStore.getSubscribeCycleForMobilePosition() == 0 || deviceInStore.getSubscribeCycleForMobilePosition() != device.getSubscribeCycleForMobilePosition()) { // 若已开启订阅,但订阅周期不同,则先取消
deviceInStore.setMobilePositionSubmissionInterval(device.getMobilePositionSubmissionInterval()); if (deviceInStore.getSubscribeCycleForMobilePosition() != 0) {
deviceInStore.setSubscribeCycleForMobilePosition(device.getSubscribeCycleForMobilePosition()); removeMobilePositionSubscribe(deviceInStore, result->{
// 开启订阅 // 开启订阅
deviceInStore.setSubscribeCycleForMobilePosition(device.getSubscribeCycleForMobilePosition());
addMobilePositionSubscribe(deviceInStore);
// 因为是异步执行,需要在这里更新下数据
deviceMapper.updateCustom(deviceInStore);
redisCatchStorage.updateDevice(deviceInStore);
});
}else {
// 开启订阅
deviceInStore.setSubscribeCycleForMobilePosition(device.getSubscribeCycleForMobilePosition());
addMobilePositionSubscribe(deviceInStore); addMobilePositionSubscribe(deviceInStore);
} }
}else if (device.getSubscribeCycleForMobilePosition() == 0) { }else if (device.getSubscribeCycleForMobilePosition() == 0) {
if (deviceInStore.getSubscribeCycleForMobilePosition() != 0) {
deviceInStore.setMobilePositionSubmissionInterval(device.getMobilePositionSubmissionInterval());
deviceInStore.setSubscribeCycleForMobilePosition(device.getSubscribeCycleForMobilePosition());
// 取消订阅 // 取消订阅
removeMobilePositionSubscribe(deviceInStore); deviceInStore.setSubscribeCycleForCatalog(0);
removeCatalogSubscribe(deviceInStore, null);
} }
} }
if (deviceInStore.getGeoCoordSys() != null) { if (deviceInStore.getGeoCoordSys() != null) {
@ -574,9 +590,8 @@ public class DeviceServiceImpl implements IDeviceService {
//作为消息通道 //作为消息通道
deviceInStore.setAsMessageChannel(device.isAsMessageChannel()); deviceInStore.setAsMessageChannel(device.isAsMessageChannel());
// 更新redis
deviceMapper.updateCustom(deviceInStore); deviceMapper.updateCustom(deviceInStore);
redisCatchStorage.removeDevice(deviceInStore.getDeviceId()); redisCatchStorage.updateDevice(deviceInStore);
} }
@Override @Override

View File

@ -153,10 +153,7 @@ public class MobilePositionController {
Device device = storager.queryVideoDevice(deviceId); Device device = storager.queryVideoDevice(deviceId);
device.setSubscribeCycleForMobilePosition(Integer.parseInt(expires)); device.setSubscribeCycleForMobilePosition(Integer.parseInt(expires));
device.setMobilePositionSubmissionInterval(Integer.parseInt(interval)); device.setMobilePositionSubmissionInterval(Integer.parseInt(interval));
deviceService.updateDevice(device); deviceService.updateCustomDevice(device);
if (!deviceService.removeMobilePositionSubscribe(device)) {
throw new ControllerException(ErrorCode.ERROR100);
}
} }
/** /**

View File

@ -199,7 +199,7 @@ public class DeviceQuery {
Runnable runnable = dynamicTask.get(key); Runnable runnable = dynamicTask.get(key);
if (runnable instanceof ISubscribeTask) { if (runnable instanceof ISubscribeTask) {
ISubscribeTask subscribeTask = (ISubscribeTask) runnable; ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
subscribeTask.stop(); subscribeTask.stop(null);
} }
dynamicTask.stop(key); dynamicTask.stop(key);
} }