Java 如何在服务器模式下将 OpenOffice 用作多线程服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/625241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:06:00  来源:igfitidea点击:

How can I use OpenOffice in server mode as a multithreaded service?

javamultithreadingopenoffice.org

提问by user63898

What is the experience of working with OpenOfficein server mode? I know OpenOffice is not multithreaded and now I need to use its services in our server.
What can I do to overcome this problem?

在服务器模式下使用OpenOffice是什么体验?我知道 OpenOffice 不是多线程的,现在我需要在我们的服务器中使用它的服务。
我能做些什么来克服这个问题?

I'm using Java.

我正在使用 Java。

采纳答案by vladr

Yes, I am using OpenOffice as a document conversion server.

是的,我使用 OpenOffice 作为文档转换服务器。

Unfortunately, the solution to your problem is to spawn a pool of OpenOffice processes.

不幸的是,您的问题的解决方案是生成一个 OpenOffice 进程池。

The commons-poolbranch of JODConverter (before it moved to code.google.com) implemented this out-of-the-box for you.

JODConverter的commons-pool分支(在移至 之前code.google.com)为您实现了这个开箱即用的功能。

回答by user77978

Vlad is correct about having to run multiple instances of OpenOffice on different ports.

关于必须在不同端口上运行多个 OpenOffice 实例,Vlad 是正确的。

I'd just like to add that OpenOffice doesn't seem to be stable. We run 10 instances of it in a production environment and set the code up to re-try with another instance if the first attempt fails. This way when one of the OpenOffice servers crashes (or doesn't crash but doesn't respond either) production is not affected. Since it's a pain to keep restarting the servers on a daily basis, we're slowly converting all our documents to JasperReports(see iReport for details). I'm not sure how you're using the OpenOffice server; we use it for mail merging (filling out forms for customers). If you need to convert things to PDF, I'd recommend iText.

我只想补充一点,OpenOffice 似乎并不稳定。我们在生产环境中运行它的 10 个实例,并设置代码以在第一次尝试失败时使用另一个实例重试。这样,当其中一台 OpenOffice 服务器崩溃(或没有崩溃但也没有响应)时,生产不会受到影响。由于每天重新启动服务器很痛苦,因此我们正在慢慢将所有文档转换为JasperReports(有关详细信息,请参阅 iReport)。我不确定您是如何使用 OpenOffice 服务器的;我们将其用于邮件合并(为客户填写表格)。如果您需要将内容转换为 PDF,我建议您使用iText

回答by Mercer Traieste

OpenOffice can be used in headless mode, but it has not been built to handle a lot of requests in a stressfull production environment.

OpenOffice 可以在无头模式下使用,但它尚未构建为在压力大的生产环境中处理大量请求。

Using OpenOffice in headless mode has several issues:

在无头模式下使用 OpenOffice 有几个问题:

  • The process might die/become unavailable.
  • There are several memory leaks issues.
  • Opening several OpenOffice "workers" does not scale as expected, and needs some tweaking to really have different open proccesses (having several OpenOffice copies, several services, running under different users.)
  • 该进程可能会死亡/变得不可用。
  • 有几个内存泄漏问题。
  • 打开多个 OpenOffice “workers”并没有按预期进行扩展,需要进行一些调整才能真正拥有不同的打开过程(具有多个 OpenOffice 副本、多个服务、在不同用户下运行。)

As suggested, jodconverter can be used to access the OpenOffice process.

按照建议,jodconverter 可用于访问 OpenOffice 进程。

http://code.google.com/p/jodconverter/wiki/GettingStarted

http://code.google.com/p/jodconverter/wiki/GettingStarted

回答by James Lee

you can try this:

你可以试试这个:

http://www.jopendocument.org/

http://www.jopendocument.org/

its an opensource java based library that allows you to work with open office documents without open office, thus removing the need for the OOserver.

它是一个基于 Java 的开源库,允许您在没有开放办公室的情况下处理开放的办公室文档,从而消除对 OOserver 的需要。

回答by Bastian Spanneberg

With the current version of JODConverter(3.0-SNAPSHOT), it's quite easy to handle multiple threads of OOo in headless-mode, as the library now supports starting up several instances and keeping them in a pool, by just providing several port numbers or named pipes when constructing a OfficeManager instance:

使用当前版本的JODConverter(3.0-SNAPSHOT),在无头模式下处理 OOo 的多个线程非常容易,因为该库现在支持启动多个实例并将它们保存在池中,只需提供几个端口号或命名构造 OfficeManager 实例时的管道:

final OfficeManager om = new DefaultOfficeManagerConfiguration()
  .setOfficeHome("/usr/lib/openoffice")
  .setPortNumbers(8100, 8101, 8102, 8103)
  .buildOfficeManager();

om.start();

You can then us the library e.g. for converting documents without having to deal with the pool of OOo instances in the background:

然后,您可以使用库,例如​​转换文档,而无需在后台处理 OOo 实例池:

OfficeDocumentConverter converter = new OfficeDocumentConverter(om);
converter.convert(new File("src/test/resources/test.odt"), new File("target/test.pdf"));

回答by Ulug'bek Ro'zimboyev

Thanks Bastian. I found another way, based on Bastian's answer. Opening several ports it provides access to create multithreads. But without many ports(enought several) we can improve performence by increase task queue timeouthere is a documentation. And one thing again, we decided not to startand stopofficeManageron each convertion process.At the end, I solved this task by this approach:

谢谢巴斯蒂安。根据巴斯蒂安的回答,我找到了另一种方法。打开多个端口,它提供了创建多线程的访问权限。但是没有很多端口(足够几个)我们可以通过增加task queue timeout这里的文档来提高性能。还有一件事,我们决定不startstopofficeManager在每个转换过程中。最后,我通过这种方法解决了这个任务:

public class JODConverter {

    private static volatile OfficeManager officeManager;
    private static volatile OfficeDocumentConverter converter;

    public static void startOfficeManager(){
        try {

            officeManager = new DefaultOfficeManagerConfiguration()
                    .setOfficeHome(new File('libre office home path'))
                    .setPortNumbers(8100, 8101, 8102, 8103, 8104 )  
                    .setTaskExecutionTimeout(600000L)    // for big files
                    .setTaskQueueTimeout(200000L)        // wait if all port were busy
                    .buildOfficeManager();
            officeManager.start();

            // 2) Create JODConverter converter
            converter = new OfficeDocumentConverter(officeManager);

        } catch (Throwable e){
            e.printStackTrace();
        }
    }

    public static void convertPDF(File inputFile, File outputFile) throws Throwable {

        converter.convert(inputFile, outputFile);
    }

    public static void stopOfficeManager(){
        officeManager.stop();
    }

}

I call JODConverter's convertPDFwhen convertion is need. It will be stopped only when application was down.

当需要转换时,我会打电话给JODConverter's convertPDF。只有当应用程序关闭时它才会停止。