Quellcode durchsuchen

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

欧阳劲驰 vor 2 Tagen
Ursprung
Commit
0f870507fe

+ 1 - 1
pom.xml

@@ -23,7 +23,7 @@
         <fasterxml.version>2.9.5</fasterxml.version><!-- 2.8.11.2 -->
         <fastercore.version>2.9.5</fastercore.version>
         <fasterannot.version>2.9.5</fasterannot.version>
-        <geotools.version>28.6</geotools.version>
+        <geotools.version>28.6.1</geotools.version>
     </properties>
 
     <!--工具包jar版本不一致,解决方案,以下配置促使本应用工程中的相应jar强制使用规定版本-->

+ 46 - 6
src/main/java/com/shkpr/service/alambizplugin/commtools/ShapeUtils.java

@@ -1,7 +1,10 @@
 package com.shkpr.service.alambizplugin.commtools;
 
+import com.global.base.log.LogLevelFlag;
+import com.global.base.log.LogPrintMgr;
 import com.shkpr.service.alambizplugin.constants.GisMetadataDefine;
 import com.shkpr.service.alambizplugin.constants.GisSurveyExcelDefine;
+import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
 import org.geotools.data.Transaction;
 import org.geotools.data.collection.ListFeatureCollection;
 import org.geotools.data.shapefile.ShapefileDataStore;
@@ -11,7 +14,7 @@ import org.geotools.feature.simple.SimpleFeatureBuilder;
 import org.opengis.feature.simple.SimpleFeatureType;
 
 import java.io.IOException;
-import java.io.Serializable;
+import java.nio.charset.Charset;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.HashMap;
@@ -27,8 +30,19 @@ import java.util.stream.Collectors;
  * @since 1.0.0
  */
 public class ShapeUtils {
-    //缓存库工厂
+    /**
+     * log
+     */
+    private static final String mStrClassName = "ShapeUtils";
+    private static final String mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
+    /**
+     * 缓存库工厂
+     */
     private static final ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
+    /**
+     * 编码
+     */
+    private static final Charset charset = Charset.forName("GB18030");
 
     /**
      * 输出shape文件
@@ -79,11 +93,17 @@ public class ShapeUtils {
      * @throws IOException io异常
      */
     public static void writeShape(final SimpleFeatureType TYPE, List<Map<String, Object>> data, Path filePath) throws IOException {
-        //特征构建
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                , String.format(
+                        "开始构建特征,类型:%s========>"
+                        , TYPE.getTypeName()
+                )
+        );
+        //根据类型创建特征构建器
         final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
         //特征集合
         ListFeatureCollection collection = new ListFeatureCollection(TYPE);
-        //遍历数据
+        //遍历数据,构建特征集合
         data.forEach(it -> {
             //设置geom
             featureBuilder.set(GisSurveyExcelDefine.FILE.THE_GEOM, it.get(GisSurveyExcelDefine.FILE.THE_GEOM));
@@ -93,15 +113,25 @@ public class ShapeUtils {
             });
             collection.add(featureBuilder.buildFeature(null));
         });
+        //释放数据
+        data.clear();
 
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                , String.format(
+                        "开启shapefile缓存库,开始写入,类型:%s========>"
+                        , TYPE.getTypeName()
+                )
+        );
         //从文件构建数据缓存库
-        Map<String, Serializable> params = new HashMap<>();
+        Map<String, Object> params = new HashMap<>();
         params.put(ShapefileDataStoreFactory.URLP.key, filePath.toUri().toURL());
         params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.TRUE);
-        params.put(ShapefileDataStoreFactory.DBFCHARSET.key, "GB18030");
+        params.put(ShapefileDataStoreFactory.MEMORY_MAPPED.key, Boolean.TRUE);
 
         ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createDataStore(params);
         dataStore.createSchema(TYPE);
+        dataStore.setCharset(charset);
+        dataStore.setTryCPGFile(true);
 
         //启动事务
         try (Transaction transaction = Transaction.AUTO_COMMIT) {
@@ -113,5 +143,15 @@ public class ShapeUtils {
             //写入特征集合
             featureStore.addFeatures(collection);
         }
+
+        //释放资源
+        collection.clear();
+        dataStore.dispose();
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                , String.format(
+                        "释放shapefile缓存库,结束写入,类型:%s========>"
+                        , TYPE.getTypeName()
+                )
+        );
     }
 }

+ 8 - 0
src/main/java/com/shkpr/service/alambizplugin/components/AsyncResultManager.java

@@ -305,6 +305,14 @@ public class AsyncResultManager {
             Path tempDirectory = Files.createTempDirectory(tempFileProperties.getResourcePath(), flag);
             //导出到shape
             ShapeUtils.writeShape(data, tempDirectory);
+
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
+                    , String.format(
+                            "开始执行压缩文件,结果标识:%s,文件名:%s"
+                            , flag
+                            , fileName
+                    )
+            );
             //压缩文件夹
             Path zipPath = CompressorUtils.archivalZip(tempDirectory);
             //移动文件