缓冲池

存储模板类:

package com.huawei.manageone.modriverframework.mocommondriverservice.clearAlarm;

/**
 * The InfoContent
 *
 * @since 2020/4/8
 */
class InfoContent<T> {

    private long cacheTime;

    private long createTime = System.currentTimeMillis();

    private T content;

    public long getCreateTime() {
        return this.createTime;
    }

    public T getValue() {
        return this.content;
    }

    public void setValue(T value) {
        this.content = value;
    }

    public long getCacheTime() {
        return cacheTime;
    }

    public void setCacheTime(long cacheTime) {
        this.cacheTime = cacheTime;
    }
}

管理类:

package com.huawei.manageone.modriverframework.mocommondriverservice.clearAlarm;

import com.huawei.bsp.deploy.util.DefaultEnvUtil;
import com.huawei.bsp.log.OssLog;
import com.huawei.bsp.log.OssLogFactory;
import com.huawei.ies.sdk.utils.FileUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

/**
 * The InfoContentMange
 *
 * @since 2020/4/8
 */
public class InfoContentMange {

    private static final OssLog LOG = OssLogFactory.getLog(InfoContentMange.class);

    private static ConcurrentHashMap<String,InfoContent> contentMap = new ConcurrentHashMap<>();

    private static final String PATH = "com/huawei/manageone/modriverframework/mocommondriverservice/clearAlarm/configurationFile.json";

    private static InfoContentMange infoContentMange = new InfoContentMange();

    private static Long expirationDate = 60000L;

    private static final String KEY = "expirationDate";

    private static final String TIME_CYCLE = "timerCycle";

    private static Long timeCycle = 30000L;

    private InfoContentMange (){
    }

    static{
        String path = DefaultEnvUtil.getAppRoot() + PATH;
        try{
            String jsonString = FileUtil.getFileAsString(path);
            JSONObject jsonObject = JSON.parseObject(jsonString);
            if (jsonObject.containsKey(KEY)){
                expirationDate = jsonObject.getLong(KEY);
            }
            if (jsonObject.containsKey(TIME_CYCLE)){
                timeCycle = jsonObject.getLong(TIME_CYCLE);
            }
        } catch (IOException e){
            LOG.error("get file 'configurationFile.json' fail. file path is {}",path);
        }
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                deleteOldData();
            }
        },0,timeCycle);
    }

    private static void deleteOldData(){
        Iterator it = contentMap.keySet().iterator();
        while (it.hasNext()){
            String nativeId = (String) it.next();
            if (isOldData(nativeId)){
                it.remove();
            }
        }
    }

    private static boolean isOldData(String nativeId){
        if(contentMap.containsKey(nativeId)){
            InfoContent cacheEntity = contentMap.get(nativeId);
            long createTime = cacheEntity.getCreateTime();
            long currentTime = System.currentTimeMillis();
            long cacheTime = cacheEntity.getCacheTime();
            if (cacheTime>0&&(currentTime-createTime)>cacheTime){
                return true;
            }
            return false;
        }
        return true;
    }


    public static InfoContentMange getInstance(){
        return infoContentMange;
    }

    /**
     * getSystemInfo.
     *
     * @param nativeId String
     * @return Object
     */
    public Object getSystemInfo(String nativeId){
        if(StringUtils.isEmpty(nativeId)){
            LOG.error("can not find the native id {}",nativeId);
            return null;
        }
        Object object = getNotExpireData(nativeId);
        if (object == null){
            LOG.error("can not find the native id {}",nativeId);
        }
        return object;
    }

    private Object getNotExpireData(String nativeId){
        if(contentMap.containsKey(nativeId)){
            InfoContent cacheEntity = contentMap.get(nativeId);
            long createTime = cacheEntity.getCreateTime();
            long currentTime = System.currentTimeMillis();
            long cacheTime = cacheEntity.getCacheTime();
            if(cacheTime>0&&currentTime-createTime>cacheTime){
                return refreshContent(nativeId);
            }
            LOG.warn("Found in cache.nativeId is {}",nativeId);
            return cacheEntity.getValue();
        }
        return refreshContent(nativeId);
    }

    private synchronized Map<String, Object> refreshContent(String nativeId){
        Map<String, Object> systemInfo = GetSystemInfo.process(nativeId);
        LOG.warn("SystemInfo for {} is {}",nativeId,systemInfo);
        if (systemInfo == null){
            LOG.error("get system info fail.");
            return null;
        }
        InfoContent<Object> cacheEntity = new InfoContent<>();
        cacheEntity.setCacheTime(expirationDate);
        cacheEntity.setValue(systemInfo);
        contentMap.put(nativeId,cacheEntity);
        return systemInfo;
    }
}

配置文件:

{
  "expirationDate": 60000,
  "timerCycle": 30000,
  "alarmPoolCapacity": 6000,
  "maximumCapacityOfSingleConsumption": 100,
  "numberOfConsumingThreads": 2
}

数据获取接口:

package com.huawei.manageone.modriverframework.mocommondriverservice.clearAlarm;

import com.huawei.bsp.log.OssLog;
import com.huawei.bsp.log.OssLogFactory;
import com.huawei.bsp.remoteservice.exception.ServiceException;
import com.huawei.bsp.roa.util.restclient.Restful;
import com.huawei.bsp.roa.util.restclient.RestfulFactory;
import com.huawei.bsp.roa.util.restclient.RestfulParametes;
import com.huawei.bsp.roa.util.restclient.RestfulResponse;
import com.huawei.cloudsop.common.tokenhelper.util.rpc.RestAdapter;
import com.huawei.ies.rucor.common.exception.RucorEnum;
import com.huawei.ies.sdk.utils.Constant;
import com.huawei.ies.sdk.utils.JsonUtil;
import com.huawei.manageone.modriverframework.mocommondriverservice.util.LoggerHelper;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The getSystemInfo
 *
 * @since 2020/4/8
 */
class GetSystemInfo {

    private static final OssLog LOG = OssLogFactory.getLog(GetSystemInfo.class);

    private static final Restful REST = new RestAdapter(RestfulFactory.getRestInstance());

    private static final String SEARCH_URL_NEW = "/rest/system-access/v1/accesspoint/nativeid/system?nativeId=";

    /**
     * process.
     *
     * @param nativeId String
     * @return Object
     */
    public static Map<String, Object> process(String nativeId){
        RestfulParametes restParams = new RestfulParametes();
        Map<String, String> header = new HashMap<>();
        header.put("Content-Type", "application/json");
        restParams.setHeaderMap(header);
        RestfulResponse response;
        try {
            LOG.info("Search system {} Info.", nativeId);
            response = REST.get(SEARCH_URL_NEW + nativeId, restParams);
        } catch (ServiceException e) {
            LOG.error("ServiceException exception.nativeId is {}",nativeId,e);
            return null;
        }
        if (null == response || null == response.getResponseContent()) {
            LOG.error("search system response is null.nativeId is {}",nativeId);
            return null;
        }
        LOG.info("Search system Response is {}", LoggerHelper.logResponse(response));
        Map<String, Object> responseMap = JSON.parseObject(response.getResponseContent());
        String successCode = RucorEnum.SUCCESS.getCode();
        if (!responseMap.containsKey("retCode") || !successCode.equals(responseMap.get("retCode"))) {
            LOG.error("search system response {} retCode is error", response.getResponseContent());
            return null;
        }
        List<Map> dataList = JsonUtil.parseArray(JsonUtil.getJson(responseMap.get("data")), Map.class);
        if (null == dataList || dataList.isEmpty()) {
            return null;
        }
        return dataList.get(Constant.INT_0);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值