Pay attention to the red font part
Business requirements: Data may come from different data sources.mysql, oracle, or remote interface (divided into two implementation classes: data and api)
useThread poolCall
1.service layer core code:
@Autowired
//The implementation class of data and api, inherits AbstractDataHanderService, AbstractDataHanderService is an abstract class,
private Map<String,AbstractDataHanderService> handerServiceMap;
private ThreadPoolExecutor threadPool;
//The following initializes the thread pool
@PostConstruct
private void initThreadPool(){
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("engine-%d").setDaemon(true).build();
threadPool = new ThreadPoolExecutor(20,32,50L,,new LinkedBlockingQueue<Runnable>(32),threadFactory);
();
}
@Override
public List<IndicatorDataRespDTO> getIndicatorData(IndicatorReqDTO reqDTO,userCode){
.....Omit some code
List<IndicatorDataDTO> dataDtoList = new ArrayList<>(); // Returned result
List<Future<List<IndicatorDataDTO>>> futures = new ArrayList<>(); //The thread with return result is received with future
for(){
//Transfer the calling data or API implementation class, () is to get the implementation service,
//The getHandlerList method encapsulates the object to be executed
List<AbstractGetDataHandler> handerList = (()+"_dataHandlerService")
.getHandlerList(reqDTO,(),getGroupReqFields());
((handlerList));//This step is the real execution, and the method in the encapsulation object was executed in the previous step
}
//Merge result data
for(Future<List<IndicatorDataDTO>> future:futures){
List<IndicatorDataDTO> resultDataList = ();
if(!(resultDataList)){
(resultDataList);
}
}
}
2.Note the execution object encapsulated by the previous code List<AbstractGetDataHandler> handerList =
and List<Future<List<IndicatorDataDTO>>> futures = new ArrayList<>(); //The thread with return result receives it with future
AbstractGetDataHandler Abstract class:Note that implements Callable<List<IndicatorDataDTO>>
@Slf4j
@Data
public abstract class AbstractGetDataHandler implements Callable<List<IndicatorDataDTO>>{
public abstract List<IndicatorDataDTO> getIndicatorData(); //Processing business in the implementation of this method,
//Note that the return of this method List<IndicatorDataDTO> and List<Future<List<IndicatorDataDTO>>>> futures = new ArrayList<>();
}
3.Implement the core code of the getHandlerList method in the class
//Return List<AbstractGetDataHandler> dataHandlerList
//Note here that DataBaseGetDataHandler inherits the above AbstractGetDataHandler and implements the getIndicatorData method
//
(new DataBaseGetDataHandler(jdbcTemplate,paramMap,dataPath,type,engineDbConfig));
4.DataBaseGetDataHandler
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DataBaseGetDataHandler extends AbstractGetDataHander{
//Implementation getIndicatorData, business processing here
@Override
public List<IndicatorDataDTO> getIndicatorData(){
。。。。。。
}
// Pay attention to the following method, the key point!
@Override
public List<IndicatorDataDTO> call() thows Exception{
return getIndicatorData
}
}