Reputation: 664
I am trying to implement a timeout on a save operation on a PDF document. I have this piece of code so far:
private Boolean saveDocumentWithTimeout(Path path, PDDocument pdDocument) {
ExecutorService executor = Executors.newSingleThreadExecutor();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
try {
Callable<Boolean> taskSavePDF = () -> {
try {
pdDocument.save(path.toString());
return true;
} catch (ClosedChannelException e) {
return false; // This is never reached
} catch(Exception e) {
return false // This is never reached
}
};
Future<Boolean> future = executor.submit(taskSavePDF);
scheduler.schedule(() -> future.cancel(true), 1, TimeUnit.MINUTES);
boolean success;
try {
success = future.get();
} catch (CancellationException e) {
success = false;
} catch (ExecutionException | InterruptedException e) {
success = false;
}
if (!success) {
pdDocument.close();
System.out.println("Could not save PDF");
}
return success;
} catch (IOException | ConcurrentModificationException e) {
return false;
}
finally {
executor.shutdownNow();
scheduler.shutdownNow();
}
}
If it is relevant: PDDocument is from Apache PDFBOX.
The problem I am encountering is that pdDocument.save(path.toString()) (that performs IO-operations) starts throwing quite a few exceptions when saving while the file has been closed/interrupted. It stops throwing these exceptions after a few seconds, and gives up saving the PDF, resulting in the thread being shutdown. These exceptions are unfortunately encapsulated in my software and displayed to the user.
Is there any way to interrupt the Future, while also catching any exceptions that the Future/thread throws during its entire shutdown phase? I tried using try/catch inside the Future, but the exception is never caught.
Here is one of the exceptions I am trying to catch:
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) java.nio.channels.ClosedChannelException: null
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at java.base/sun.nio.ch.FileChannelImpl.ensureOpen(Unknown Source)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at java.base/sun.nio.ch.FileChannelImpl.position(Unknown Source)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.io.RandomAccessReadBufferedFile.seek(RandomAccessReadBufferedFile.java:143)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.io.RandomAccessReadView.seek(RandomAccessReadView.java:90)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:143)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.cos.COSStream.createView(COSStream.java:196)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfparser.PDFObjectStreamParser.<init>(PDFObjectStreamParser.java:51)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfparser.COSParser.parseObjectStreamObject(COSParser.java:825)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(COSParser.java:674)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfparser.COSParser.dereferenceCOSObject(COSParser.java:623)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.cos.COSObject.getObject(COSObject.java:121)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfwriter.compress.COSWriterCompressionPool.addStructure(COSWriterCompressionPool.java:164)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfwriter.compress.COSWriterCompressionPool.<init>(COSWriterCompressionPool.java:84)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfwriter.COSWriter.doWriteBodyCompressed(COSWriter.java:474)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1295)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:429)
2024-07-08 16:24:35,019 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1568)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1444)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1034)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:984)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:940)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear//org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:923)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at deployment.lovisaear-7.185.0-SNAPSHOT.ear.dokumentsamling-server-7.185.0-SNAPSHOT.jar//com.computas.lovisa.dokumentsamling.server.model.PDFIDokumentsamling.la
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
2024-07-08 16:24:35,020 INFO [stdout] (pool-25-thread-1) at java.base/java.lang.Thread.run(Unknown Source)
Upvotes: 1
Views: 68