为什么openoffice实现doc转pdf一个线程可以转换成功,两个人同时转换Linux就死机呢,在线等

xhbe 2014-12-22 01:51:54
package com.jmw.utils;

import java.io.File;
import java.util.Date;

import org.apache.log4j.Logger;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

public class Doc2PdfUtil extends Thread{
public static enum STATUS {
SUCCESS, FAIL, NOINSTALL
};

private static OfficeManager officeManager;
//private static String OFFICE_HOME = "C:\\Program Files\\OpenOffice.org 3";
private static String OFFICE_HOME = ConstantPath.OfficeHome;
//private static int port[] = {7605,7606,7607};
private static int port[] = {7605,7606};

public static STATUS convert2PDF(File inputFile, File pdfFile) {

Date start = new Date();

try{
System.setProperty("office.home", OFFICE_HOME);
startService();
Logger.getLogger(Doc2PdfUtil.class).info("进行文档转换:" + inputFile.getName() );
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(inputFile, pdfFile);
}catch (Exception e) {
System.out.println("convert2PDF Message = " + e.getMessage() + " Cause = " + e.getCause());
return STATUS.NOINSTALL;
}finally{
stopService();
}

long l = (start.getTime() - new Date().getTime());
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

System.out.println("convert2PDF doc 转 pdf 生成耗费:" + min + "分" + s + "秒");
Logger.getLogger(Doc2PdfUtil.class).info("生成" + pdfFile.getName() + "耗费:" + min + "分" + s + "秒");
if (pdfFile.exists()) {
return STATUS.SUCCESS;
} else {
return STATUS.FAIL;
}
}

public static void startService() {
System.out.println("调用Doc2PdfUtil startService....");

DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();

try {
configuration.setOfficeHome(OFFICE_HOME);// 设置OpenOffice.org安装目录
configuration.setPortNumbers(port); // 设置转换端口,默认为8100
configuration.setMaxTasksPerProcess(2);

configuration.setTaskExecutionTimeout(1000 * 60 * 3L);// 设置任务执行超时3分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时24小时

System.out.println("officeManager start configuration.toString =" + configuration.toString());

officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
}catch(IllegalStateException se){
System.out.println(" startService Message1 = " + se.getMessage() + " Cause2 = " + se.getCause() + " LocalizedMessage() = " + se.getLocalizedMessage());
se.printStackTrace();
} catch (Exception ce) {
System.out.println("office转换服务启动失败!详细信息:" + ce);
}
}

public static void stopService() {
System.out.println("关闭office转换服务....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("关闭office转换成功!");
}
}
...全文
559 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序男 2016-09-30
  • 打赏
  • 举报
回复
有道理。。。
baidu_25714341 2015-01-30
  • 打赏
  • 举报
回复
两个人同时转的时候,有可能第一个人启动了服务,第二人再启动服务的时候就报错,如果有并发操作的话最好不要用这种方式
xhbe 2014-12-22
  • 打赏
  • 举报
回复
用的是jodconverter-core-3.0-beta-4
冥王之锤 2014-12-22
  • 打赏
  • 举报
回复
用的哪个版本的jodconverter?我没找到你的这个库。 是否两个线程同时写了一个文件?又没有特殊处理。这是不允许的。
阿良良木月火 2014-12-22
  • 打赏
  • 举报
回复
没研究过这个呢
xhbe 2014-12-22
  • 打赏
  • 举报
回复
来者有分,在线等

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧