Java 如何异步运行长时间运行的处理器并从同一线程使用它?
基本上,我们有一个名为OfficeManger的类,它充当连接到openoffice软件的驱动程序,它需要一直连接,以便我们可以使用转换器转换文档。我们在web应用程序启动期间启动OpenOfficeProcess,该应用程序启动良好。但看起来在init()中运行的执行器位于不同的线程上,我们无法获取OfficeManager的运行实例。如何在它自己的线程中运行,以便我可以从不同的类调用这个实例来使用converter方法Java 如何异步运行长时间运行的处理器并从同一线程使用它?,java,multithreading,threadpool,executors,Java,Multithreading,Threadpool,Executors,基本上,我们有一个名为OfficeManger的类,它充当连接到openoffice软件的驱动程序,它需要一直连接,以便我们可以使用转换器转换文档。我们在web应用程序启动期间启动OpenOfficeProcess,该应用程序启动良好。但看起来在init()中运行的执行器位于不同的线程上,我们无法获取OfficeManager的运行实例。如何在它自己的线程中运行,以便我可以从不同的类调用这个实例来使用converter方法 OfficeDocumentConverter converte
OfficeDocumentConverter converter = OpenOfficeProcessor.getInstance().getDocumentConverter();
converter.convert(inputFile, outputFile, pdf);
OpenOfficeProcessor
public class OpenOfficeProcessor {
private static final OpenOfficeProcessor INSTANCE = new OpenOfficeProcessor();
static ExecutorService executor = Executors.newSingleThreadExecutor();
private final OfficeManager officeManager;
private final OfficeDocumentConverter documentConverter;
public OpenOfficeProcessor(){
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
String homePath = ConfigurationManager.getApplicationProperty(ConfigurationManager.OPENOFFICE_HOME_PATH);
if(homePath != null){
configuration.setOfficeHome(homePath);
} else {
LOG.error("OpenOffice.home.path is not set in the properties file");
new Throwable("Please set OPENOFFICE.HOME.PATH parameter in the properties file");
}
String port = ConfigurationManager.getApplicationProperty(ConfigurationManager.OPENOFFICE_LISTENER_PORT);
if( port != null){
configuration.setPortNumber(Integer.parseInt(port));
}else {
LOG.error("openoffice.listener.port is not set in the properties file");
}
String executionTimeout = ConfigurationManager.getApplicationProperty(ConfigurationManager.OPENOFFICE_EXECUTION_TIMEOUT);
if(executionTimeout != null){
configuration.setTaskExecutionTimeout(Long.parseLong(executionTimeout));
}
String pipeNames = ConfigurationManager.getApplicationProperty(ConfigurationManager.OPENOFFICE_PIPES_NAMES);
if(ConfigurationManager.getApplicationProperty(ConfigurationManager.OPENOFFICE_PIPES_NAMES)!= null){
configuration.setPipeNames(pipeNames);
}
officeManager = configuration.buildOfficeManager();
documentConverter = new OfficeDocumentConverter(officeManager);
}
public static OpenOfficeProcessor getInstance()
{
return INSTANCE;
}
protected static void init() {
LOG.debug("Starting the open office listener...");
executor.submit(new Callable(){
@Override
public Object call() throws Exception {
OpenOfficeProcessor.getInstance().officeManager.start();
return null;
}
});
}
protected static void destroy() {
LOG.debug("Stopping the open office listener...");
OpenOfficeProcessor.getInstance().officeManager.stop();
}
public OfficeManager getOfficeManager() {
return officeManager;
}
public OfficeDocumentConverter getDocumentConverter() {
return documentConverter;
}
}
办公室经理
public interface OfficeManager {
void execute(OfficeTask task) throws OfficeException;
void start() throws OfficeException;
void stop() throws OfficeException;
boolean isRunning();
}
我不太明白你在这里想干什么。
start()
做什么?这会产生另一个线程吗?OfficeManager
代码是否在start()中运行?现在在我看来,好像您调用了start()
,但是没有任何东西在运行管理器代码本身?你应该做一些编辑,考虑一下这个项目对没有经验的人来说是怎样的。到底是什么问题,代码没有编译?如果我直接使用OfficeManager.start(),那么我可以使用documentConverter进行转换。但现在,由于我使用executor启动OfficeManager,所以无法使用documentConverter,因为它告诉我OfficeManager未启动。