Hello,
we are converting html to pdf using java, and the conversion code is deployed on the Tomcat as part of a web application.
The problem we are experiencing is that when the Tomcat is shut down, the following is written to the log:
06-Oct-2021 16:16:17.957 SEVERE [Thread-4] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mywebapp] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@33bfe508]) and a value of type [com.aspose.pdf.internal.imaging.system.Threading.Thread] (value [com.aspose.pdf.internal.imaging.system.Threading.Thread@51900969]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Actually, I had had created a thread on the topic Tomcat fails to remove ThreadLocal on undeploy/shutdown The problem was marked as fixed and it could not be reproduced with my previous sample application. But it still was present in our more complex application, and I tried to determine what else might be causing exceptions on shutdown. I found out that if the following line is added to the servlet code, exceptions start occurring:
FontSettings.setDefaultFontName("Arial");
Is there a way to get rid of the exception?
The relevant piece of code used to reproduce it:
private byte[] getDocument()
{
FontSettings.setDefaultFontName("Arial");
byte[] result = null;
String htmlString = "<html><head><title>Title</title></head><body><p>Body</p></body></html>";
InputStream inputStream = new ByteArrayInputStream(htmlString.getBytes());
HtmlLoadOptions options = new HtmlLoadOptions();
options.setInputEncoding(StandardCharsets.UTF_8.name());
Document document = new Document(inputStream, options);
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream())
{
document.save(outputStream);
result = outputStream.toByteArray();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
document.close();
document.dispose();
MemoryCleaner.clear();
MemoryCleaner.clearCurrentThreadLocals();
}
return result;
}
Aspose version: 21.8
Java version: 16
Tomcat version: 9.0.43
Thank you
Arjana Bivainiene