| | |
| | | public class MdmEngineServiceImpl implements MdmEngineService { |
| | | |
| | | /** |
| | | * 多线程方式批量执行开启线程池的总数 |
| | | */ |
| | | @Value("${batchadd.thread_num:10}") |
| | | private Integer THREAD_NUM; |
| | | |
| | | /** |
| | | * 单次sql的最多导入数量 |
| | | */ |
| | | @Value("${batchadd.single_maxnum:500}") |
| | | @Value("${batchadd.single_maxnum:200}") |
| | | private Integer MAX_IMPORT_NUM; |
| | | |
| | | /** |
| | | * 是否开启多线程方式导入历史数据 |
| | | */ |
| | | @Value("${batchadd.is_thread_import:false}") |
| | | private boolean IS_THREAD_IMPORT; |
| | | |
| | | /** |
| | | * 模板的服务 |
| | |
| | | * @param orderDTO 申请的信息,需要包含属性的内容和码段相关的内容 |
| | | * @return 返回编码的内容 |
| | | */ |
| | | private String addSaveCode(CodeOrderDTO orderDTO,boolean authUser) throws Exception { |
| | | private String addSaveCode(CodeOrderDTO orderDTO, boolean authUser) throws Exception { |
| | | VciBaseUtil.alertNotNull(orderDTO, "编码申请相关的属性和码段的内容都为空", orderDTO.getCodeClassifyOid(), "主题库分类的主键", |
| | | orderDTO.getTemplateOid(), "模板的主键", orderDTO.getCodeRuleOid(), "编码规则的主键"); |
| | | CodeClassifyFullInfoBO classifyFullInfo = classifyService.getClassifyFullInfo(orderDTO.getCodeClassifyOid()); |
| | |
| | | * @return 处理成功数据条数 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Integer insertBatchByType(String btmType, List<BaseModel> baseModels) { |
| | | //使用传入的业务类型查询表 |
| | | R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(btmType)); |
| | |
| | | throw new VciBaseException("类型转换错误:" + e.toString()); |
| | | } |
| | | }); |
| | | try { |
| | | bactchExecuteInsert(listR.getData().get(0).getTableName(),maps); |
| | | }catch (Exception e){ |
| | | throw new ServiceException("分批执行insert语句报错:"+e.getMessage()); |
| | | // 是否开启多线程执行插入语句 |
| | | if(IS_THREAD_IMPORT){ |
| | | try { |
| | | threadBactchExecuteInsert(listR.getData().get(0).getTableName(),maps); |
| | | }catch (Exception e){ |
| | | throw new ServiceException("分批执行insert语句报错:"+e.getMessage()); |
| | | } |
| | | }else { |
| | | bacthExcecuteInsert(listR.getData().get(0).getTableName(),maps); |
| | | } |
| | | |
| | | return maps.size(); |
| | | } |
| | | |
| | | /** |
| | | * 分批执行insert语句 |
| | | * 多线程方式分批执行insert语句 |
| | | * @param tableName |
| | | * @param maps |
| | | * @throws ServiceException |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | void bactchExecuteInsert(String tableName, List<Map<String, String>> maps) throws ServiceException{ |
| | | ExecutorService executor = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池 |
| | | private void threadBactchExecuteInsert(String tableName, List<Map<String, String>> maps) throws ServiceException{ |
| | | ExecutorService executor = Executors.newFixedThreadPool(THREAD_NUM); // 创建一个固定大小的线程池 |
| | | List<Map<String, String>> threadSafeMaps = new CopyOnWriteArrayList<>(maps); |
| | | |
| | | for (int i = 0; i < threadSafeMaps.size(); i += MAX_IMPORT_NUM) { |
| | | final int startIndex = i; |
| | | final int endIndex = Math.min(i + MAX_IMPORT_NUM, maps.size()); |
| | | final int endIndex = Math.min(i + MAX_IMPORT_NUM, threadSafeMaps.size()); |
| | | |
| | | executor.execute(() -> { |
| | | List<Map<String, String>> subList = threadSafeMaps.subList(startIndex, endIndex); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 单线程方式分批执行 |
| | | * @param tableName |
| | | * @param maps |
| | | */ |
| | | private void bacthExcecuteInsert(String tableName, List<Map<String, String>> maps){ |
| | | for (int i = 0; i < maps.size(); i += MAX_IMPORT_NUM) { |
| | | final int startIndex = i; |
| | | final int endIndex = Math.min(i + MAX_IMPORT_NUM, maps.size()); |
| | | List<Map<String, String>> subList = maps.subList(startIndex, endIndex); |
| | | // 调用插入数据库的方法 |
| | | commonsMapper.insertByBaseModel(tableName, maps.get(0), subList); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 传入业务类型以及ID查询业务表数据是否重复 |
| | | * |
| | | * @param btmType 业务类型 |