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

com.bizmda.bizsip.integrator.checkrule.ServiceCheckRuleHelper Maven / Gradle / Ivy

There is a newer version: 1.0.0.Beta9
Show newest version
package com.bizmda.bizsip.integrator.checkrule;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONObject;
import com.bizmda.bizsip.common.BizException;
import com.bizmda.bizsip.common.BizResultEnum;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class ServiceCheckRuleHelper {
    private static ExecutorService serviceCheckExecutorService = ThreadUtil.newExecutor(Runtime.getRuntime().availableProcessors());

    private ServiceCheckRuleHelper() {

    }

    public static List checkServiceRule(JSONObject jsonObject, List serviceCheckRuleList, CheckMode checkMode) throws BizException {
        CompletionService service = new ExecutorCompletionService<>(serviceCheckExecutorService);
        List> futureList = new ArrayList<>();
        for (ServiceCheckRule serviceCheckRule : serviceCheckRuleList) {
            Future future = service.submit(new ServiceCheckRuleThread(jsonObject, serviceCheckRule));
            futureList.add(future);
        }
        List serviceCheckRuleResultList = new ArrayList<>();
        for (int i = 0; i < serviceCheckRuleList.size(); i++) {
            if (takeServiceCheckRuleResult(checkMode, service, futureList, serviceCheckRuleResultList)) {
                return serviceCheckRuleResultList;
            }
        }
        return serviceCheckRuleResultList;
    }

    private static boolean takeServiceCheckRuleResult(CheckMode checkMode, CompletionService service, List> futureList, List serviceChcekRuleResultList) throws BizException {
        Future take = null;
        try {
            take = service.take();
        } catch (InterruptedException e) {
            log.error("服务校验计算被中断:",e);
            Thread.currentThread().interrupt();
            throw new BizException(BizResultEnum.CHECKRULE_SERVICE_CHECK_THREAD_ERROR, e);
        }
        ServiceChcekRuleResult serviceChcekRuleResult = null;
        try {
            serviceChcekRuleResult = take.get();
        } catch (InterruptedException e) {
            log.error("服务校验计算被中断:",e);
            Thread.currentThread().interrupt();
            throw new BizException(BizResultEnum.CHECKRULE_SERVICE_CHECK_THREAD_ERROR, e);
        } catch (ExecutionException e) {
            throw new BizException(BizResultEnum.CHECKRULE_SERVICE_CHECK_THREAD_ERROR, e);
        }
        if (serviceChcekRuleResult.getResult() != null) {
            if (checkMode == CheckMode.ONE) {
                for (Future future : futureList) {
                    future.cancel(true);
                }
                serviceChcekRuleResultList.add(serviceChcekRuleResult);
                return true;
            }
            else {
                serviceChcekRuleResultList.add(serviceChcekRuleResult);
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy