在使用jodconverter进行pdf转换的时候,按默认的配置,请求量一上来就报错"org.jodconverter.office.OfficeException: No office manager available."

异常

2017-06-27 17:39:03.136  INFO 8 --- [ProcessThread-0] o.j.office.ManagedOfficeProcess          : process forcibly terminated with code 81
2017-06-27 17:52:55.067  INFO 8 --- [ustom-tomcat-27] o.j.office.ManagedOfficeProcess          : Executing task 'Restart After Timeout'...
2017-06-27 17:52:55.067  INFO 8 --- [ProcessThread-0] org.jodconverter.office.OfficeProcess    : Trying to forcibly terminate process: 'host=127.0.0.1,port=2002,tcpNoDelay=1' (pid -2)
2017-06-27 17:52:55.069 ERROR 8 --- [ustom-tomcat-27] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.jodconverter.office.OfficeException: task did not complete within timeout] with root cause

java.util.concurrent.TimeoutException: null
    at java.util.concurrent.FutureTask.get(FutureTask.java:205) ~[na:1.8.0_131]
    at org.jodconverter.office.PooledOfficeManager.execute(PooledOfficeManager.java:161) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    at org.jodconverter.office.ProcessPoolOfficeManager.execute(ProcessPoolOfficeManager.java:117) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    at org.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:130) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    at org.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:103) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    at org.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:84) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    
2017-06-27 17:52:55.072  INFO 8 --- [ProcessThread-0] o.j.office.ManagedOfficeProcess          : process forcibly terminated with code 81
2017-06-27 18:05:53.759 ERROR 8 --- [ustom-tomcat-25] c.g.b.pdf.controller.ConvertController   : failed conversion:  [15563b] to pdf; org.jodconverter.office.OfficeException: No office manager available.; input file: file4579207008092030803.
2017-06-27 18:05:53.762 ERROR 8 --- [ustom-tomcat-25] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [conversion failed] with root cause

org.jodconverter.office.OfficeException: No office manager available.
    at org.jodconverter.office.ProcessPoolOfficeManager.execute(ProcessPoolOfficeManager.java:115) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    at org.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:130) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    at org.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:103) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]
    at org.jodconverter.OfficeDocumentConverter.convert(OfficeDocumentConverter.java:84) ~[jodconverter-core-4.0.0-RELEASE.jar!/:4.0.0-RELEASE]

源码

参数

jodconverter-core-4.0.0-RELEASE-sources.jar!/org/jodconverter/office/DefaultOfficeManagerBuilder.java

final int numInstances =
        connectionProtocol == OfficeConnectionProtocol.PIPE ? pipeNames.length : portNumbers.length;
    final UnoUrl[] unoUrls = new UnoUrl[numInstances];
    for (int i = 0; i < numInstances; i++) {
      unoUrls[i] =
          (connectionProtocol == OfficeConnectionProtocol.PIPE)
              ? UnoUrlUtils.pipe(pipeNames[i])
              : UnoUrlUtils.socket(portNumbers[i]);
    }

构造器

 /** Constructs a new instance of the class with the specified settings. */
  public ProcessPoolOfficeManager(
      final UnoUrl[] unoUrls,
      final File officeHome,
      final File workingDir,
      final ProcessManager processManager,
      final String[] runAsArgs,
      final File templateProfileDir,
      final long retryTimeout,
      final long retryInterval,
      final boolean killExistingProcess,
      final long taskQueueTimeout,
      final long taskExecutionTimeout,
      final int maxTasksPerProcess) {

    this.taskQueueTimeout = taskQueueTimeout;
    pool = new ArrayBlockingQueue<>(unoUrls.length);
    pooledManagers = new PooledOfficeManager[unoUrls.length];
    for (int i = 0; i < unoUrls.length; i++) {
      final PooledOfficeManagerSettings settings =
          new PooledOfficeManagerSettings(unoUrls[i], officeHome, workingDir, processManager);
      settings.setRunAsArgs(runAsArgs);
      settings.setTemplateProfileDir(templateProfileDir);
      settings.setRetryTimeout(retryTimeout);
      settings.setRetryInterval(retryInterval);
      settings.setKillExistingProcess(killExistingProcess);
      settings.setTaskExecutionTimeout(taskExecutionTimeout);
      settings.setMaxTasksPerProcess(maxTasksPerProcess);
      pooledManagers[i] = new PooledOfficeManager(settings);
    }
    logger.debug(
        "ProcessManager implementation is '{}'", processManager.getClass().getSimpleName());
  }

解决

jodconverter:
  enabled: true
  portNumbers: 2002,2003,2004,2005,2006
  maxTasksPerProcess: 1000
  taskQueueTimeout: 30

通过设置多个port,可以启动多个office
通过设置maxTasksPerProcess,可以增加队列容量


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...