Browse Source

!49 修复Linux环境中OfficePluginManager中KillProcess出现杀掉进程失败,导致二次启动失败的bug
* 修复Linux环境中OfficePluginManager中KillProcess出现杀掉已经存在进程失败,导致二次启动失败的bug

tomhusky 2 years ago
parent
commit
3e43c2f9d0
1 changed files with 23 additions and 10 deletions
  1. 23 10
      server/src/main/java/cn/keking/service/OfficePluginManager.java

+ 23 - 10
server/src/main/java/cn/keking/service/OfficePluginManager.java

@@ -6,6 +6,7 @@ import org.artofsolving.jodconverter.OfficeDocumentConverter;
 import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
 import org.artofsolving.jodconverter.office.OfficeManager;
 import org.artofsolving.jodconverter.office.OfficeUtils;
+import org.artofsolving.jodconverter.util.PlatformUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
@@ -24,7 +25,6 @@ import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Properties;
 
 /**
  * 创建文件转换器
@@ -50,7 +50,7 @@ public class OfficePluginManager {
      * 启动Office组件进程
      */
     @PostConstruct
-    public void startOfficeManager(){
+    public void startOfficeManager() {
         File officeHome = OfficeUtils.getDefaultOfficeHome();
         if (officeHome == null) {
             throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
@@ -62,7 +62,7 @@ public class OfficePluginManager {
         try {
             DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
             configuration.setOfficeHome(officeHome);
-            String []portsString = serverPorts.split(",");
+            String[] portsString = serverPorts.split(",");
 
             int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray();
 
@@ -86,8 +86,8 @@ public class OfficePluginManager {
         return converter;
     }
 
-    private Map<String,?> getLoadProperties() {
-        Map<String,Object> loadProperties = new HashMap<>(10);
+    private Map<String, ?> getLoadProperties() {
+        Map<String, Object> loadProperties = new HashMap<>(10);
         loadProperties.put("Hidden", true);
         loadProperties.put("ReadOnly", true);
         loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE);
@@ -97,9 +97,8 @@ public class OfficePluginManager {
 
     private boolean killProcess() {
         boolean flag = false;
-        Properties props = System.getProperties();
         try {
-            if (props.getProperty("os.name").toLowerCase().contains("windows")) {
+            if (PlatformUtils.isWindows()) {
                 Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 InputStream os = p.getInputStream();
@@ -112,8 +111,22 @@ public class OfficePluginManager {
                     Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f");
                     flag = true;
                 }
+            } else if (PlatformUtils.isLinux()) {
+                Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", "ps -ef | grep " + "soffice.bin" + " |grep -v grep | wc -l"});
+                ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                InputStream os = p.getInputStream();
+                byte[] b = new byte[256];
+                while (os.read(b) > 0) {
+                    baos.write(b);
+                }
+                String s = baos.toString();
+                if (!"0".equals(s)) {
+                    String[] cmd = {"sh", "-c", "ps -ef | grep soffice.bin | grep -v grep | awk '{print \"kill -9 \"$2}' | sh"};
+                    Runtime.getRuntime().exec(cmd);
+                    flag = true;
+                }
             } else {
-                Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps -ef | grep " + "soffice.bin"});
+                Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", "ps -ef | grep " + "soffice.bin"});
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 InputStream os = p.getInputStream();
                 byte[] b = new byte[256];
@@ -122,7 +135,7 @@ public class OfficePluginManager {
                 }
                 String s = baos.toString();
                 if (StringUtils.ordinalIndexOf(s, "soffice.bin", 3) > 0) {
-                    String[] cmd ={"sh","-c","kill -15 `ps -ef|grep " + "soffice.bin" + "|awk 'NR==1{print $2}'`"};
+                    String[] cmd = {"sh", "-c", "kill -15 `ps -ef|grep " + "soffice.bin" + "|awk 'NR==1{print $2}'`"};
                     Runtime.getRuntime().exec(cmd);
                     flag = true;
                 }
@@ -134,7 +147,7 @@ public class OfficePluginManager {
     }
 
     @PreDestroy
-    public void destroyOfficeManager(){
+    public void destroyOfficeManager() {
         if (null != officeManager && officeManager.isRunning()) {
             logger.info("Shutting down office process");
             officeManager.stop();