All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.lunasaw.sip.common.subscribe.SubscribeHolder Maven / Gradle / Ivy

There is a newer version: 1.2.4
Show newest version
package io.github.lunasaw.sip.common.subscribe;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.github.lunasaw.sip.common.utils.DynamicTask;

/**
 * @author lin
 */
@Component
public class SubscribeHolder {

    private static ConcurrentHashMap catalogMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap mobilePositionMap = new ConcurrentHashMap<>();
    private final String taskOverduePrefix = "subscribe_overdue_";
    @Autowired
    private DynamicTask dynamicTask;

    public void putCatalogSubscribe(String userId, SubscribeInfo subscribeInfo) {
        catalogMap.put(userId, subscribeInfo);
        // 添加订阅到期
        String taskOverdueKey = taskOverduePrefix + "catalog_" + userId;
        // 添加任务处理订阅过期
        dynamicTask.startDelay(taskOverdueKey, () -> removeCatalogSubscribe(subscribeInfo.getId()),
                subscribeInfo.getExpires() * 1000);
    }

    public SubscribeInfo getCatalogSubscribe(String platformId) {
        return catalogMap.get(platformId);
    }

    public void removeCatalogSubscribe(String sipId) {

        catalogMap.remove(sipId);
        String taskOverdueKey = taskOverduePrefix + "catalog_" + sipId;
        Runnable runnable = dynamicTask.get(taskOverdueKey);
        if (runnable instanceof SubscribeTask) {
            SubscribeTask subscribeTask = (SubscribeTask) runnable;
            subscribeTask.stop();
        }
        // 添加任务处理订阅过期
        dynamicTask.stop(taskOverdueKey);
    }

    public void putMobilePositionSubscribe(String userId, String prefixKey, SubscribeTask task, SubscribeInfo subscribeInfo) {
        mobilePositionMap.put(userId, subscribeInfo);
        String key = prefixKey + userId;
        // 添加任务处理GPS定时推送
        dynamicTask.startCron(key, task, subscribeInfo.getGpsInterval() * 1000L);
        String taskOverdueKey = taskOverduePrefix + prefixKey + userId;
        // 添加任务处理订阅过期
        dynamicTask.startDelay(taskOverdueKey, () -> {
                    removeMobilePositionSubscribe(subscribeInfo.getId(), prefixKey);
                },
                subscribeInfo.getExpires() * 1000);
    }

    public SubscribeInfo getMobilePositionSubscribe(String userId) {
        return mobilePositionMap.get(userId);
    }

    public void removeMobilePositionSubscribe(String userId, String prefixKey) {
        mobilePositionMap.remove(userId);
        String key = prefixKey + "MobilePosition_" + userId;
        // 结束任务处理GPS定时推送
        dynamicTask.stop(key);
        String taskOverdueKey = taskOverduePrefix + "MobilePosition_" + userId;
        Runnable runnable = dynamicTask.get(taskOverdueKey);
        if (runnable instanceof SubscribeTask) {
            SubscribeTask subscribeTask = (SubscribeTask) runnable;
            subscribeTask.stop();
        }
        // 添加任务处理订阅过期
        dynamicTask.stop(taskOverdueKey);
    }

    public List getAllCatalogSubscribePlatform() {
        List platforms = new ArrayList<>();
        if (MapUtils.isNotEmpty(catalogMap)) {
            for (String key : catalogMap.keySet()) {
                platforms.add(catalogMap.get(key).getId());
            }
        }
        return platforms;
    }

    public void removeAllSubscribe(String userId, String prefixKey) {
        removeMobilePositionSubscribe(userId, prefixKey);
        removeCatalogSubscribe(userId);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy