线程池&CountDownLatch组合

package com.bosssoft.bigdata.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.bigdata.admin.api.entity.ExternalInterface;
import com.bosssoft.bigdata.admin.api.vo.ExternalInterfaceStateVO;
import com.bosssoft.bigdata.admin.monitor.ExternalInterfaceConnnectFactory;
import com.bosssoft.bigdata.admin.mapper.ExternalInterfaceMapper;
import com.bosssoft.bigdata.admin.monitor.ExternalInterfaceConnect;
import com.bosssoft.bigdata.admin.service.ExternalInterfaceService;
import com.bosssoft.bigdata.common.core.utils.CollectionUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * 
 * @author daocaor
 * @date 2019-08-27 15:44:38
 */
@Service
@Slf4j
public class ExternalInterfaceServiceImpl extends ServiceImpl<ExternalInterfaceMapper, ExternalInterface> implements ExternalInterfaceService {

    private static ExecutorService taskPool = Executors.newFixedThreadPool(8);

    @Override
    public List<ExternalInterfaceStateVO> states() {
        List<ExternalInterface> taskList = this.list(new QueryWrapper<ExternalInterface>().eq("state",1));
        List<ExternalInterfaceStateVO> externalInterfaceStates = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(taskList)){
            try {
                CountDownLatch latch=new CountDownLatch(taskList.size());
                for(ExternalInterface externalInterface : taskList){
                   RunnerTask runnerTask = new RunnerTask(latch,externalInterface,externalInterfaceStates);
                   taskPool.execute(runnerTask);
                }
                latch.await();
                Collections.sort(externalInterfaceStates);
            }catch (Exception e ){
                e.printStackTrace();
            }
        }
        return externalInterfaceStates;
    }
}



@Data
@AllArgsConstructor
@Slf4j
class RunnerTask implements Runnable{
    private CountDownLatch latch;
    private ExternalInterface externalInterface ;
    private List<ExternalInterfaceStateVO> list;

    @Override
    public void run() {
       try {
         ExternalInterfaceConnect externalInterfaceConnect =  ExternalInterfaceConnnectFactory.build(externalInterface.getType());
         JSONObject configParams = JSONObject.parseObject(externalInterface.getConfigParams());
         this.addItem(externalInterfaceConnect.connect(this.externalInterface.getUrl(),configParams));
       }catch (Exception e){
           e.printStackTrace();
           log.error("\n【 请求接口 {} 报错】 \n接口链接: {},\n参数:{},\n报错信息",
                   this.externalInterface.getName(),this.externalInterface.getUrl(),this.externalInterface.getConfigParams(),e.getMessage());
       }finally {
           if(this.latch != null ){
               this.latch.countDown();
           }
       }
    }

    /**
     * list add 线程不安全
     * @param externalInterfaceState
     */
    public synchronized void addItem(ExternalInterfaceStateVO externalInterfaceState){
        externalInterfaceState.setName(this.externalInterface.getName());
        this.list.add(externalInterfaceState);
    }
}

你可能感兴趣的:(Java多线程)