4

Is it possible switch the native parser, which I believe is based on Java reflection. We have some performance issues and wondering whether we can switch the implementation.

Your advise is highly appreciated.

Additional information: This is inherited code and we need to fix performance issues in our web-services. I am looking for performance boost without code changes. The existing code uses JAXB for marshalling and unmarshalling java objects which are generated via CXF (wsdl to java).

My goal is to switch the implementation to sTax and then use Woodstox library.

anonmys
  • 419
  • 1
  • 8
  • 18
  • What kind of performance problems? Is it because the files are large? How big are the files? Reflection isn't slow after all of the method objects and so on have been retrieved. – Matt Wonlaw Mar 29 '11 at 19:38
  • You also won't be able to get around the use of reflection if you want your parser to be able to create any Java object from an XML document. – Matt Wonlaw Mar 29 '11 at 19:51

1 Answers1

9

If your JAXB implementation uses a StAX parser under the covers via the standard JAXP APIs, then adding the Woodstox jar to your classpath should cause your JAXB impl to use Woodstox. You should see a performance improvement by doing this.

Since the Woodstox jar contains the following entries, adding it to the classpath will allow the JAXP APIs to return an instance of it:

  • META-INF/services/javax.xml.stream.XMLInputFactory
  • META-INF/services/javax.xml.stream.XMLOuputFactory

Note: I lead EclipseLink JAXB (MOXy), and MOXy uses a StAX parser when one is available. The other JAXB implementations (Metro, JaxMe) probably do the same thing.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 3
    actually i stand corrected (thought for sure jdk jaxb used stax). the jdk jaxb impl (in jdk 6 at least) uses SAX. – jtahlborn Mar 29 '11 at 20:50
  • 1
    I see. Thank you. Is it possible to simply switch the implementation from SAX to sTax without any code changes? Yes, we are using jdk1.6 – anonmys Mar 29 '11 at 21:34
  • 2
    If you unmarshal an XMLStreamReader created from Woodstox you should get the behavior you want regardless of JAXB implementation used. – bdoughan Mar 29 '11 at 21:41
  • Also: Woodstox 3.2 and above also implement SAX API -- the only thing it doesn't do is auto-register it as implementation (to reduce any chance of causing unexpected side effects). So you can actually use Woodstox via SAX too; this can be faster than Xerces for many tasks. – StaxMan Mar 30 '11 at 18:31
  • @StaxMan - Thanks, I'll have to check out the SAX implementation. – bdoughan Mar 30 '11 at 18:47
  • I changed the implementation to use the XMLStreamReader and I see significant performance improvement.XMLInputFactory factory = XMLInputFactory.newInstance(); StringReader reader = new StringReader(xml); XMLStreamReader streamReader = factory.createXMLStreamReader(reader); Unmarshaller u = jaxbContext.createUnmarshaller(); return (T) u.unmarshal(streamReader); But after this I replaced the jars with woodstox jar but I don't see any performance improvement when I switch to the new library (I verified that the xmlreader is woodstox(com.ctc.wstx.sr.ValidatingStreamReader). Any idea? – anonmys Mar 31 '11 at 00:47
  • when I remove the woodstox jars then I see the implementing class as com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl – anonmys Mar 31 '11 at 01:06
  • It could be that the Xerces impl of StAX you are using is as fast as the Woodstox impl of StAX. – bdoughan Mar 31 '11 at 01:30
  • 2
    Default JDK Stax impl is actually Sun's SJSXP, not Xerces. For reading it can be close to woodstox speed, but there are significant differences for some cases, esp for small files. Biggest difference is actually on writing, SJSXP is (for some reason) very slow. One thing to make sure is to always reuse XMLInputFactory (without this, Woodstox does lots of unnecessary one-off work). Another useful thing is to always close XMLStreamReader, -Writers. These make big difference. – StaxMan Mar 31 '11 at 17:53
  • How can I be sure that adding Woodstox (by Maven) it will be used? Is there a way to check it? Thank you! – Antonio Petricca May 12 '16 at 15:35
  • I don't see that `moxy` would provide `javax.xml.bind.JAXBContext` service via meta-inf/services file. Is it really an implementation of jaxb? – Dragas Jul 01 '21 at 12:50