Преглед изворни кода

优化导出内存,避免内存耗尽GC失能

欧阳劲驰 пре 3 дана
родитељ
комит
d9073cf160

+ 4 - 6
src/main/java/com/shkpr/service/alambizplugin/commtools/ExcelUtils.java

@@ -9,6 +9,7 @@ import org.apache.commons.collections4.map.CaseInsensitiveMap;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 import java.io.IOException;
@@ -302,7 +303,7 @@ public class ExcelUtils {
     public static void writeFile(Map<String, Map<String, String>> headers, Map<String, List<Map<String, Object>>> datas
             , OutputStream outputStream, ExcelEnum excelEnum, Integer headerRowNum, Integer dataRowNum) {
         //创建表
-        try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new XSSFWorkbook() : new HSSFWorkbook()) {
+        try (Workbook workbook = excelEnum == ExcelEnum.XLSX ? new SXSSFWorkbook() : new HSSFWorkbook()) {
             //表头样式
             CellStyle headerStyle = workbook.createCellStyle();
             headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
@@ -323,6 +324,8 @@ public class ExcelUtils {
             }
             //写入文件
             workbook.write(outputStream);
+            //清理临时文件
+            if (workbook instanceof SXSSFWorkbook) ((SXSSFWorkbook) workbook).dispose();
         } catch (IOException e) {
             LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
                     , String.format("excel文件写入失败 msg:%s", e.getMessage())
@@ -406,11 +409,6 @@ public class ExcelUtils {
                 setCellValue(cell, item.get(headerKey));
             }
         }
-        //设置自动宽度
-        for (int i = 0; i < header.size(); i++) {
-            sheet.autoSizeColumn(i, true);
-            sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 12 / 10);
-        }
     }
 
     /**

+ 40 - 9
src/main/java/com/shkpr/service/alambizplugin/components/GisSurveyThirdExporter.java

@@ -38,6 +38,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.time.Duration;
 import java.time.LocalDateTime;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -45,6 +46,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -114,13 +116,24 @@ public class GisSurveyThirdExporter {
             //处理坐标系
             if (!Objects.equals(targetCRSCode, CommCRSDefine.CGCS2000)) transformGeom(targetCRSCode, points, lines);
 
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                    , String.format("开始构建模版查询条件;任务id: %s", jobId)
+            );
             //获取图层key
-            List<String> pointLayer = getLayerKeys(points);
-            List<String> lineLayer = getLayerKeys(lines);
+            Set<String> pointLayer = getLayerKeys(points);
+            Set<String> lineLayer = getLayerKeys(lines);
+
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                    , String.format("开始查询模版;任务id: %s", jobId)
+            );
             //查询点图层模版
-            List<GisMetadataLayerTemplate> pointLayerTemplates = layerTemplateService.findByKeyIn(pointLayer);
+            List<GisMetadataLayerTemplate> pointLayerTemplates = layerTemplateService.findByKeyIn(new ArrayList<>(pointLayer));
             //查询线图层模版
-            List<GisMetadataLayerTemplate> lineLayerTemplates = layerTemplateService.findByKeyIn(lineLayer);
+            List<GisMetadataLayerTemplate> lineLayerTemplates = layerTemplateService.findByKeyIn(new ArrayList<>(lineLayer));
+
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                    , String.format("查询模版完毕,开始导出;任务id: %s", jobId)
+            );
 
             //结果flag
             final String FLAG = RESULT_PREFIX + jobId;
@@ -256,8 +269,18 @@ public class GisSurveyThirdExporter {
         //构建excel数据
         Map<String, List<Map<String, Object>>> excelData = buildExcelData(points, lines, pointLayerTemplates, lineLayerTemplates);
 
+        //清除查询的数据
+        points.clear();
+        lines.clear();
+
         //导出excel
-        return asyncResultManager.writeExcel(excelHeader, excelData, FLAG, "third-export.xlsx");
+        Path result = asyncResultManager.writeExcel(excelHeader, excelData, FLAG, "third-export.xlsx");
+
+        //清除shape数据
+        excelHeader.clear();
+        excelData.clear();
+
+        return result;
     }
 
     /**
@@ -276,8 +299,17 @@ public class GisSurveyThirdExporter {
         //构建shape数据
         Map<SimpleFeatureType, List<Map<String, Object>>> shapeData = buildShapeData(points, lines, pointLayerTemplates, lineLayerTemplates);
 
+        //清除查询的数据
+        points.clear();
+        lines.clear();
+
         //导出shape
-        return asyncResultManager.writeShape(shapeData, FLAG, "third-export.zip");
+        Path result = asyncResultManager.writeShape(shapeData, FLAG, "third-export.zip");
+
+        //清除shape数据
+        shapeData.clear();
+
+        return result;
     }
 
 
@@ -287,13 +319,12 @@ public class GisSurveyThirdExporter {
      * @param datas 数据
      * @return 图层key集合
      */
-    private List<String> getLayerKeys(List<GisSurveyLayerApply> datas) {
+    private Set<String> getLayerKeys(List<GisSurveyLayerApply> datas) {
         //去重图层
         return datas.parallelStream()
                 .map(GisSurveyLayerApply::getLayer)
                 .filter(StringUtils::isNotBlank)
-                .distinct()
-                .collect(Collectors.toList());
+                .collect(Collectors.toSet());
     }
 
     /**