Parcourir la source

Office组件启动异步化,提速应用启动速度到5秒内

chenkailing il y a 4 ans
Parent
commit
37c37868a3

+ 14 - 5
jodconverter-web/src/main/java/cn/keking/utils/ConverterUtils.java → jodconverter-web/src/main/java/cn/keking/service/OfficeProcessManager.java

@@ -1,4 +1,4 @@
-package cn.keking.utils;
+package cn.keking.service;
 
 import com.sun.star.document.UpdateDocMode;
 import cn.keking.extend.ControlDocumentFormatRegistry;
@@ -9,6 +9,8 @@ import org.artofsolving.jodconverter.office.OfficeManager;
 import org.artofsolving.jodconverter.office.OfficeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.Ordered;
+import org.springframework.core.annotation.Order;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
@@ -29,16 +31,23 @@ import java.util.Properties;
  * @date 2017/11/13
  */
 @Component
-public class ConverterUtils {
+@Order(Ordered.HIGHEST_PRECEDENCE)
+public class OfficeProcessManager {
 
-    private final Logger logger = LoggerFactory.getLogger(ConverterUtils.class);
+    private final Logger logger = LoggerFactory.getLogger(OfficeProcessManager.class);
 
     private OfficeManager officeManager;
 
     @PostConstruct
     public void initOfficeManager() {
-        File officeHome;
-        officeHome = OfficeUtils.getDefaultOfficeHome();
+        new Thread(this::startOfficeManager).start();
+    }
+
+    /**
+     * 启动Office组件进程
+     */
+    private void startOfficeManager(){
+        File officeHome = OfficeUtils.getDefaultOfficeHome();
         if (officeHome == null) {
             throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
         }

+ 6 - 6
jodconverter-web/src/main/java/cn/keking/utils/OfficeToPdf.java → jodconverter-web/src/main/java/cn/keking/service/OfficeToPdfService.java

@@ -1,4 +1,4 @@
-package cn.keking.utils;
+package cn.keking.service;
 
 import org.artofsolving.jodconverter.OfficeDocumentConverter;
 import org.springframework.stereotype.Component;
@@ -9,11 +9,11 @@ import java.io.File;
  * @author yudian-it
  */
 @Component
-public class OfficeToPdf {
-    private final ConverterUtils converterUtils;
+public class OfficeToPdfService {
+    private final OfficeProcessManager officeProcessManager;
 
-    public OfficeToPdf(ConverterUtils converterUtils) {
-        this.converterUtils = converterUtils;
+    public OfficeToPdfService(OfficeProcessManager officeProcessManager) {
+        this.officeProcessManager = officeProcessManager;
     }
 
     public void openOfficeToPDF(String inputFilePath, String outputFilePath) {
@@ -33,7 +33,7 @@ public class OfficeToPdf {
 
 
     public void office2pdf(String inputFilePath, String outputFilePath) {
-        OfficeDocumentConverter converter = converterUtils.getDocumentConverter();
+        OfficeDocumentConverter converter = officeProcessManager.getDocumentConverter();
         if (null != inputFilePath) {
             File inputFile = new File(inputFilePath);
             // 判断目标文件路径是否为空

+ 5 - 5
jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java

@@ -6,7 +6,7 @@ import cn.keking.model.ReturnResponse;
 import cn.keking.service.FilePreview;
 import cn.keking.utils.DownloadUtils;
 import cn.keking.utils.FileUtils;
-import cn.keking.utils.OfficeToPdf;
+import cn.keking.service.OfficeToPdfService;
 import cn.keking.utils.PdfUtils;
 import cn.keking.web.filter.BaseUrlFilter;
 import org.springframework.stereotype.Service;
@@ -25,13 +25,13 @@ public class OfficeFilePreviewImpl implements FilePreview {
     private final FileUtils fileUtils;
     private final PdfUtils pdfUtils;
     private final DownloadUtils downloadUtils;
-    private final OfficeToPdf officeToPdf;
+    private final OfficeToPdfService officeToPdfService;
 
-    public OfficeFilePreviewImpl(FileUtils fileUtils, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdf officeToPdf) {
+    public OfficeFilePreviewImpl(FileUtils fileUtils, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) {
         this.fileUtils = fileUtils;
         this.pdfUtils = pdfUtils;
         this.downloadUtils = downloadUtils;
-        this.officeToPdf = officeToPdf;
+        this.officeToPdfService = officeToPdfService;
     }
 
     public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
@@ -59,7 +59,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
             }
             filePath = response.getContent();
             if (StringUtils.hasText(outFilePath)) {
-                officeToPdf.openOfficeToPDF(filePath, outFilePath);
+                officeToPdfService.openOfficeToPDF(filePath, outFilePath);
                 if (isHtml) {
                     // 对转换后的文件进行操作(改变编码方式)
                     fileUtils.doActionConvertedFile(outFilePath);