优化订阅信息的发送与取消订阅

This commit is contained in:
648540858
2022-04-08 14:44:49 +08:00
parent f1c8ca602d
commit f10b458fc9
16 changed files with 242 additions and 124 deletions

View File

@@ -1,5 +1,6 @@
package com.genersoft.iot.vmp.conf;
import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -21,6 +22,7 @@ public class DynamicTask {
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
private Map<String, ScheduledFuture<?>> futureMap = new ConcurrentHashMap<>();
private Map<String, Runnable> runnableMap = new ConcurrentHashMap<>();
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
@@ -34,12 +36,12 @@ public class DynamicTask {
* @param cycleForCatalog 间隔
* @return
*/
public String startCron(String key, Runnable task, int cycleForCatalog) {
public void startCron(String key, Runnable task, int cycleForCatalog) {
stop(key);
// scheduleWithFixedDelay 必须等待上一个任务结束才开始计时period cycleForCatalog表示执行的间隔
ScheduledFuture future = threadPoolTaskScheduler.scheduleWithFixedDelay(task, cycleForCatalog * 1000L);
futureMap.put(key, future);
return "startCron";
runnableMap.put(key, task);
}
/**
@@ -49,18 +51,22 @@ public class DynamicTask {
* @param delay 延时 /毫秒
* @return
*/
public String startDelay(String key, Runnable task, int delay) {
public void startDelay(String key, Runnable task, int delay) {
stop(key);
Date starTime = new Date(System.currentTimeMillis() + delay);
// scheduleWithFixedDelay 必须等待上一个任务结束才开始计时period cycleForCatalog表示执行的间隔
ScheduledFuture future = threadPoolTaskScheduler.schedule(task, starTime);
futureMap.put(key, future);
return "startCron";
}
public void stop(String key) {
if (futureMap.get(key) != null && !futureMap.get(key).isCancelled()) {
futureMap.get(key).cancel(true);
Runnable runnable = runnableMap.get(key);
if (runnable instanceof ISubscribeTask) {
ISubscribeTask subscribeTask = (ISubscribeTask) runnable;
subscribeTask.stop();
}
}
}

View File

@@ -14,7 +14,7 @@ import java.util.List;
/**
* 系统启动时控制设备离线
* 系统启动时控制设备
*/
@Component
@Order(value=4)
@@ -41,10 +41,14 @@ public class SipDeviceRunner implements CommandLineRunner {
for (String deviceId : onlineForAll) {
storager.online(deviceId);
Device device = redisCatchStorage.getDevice(deviceId);
if (device != null && device.getSubscribeCycleForCatalog() > 0) {
// 查询在线设备那些开启了订阅,为设备开启定时的目录订阅
deviceService.addCatalogSubscribe(device);
deviceService.addMobilePositionSubscribe(device);
if (device != null ) {
if (device.getSubscribeCycleForCatalog() > 0) {
// 查询在线设备那些开启了订阅,为设备开启定时的目录订阅
deviceService.addCatalogSubscribe(device);
}
if (device.getSubscribeCycleForMobilePosition() > 0) {
deviceService.addMobilePositionSubscribe(device);
}
}
}
// 重置cseq计数