Hi,
apart from the FopFactory which Simon mentioned,
I believe you can also do the same for TransformerFactory.
Also, you could consider caching the Stylesheet Templates.
They are Threadsafe & can be used by multiple Threads concurrently.
I'm a bit rusty, but below is some pseudo-code to illustrate some of that.
All the best,
Dave
packagefop.example;
importjava.io.ByteArrayInputStream;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.URI;
importjava.util.HashMap;
importjavax.xml.transform.Templates;
importjavax.xml.transform.TransformerConfigurationException;
importjavax.xml.transform.TransformerFactory;
importjavax.xml.transform.stream.StreamSource;
importorg.apache.fop.apps.FopFactory;
importorg.apache.fop.apps.FopFactoryBuilder;
publicclassFopPseudoCode {
/*
* TODOa very simple Templates Cache. You might want to add more features
like Timestamp checking
*/
privatestaticfinalHashMap <String, Templates> TEMPLATES_CACHE=
newHashMap<>();
privatestaticfinalThreadLocal<TransformerFactory> LOCAL_TX_FACTORY=
newThreadLocal<>() {
@Override
protectedTransformerFactory initialValue() {
finalTransformerFactory factory= TransformerFactory.newInstance();
/*
* TODOCustomize your factory here
*/
returnfactory;
}
};
privatestaticfinalThreadLocal<FopFactory> LOCAL_FOP_FACTORY=
newThreadLocal<>() {
@Override
protectedFopFactory initialValue() {
finalURI defaultBaseURI= newFile(".").toURI();
returnnewFopFactoryBuilder(defaultBaseURI)
/*
* TODOCustomize your FopFactoryBuilder here
*/
.build();
}
};
publicstaticvoidmain(finalString[] args) throwsException {
finalTransformerFactory txFactory= LOCAL_TX_FACTORY.get();
finalFopFactory fopFactory= LOCAL_FOP_FACTORY.get();
finalTemplates docTemplates= readStylesheetTemplates(txFactory,
"Invoice.xslt");
finalbyte[] docBytes= readDocBytes( "Invoice.xml");
finalbyte[] foBytes= generateFO(docBytes, docTemplates);
finalbyte[] pdfBytes= generatePDFfromFO(txFactory, foBytes, fopFactory);
}
privatestaticfinalTemplates
readStylesheetTemplates(finalTransformerFactory factory, finalString
fileName) throwsTransformerConfigurationException, IOException {
finalTemplates templatesCached= TEMPLATES_CACHE.get(fileName);
if(null!= templatesCached) {
returntemplatesCached;
}
try(finalInputStream ist=
newByteArrayInputStream(readDocStylesheet(fileName)))
{
finalTemplates templatesNew= factory.newTemplates(newStreamSource(ist));
TEMPLATES_CACHE.put(fileName, templatesNew);
returntemplatesNew;
}
}
privatestaticfinalbyte[] readDocStylesheet(finalString fileName) {
return"<xsl:stylesheet>...".getBytes();
}
privatestaticfinalbyte[] readDocBytes(finalString fileName) {
return"<Invoice>...".getBytes();
}
privatestaticbyte[] generateFO(finalbyte[] xmlDataBytes, finalTemplates
templates) {
return"<fo:root xmlns:fo=...".getBytes();
}
privatestaticbyte[] generatePDFfromFO(finalTransformerFactory factory,
finalbyte[] bytesFO, finalFopFactory fopFactory) {
return"%PDF-1.4...".getBytes();
}
}
On 17/07/2024 08:16, Simon Steiner wrote:
Hi,
You can use multiple threads, each thread with their own fopfactory.
Thanks
*From:*Allen Wang <1289495...@qq.com.INVALID>
*Sent:* 14 July 2024 08:39
*To:* fop-users <fop-users@xmlgraphics.apache.org>
*Cc:* 1289495104 <1289495...@qq.com>
*Subject:* Fop multiple thread generate performance
Dear Fop Doctor.
May I ask if we use mutiple thread to generate more output files. if
we can have any method to improve the performance.
Kindly, Could you give us some advise?
Thanks a lot.