0

I have a large XSLT string that I am using to transform XML into json. I am doing this within a dropwizard webservice, where concurrency is highly desired. When I store this XSLT string within a file, it leads to all the webservice threads accessing the file in a sequential order from the filesystem(thus delaying execution).

If I try to store that XSLT as a java string I get a constant string too long exception. What is the best way I can store this XSLT so that my threads can access it in parallel?

Adi
  • 387
  • 3
  • 6
  • 14

2 Answers2

3

If the XSLT source is really that large, you should probably store it in a file, e.g. as a classpath resource, and only load and parse it once:

private static Templates largeTemplate;

Initialize using (once only):

try (InputStream in = Test.class.getResourceAsStream("path/to/large.xslt")) {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    largeTemplate = transformerFactory.newTemplates(new StreamSource(in));
}

Then for each thread:

Transformer transformer = largeTemplate.newTransformer();
// Use transformer here
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I wouldn't normally put the Templates object in static; that means you can only have one instance in the JVM. – Michael Kay Apr 24 '18 at 23:15
  • @MichaelKay That's the point. A single shared instance of the pre-parsed XSLT file, so system doesn't have to re-parse it every time it is needed. Since that's what you say too ("save the compiled stylesheet"), I don't know what your comment was supposed to mean. – Andreas Apr 24 '18 at 23:29
  • There's lots of discussion on when it's appropriate to use static state versus a singleton pattern, for example here: https://stackoverflow.com/questions/24214148/java-getinstance-vs-static, and I don't want to have that debate here, just to say that I would always use the singleton pattern for this unless it's a trivial single-shot application. – Michael Kay Apr 25 '18 at 07:40
  • @MichaelKay I'm confused as to your first comment. The singleton pattern also stored the singleton in a static field, and by its very nature, there will be only one instance of a singleton in the JVM. So, saying you wouldn't use `static` and that it means you can only have one instance in the JVM, then DUH, yeah, that's was the point. If you meant to say I should have used the singleton pattern, then you should have said so, but that first comment was meaningless, especially that second half of it. – Andreas Apr 25 '18 at 07:53
2

You don't want to compile the same large XSLT file more than once, even if the multiple compilations are done in parallel. Compile it during initialization of the web service, or on first reference, and save the compiled stylesheet (a Templates object, if you're using JAXP) so that it can be reused for multiple transformations. The Templates object is thread-safe so multiple transformations using this stylesheet can run concurrently.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164