10MB程度のサイズのdocxまたはpptxファイルをpdfへ変換する際に
20秒経過したら途中で処理を中止することができますでしょうか?
cells-javaのsetInterruptMonitorと同じようなことをしたいです。
I understand you would like to stop a DOCX / PPTX → PDF conversion after 20 seconds, similar to the setInterruptMonitor feature that exists in Aspose.Cells for Java [1].
Current API support
Assumption: Aspose.Words for Java and Aspose.Slides for Java do not provide a built‑in timeout or interrupt‑monitor API comparable to setInterruptMonitor. The documentation and released APIs for these products do not expose such a feature.
Work‑around you can implement
- Run the conversion in a separate thread (e.g., using
java.util.concurrent.ExecutorService). - Submit the conversion as a
Callableand obtain aFuture. - Use
Future.get(timeout, TimeUnit.SECONDS)to wait for the desired period (20 s). - If the timeout expires, call
future.cancel(true)to attempt to interrupt the conversion thread.
ExecutorService exec = Executors.newSingleThreadExecutor();
Callable<Void> task = () -> {
// Example for Words
com.aspose.words.Document doc = new com.aspose.words.Document("input.docx");
doc.save("output.pdf", com.aspose.words.SaveFormat.PDF);
// Example for Slides
// com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation("input.pptx");
// pres.save("output.pdf", com.aspose.slides.SaveFormat.Pdf);
return null;
};
Future<Void> future = exec.submit(task);
try {
future.get(20, TimeUnit.SECONDS); // wait up to 20 seconds
} catch (TimeoutException e) {
// Timeout reached – attempt to stop the conversion
future.cancel(true); // may interrupt the thread
// Optionally log or handle the partial result
} finally {
exec.shutdownNow();
}
Caveats
- The conversion code is not guaranteed to be interrupt‑safe. Cancelling the thread may leave the library in an undefined state, so it is advisable to create a fresh JVM process for each conversion if strict isolation is required.
- Resources (e.g., file handles) should be cleaned up in a
finallyblock or by using try‑with‑resources where possible. - This approach works for both Aspose.Words and Aspose.Slides because the conversion is performed synchronously inside the
savecall.
If you need a built‑in timeout feature, you can submit a feature request through the Aspose Feedback portal; the team can consider adding an API similar to setInterruptMonitor in a future release.
I understand that you want to abort a DOCX → PDF or PPTX → PDF conversion after 20 seconds, similar to the setInterruptMonitor feature that exists in Aspose.Cells for Java.
What is currently available
- Aspose.Words for Java and Aspose.Slides for Java do not expose a built‑in timeout or interrupt‑monitor API comparable to
setInterruptMonitor[1].
Practical workaround
- Run the conversion in its own thread (e.g., using
java.util.concurrent.ExecutorService). - Wrap the conversion code in a
Callableand submit it to the executor, obtaining aFuture. - Wait with a timeout:
future.get(20, TimeUnit.SECONDS). - If the timeout expires, call
future.cancel(true)to attempt to interrupt the conversion thread.
ExecutorService exec = Executors.newSingleThreadExecutor();
Callable<Void> task = () -> {
// Words example
com.aspose.words.Document doc = new com.aspose.words.Document("input.docx");
doc.save("output.pdf", com.aspose.words.SaveFormat.PDF);
// Slides example (if needed)
// com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation("input.pptx");
// pres.save("output.pdf", com.aspose.slides.SaveFormat.Pdf);
return null;
};
Future<Void> future = exec.submit(task);
try {
future.get(20, TimeUnit.SECONDS); // wait up to 20 s
} catch (TimeoutException e) {
// Timeout reached – try to stop the conversion
future.cancel(true);
// Log or handle partial result as needed
} finally {
exec.shutdownNow();
}
Caveats
- The conversion code is not guaranteed to be interrupt‑safe; cancelling the thread may leave the library in an undefined state [1].
- For strict isolation, it is advisable to run each conversion in a fresh JVM process rather than relying on thread interruption [1].
If you need a native timeout feature
- You can submit a feature request through the Aspose Feedback portal; the team may consider adding an API similar to
setInterruptMonitorin a future release [1].
Feel free to try the above pattern, and let us know if you encounter any issues or need further assistance.