问题:OfficeException: failed to start and connect(一)
场景:重启tomcat服务后,预览系统启动不了
输出日志:启动office组件失败,请检查office组件是否可用
报错:org.artofsolving.jodconverter.office.OfficeException: failed to start and connect
认定原因一:
1、杀死进程杀死不成功和启动软件进程互相冲突
boolean killOffice = killProcess();
if (killOffice) {
logger.warn("检测到有正在运行的office进程,已自动结束该进程");
}
try {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setOfficeHome(officeHome);
configuration.setPortNumber(8100);
// 设置任务执行超时为25分钟
configuration.setTaskExecutionTimeout(1000 * 60 * 25L);
// 设置任务队列超时为24小时
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
configuration.setMaxTasksPerProcess(1000);
officeManager = configuration.buildOfficeManager();
officeManager.start();
} catch (Exception e) {
logger.error("启动office组件失败,请检查office组件是否可用");
throw e;
}
//杀死进程核心代码
String[] cmd ={"cmd","/c","taskkill /im soffice.bin /f"};
Process process= Runtime.getRuntime().exec(cmd);
//输出为1,可见杀死进程失败
logger.warn("killProcess:process"+process.waitFor());
解决方式一:
我将杀死soffice服务进程的代码注释,让其直接进行启动!
//boolean killOffice = killProcess();
//if (killOffice) {
// logger.warn("检测到有正在运行的office进程,已自动结束该进程");
//}
try {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
configuration.setOfficeHome(officeHome);
configuration.setPortNumber(8100);
// 设置任务执行超时为25分钟
configuration.setTaskExecutionTimeout(1000 * 60 * 25L);
// 设置任务队列超时为24小时
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
configuration.setMaxTasksPerProcess(1000);
officeManager = configuration.buildOfficeManager();
officeManager.start();
} catch (Exception e) {
logger.error("启动office组件失败,请检查office组件是否可用");
throw e;
}
发现它可以成功启动!又尝试在杀死后面加了个延时!
Thread.sleep(5000);
结果也是可以启动!
结论一:
所以最终问题在于杀死进程命令执行后,不能立马执行启动命令,会导致报错。