I have checked on latest 25.9 version also,
We want to kill this thread in safe manner, if it stuck in aspose library. Thead.stop() is not better option to use as per Java. Please suggest.
public static String convertToHtmlWithTimeout(Document document, long timeout, TimeUnit unit) {
final StringBuilder result = new StringBuilder();
Thread worker = new Thread(() -> {
try {
HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML);
opts.setExportPageSetup(true);
opts.setExportListLabels(ExportListLabels.AS_INLINE_TEXT);
opts.setExportImagesAsBase64(true);
opts.setExportFontsAsBase64(true);
opts.setExportTocPageNumbers(true);
opts.setExportPageMargins(true);
opts.setExportOriginalUrlForLinkedImages(true);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
document.save(baos, opts);
synchronized (result) {
result.append(baos.toString(StandardCharsets.UTF_8));
}
}
} catch (Throwable t) {
// log it if needed
}
});
worker.start();
try {
worker.join(unit.toMillis(timeout));
if (worker.isAlive()) {
System.out.println("Timeout reached. Forcibly stopping thread...");
worker.stop(); // ⚠️ unsafe but guarantees kill
return null;
}
} catch (InterruptedException ignored) {}
return result.length() > 0 ? result.toString() : null;
}
@govind.sharma
To address the issue of a hanging thread during document conversion to HTML using Aspose.Words, you can implement a timeout mechanism without resorting to the unsafe Thread.stop()
method. Instead, utilize a Future
with an ExecutorService
to manage the thread execution, allowing for safe cancellation of the task if it exceeds the specified timeout.
Here’s an example of how to implement this:
import com.aspose.words.Document;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.SaveFormat;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.*;
public static String convertToHtmlWithTimeout(Document document, long timeout, TimeUnit unit) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> {
HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML);
opts.setExportPageSetup(true);
opts.setExportListLabels(ExportListLabels.AS_INLINE_TEXT);
opts.setExportImagesAsBase64(true);
opts.setExportFontsAsBase64(true);
opts.setExportTocPageNumbers(true);
opts.setExportPageMargins(true);
opts.setExportOriginalUrlForLinkedImages(true);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
document.save(baos, opts);
return baos.toString(StandardCharsets.UTF_8);
}
});
try {
return future.get(timeout, unit);
} catch (TimeoutException e) {
future.cancel(true); // Cancel the task
System.out.println("Timeout reached. Task was cancelled.");
return null;
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
} finally {
executor.shutdownNow(); // Shutdown the executor
}
}
In this code:
- An
ExecutorService
is employed to run the document conversion in a separate thread.
- The
Future
object allows you to wait for the result with a specified timeout.
- If the timeout is reached, the task is cancelled safely using
future.cancel(true)
.
This approach mitigates the risks associated with forcibly stopping threads and provides a more robust way to handle timeouts.
I have already tried executor service implementation, Thread is not getting killed and it’s taking all CPU usage.
Document (which we can’t share) is having some structure which is causing as per our analysis. It’s not related to document size.
Please just want to kill this thread in safe manner so that our application should not consumer CPU in such kind of scenarios and after killing we will not process this particular in further flow.
@govind.sharma In Aspose.Words you can use IDocumentLoadingCallback to interrupt document loading process. Also, you can use IDocumentSavingCallback to cancel save operation.
Upon saving document to PDF or any other fixed page formats, however, the most time is taken by document layout process than by saving operation. You can interrupt the layout process using IPageLayoutCallback.
If possible, could you please attach your problematic document here for testing? We will check the issue on our side and provide you more information.
Please suggest how to implement IDocumentSavingCallBack and IPageLayoutCallback, I’ll try that as I can’t share client confidential document.
I have 2 page faulty section which still has client confidential information, hence can’t share.
Please help suggested code changes, I’ll let you know if this callback worked.
End goal is thread should be killed if timeout happens.
@govind.sharma The code examples provided in our documentation shows exactly how to terminate document loading or saving process on timeout.
Please note, it is safe to attach documents in the forum. Only you as a topic starter and Aspose staff can access the attachments.