!49 修复Linux环境中OfficePluginManager中KillProcess出现杀掉进程失败,导致二次启动失败的bug
* 修复Linux环境中OfficePluginManager中KillProcess出现杀掉已经存在进程失败,导致二次启动失败的bug
This commit is contained in:
@ -6,6 +6,7 @@ import org.artofsolving.jodconverter.OfficeDocumentConverter;
|
|||||||
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
|
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
|
||||||
import org.artofsolving.jodconverter.office.OfficeManager;
|
import org.artofsolving.jodconverter.office.OfficeManager;
|
||||||
import org.artofsolving.jodconverter.office.OfficeUtils;
|
import org.artofsolving.jodconverter.office.OfficeUtils;
|
||||||
|
import org.artofsolving.jodconverter.util.PlatformUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -24,7 +25,6 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建文件转换器
|
* 创建文件转换器
|
||||||
@ -50,7 +50,7 @@ public class OfficePluginManager {
|
|||||||
* 启动Office组件进程
|
* 启动Office组件进程
|
||||||
*/
|
*/
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void startOfficeManager(){
|
public void startOfficeManager() {
|
||||||
File officeHome = OfficeUtils.getDefaultOfficeHome();
|
File officeHome = OfficeUtils.getDefaultOfficeHome();
|
||||||
if (officeHome == null) {
|
if (officeHome == null) {
|
||||||
throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
|
throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
|
||||||
@ -62,7 +62,7 @@ public class OfficePluginManager {
|
|||||||
try {
|
try {
|
||||||
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
|
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
|
||||||
configuration.setOfficeHome(officeHome);
|
configuration.setOfficeHome(officeHome);
|
||||||
String []portsString = serverPorts.split(",");
|
String[] portsString = serverPorts.split(",");
|
||||||
|
|
||||||
int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray();
|
int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray();
|
||||||
|
|
||||||
@ -86,8 +86,8 @@ public class OfficePluginManager {
|
|||||||
return converter;
|
return converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String,?> getLoadProperties() {
|
private Map<String, ?> getLoadProperties() {
|
||||||
Map<String,Object> loadProperties = new HashMap<>(10);
|
Map<String, Object> loadProperties = new HashMap<>(10);
|
||||||
loadProperties.put("Hidden", true);
|
loadProperties.put("Hidden", true);
|
||||||
loadProperties.put("ReadOnly", true);
|
loadProperties.put("ReadOnly", true);
|
||||||
loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE);
|
loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE);
|
||||||
@ -97,9 +97,8 @@ public class OfficePluginManager {
|
|||||||
|
|
||||||
private boolean killProcess() {
|
private boolean killProcess() {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
Properties props = System.getProperties();
|
|
||||||
try {
|
try {
|
||||||
if (props.getProperty("os.name").toLowerCase().contains("windows")) {
|
if (PlatformUtils.isWindows()) {
|
||||||
Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
|
Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
InputStream os = p.getInputStream();
|
InputStream os = p.getInputStream();
|
||||||
@ -112,8 +111,22 @@ public class OfficePluginManager {
|
|||||||
Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f");
|
Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f");
|
||||||
flag = true;
|
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 {
|
} 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();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
InputStream os = p.getInputStream();
|
InputStream os = p.getInputStream();
|
||||||
byte[] b = new byte[256];
|
byte[] b = new byte[256];
|
||||||
@ -122,7 +135,7 @@ public class OfficePluginManager {
|
|||||||
}
|
}
|
||||||
String s = baos.toString();
|
String s = baos.toString();
|
||||||
if (StringUtils.ordinalIndexOf(s, "soffice.bin", 3) > 0) {
|
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);
|
Runtime.getRuntime().exec(cmd);
|
||||||
flag = true;
|
flag = true;
|
||||||
}
|
}
|
||||||
@ -134,7 +147,7 @@ public class OfficePluginManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
public void destroyOfficeManager(){
|
public void destroyOfficeManager() {
|
||||||
if (null != officeManager && officeManager.isRunning()) {
|
if (null != officeManager && officeManager.isRunning()) {
|
||||||
logger.info("Shutting down office process");
|
logger.info("Shutting down office process");
|
||||||
officeManager.stop();
|
officeManager.stop();
|
||||||
|
|||||||
Reference in New Issue
Block a user