org.shoulder.batch.service.impl.DefaultBatchExportService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shoulder-batch Show documentation
Show all versions of shoulder-batch Show documentation
Shoulder 扩展-批处理模块,提供批量数据导入、导出、异步校验、导入历史记录管理等能力。
The newest version!
package org.shoulder.batch.service.impl;
import com.univocity.parsers.common.record.Record;
import org.apache.commons.collections4.CollectionUtils;
import org.shoulder.batch.config.ExportConfigManager;
import org.shoulder.batch.config.model.ExportColumnConfig;
import org.shoulder.batch.config.model.ExportFileConfig;
import org.shoulder.batch.constant.BatchConstants;
import org.shoulder.batch.enums.BatchDetailResultStatusEnum;
import org.shoulder.batch.enums.BatchErrorCodeEnum;
import org.shoulder.batch.enums.BatchI18nEnum;
import org.shoulder.batch.log.ShoulderBatchLoggers;
import org.shoulder.batch.model.BatchData;
import org.shoulder.batch.model.BatchRecord;
import org.shoulder.batch.model.BatchRecordDetail;
import org.shoulder.batch.progress.BatchProgressCache;
import org.shoulder.batch.progress.BatchProgressRecord;
import org.shoulder.batch.progress.Progress;
import org.shoulder.batch.repository.BatchRecordDetailPersistentService;
import org.shoulder.batch.repository.BatchRecordPersistentService;
import org.shoulder.batch.service.BatchAndExportService;
import org.shoulder.batch.service.BatchOutputContext;
import org.shoulder.batch.spi.BatchTaskSliceHandler;
import org.shoulder.batch.spi.DataExporter;
import org.shoulder.core.context.AppContext;
import org.shoulder.core.dto.response.PageResult;
import org.shoulder.core.exception.BaseRuntimeException;
import org.shoulder.core.exception.CommonErrorCodeEnum;
import org.shoulder.core.i18.Translator;
import org.shoulder.core.log.Logger;
import org.shoulder.core.util.ArrayUtils;
import org.shoulder.core.util.AssertUtils;
import org.shoulder.core.util.JsonUtils;
import org.shoulder.core.util.StringUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* 处理导出门面
*
* @author lym
*/
public class DefaultBatchExportService implements BatchAndExportService {
private final Logger log = ShoulderBatchLoggers.DEFAULT;
/**
* 默认的最大同时执行任务数,cpu 的两倍
*/
private static final int DEFAULT_MAX_CONCURRENT_PROCESSOR = Runtime.getRuntime().availableProcessors() << 1;
/**
* 批处理线程池
*/
protected final ThreadPoolExecutor batchThreadPool;
protected final Translator translator;
/**
* 所有导出器实现
*/
protected final List dataExporterList;
/**
* 批量处理记录
*/
protected final BatchRecordPersistentService batchRecordPersistentService;
/**
* 处理详情
*/
protected final BatchRecordDetailPersistentService batchRecordDetailPersistentService;
protected final BatchProgressCache batchProgressCache;
protected final ExportConfigManager exportConfigManager;
// ---------------------------------------
public DefaultBatchExportService(ThreadPoolExecutor batchThreadPool, Translator translator, List dataExporterList,
BatchRecordPersistentService batchRecordPersistentService,
BatchRecordDetailPersistentService batchRecordDetailPersistentService,
BatchProgressCache batchProgressCache, ExportConfigManager exportConfigManager) {
this.batchThreadPool = batchThreadPool;
this.translator = translator;
this.dataExporterList = dataExporterList;
this.batchRecordPersistentService = batchRecordPersistentService;
this.batchRecordDetailPersistentService = batchRecordDetailPersistentService;
this.batchProgressCache = batchProgressCache;
this.exportConfigManager = exportConfigManager;
}
// **************************** 导出 *******************************
/**
* 导出
*
* @param outputStream 输出流
* @param templateId 导出数据模板标识
* @param dataSupplierList 导出数据
* @throws IOException IO
*/
@Override
public String export(OutputStream outputStream, String exportType,
List>>> dataSupplierList,
String templateId) throws IOException {
// 初始化线程变量
DataExporter dataExporter = dataExporterList.stream()
.filter(exporter -> exporter.support(exportType))
.findFirst().orElseThrow(() -> BatchErrorCodeEnum.EXPORT_TYPE_NOT_SUPPORT.toException(exportType));
BatchOutputContext.get().setCurrentDataExporter(dataExporter);
log.debug("find exporter {}", dataExporter);
ExportFileConfig exportFileConfig = exportConfigManager.getFileConfigWithLocale(templateId, AppContext.getLocaleOrDefault());
if (exportFileConfig == null) {
// 编码问题,未提供配置,需先调用 ExportConfigManager.putConfig 方法设置输出配置
throw new BaseRuntimeException("templateId:" + templateId + " not existed! ");
}
BatchOutputContext.get().setExportConfig(exportFileConfig);
try {
// 准备输出
dataExporter.prepare(outputStream, exportFileConfig);
// 输出头部信息
outputCommentLinesAndHeaders();
log.trace("output headers finished.");
// 输出数据
log.debug("output data total turn: {}", dataSupplierList.size());
for (int i = 0; i < dataSupplierList.size(); i++) {
Supplier>> dataSupplier = dataSupplierList.get(i);
List