Wednesday, March 2, 2011

JAXBContext is expensive to create

According to Java Doc

"The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate. "

However it is quite slow and expensive to create. It is wise to reuse the existing instance whenever the message of the same schema is repeated for marshalling or unmarshalling.

One way is to create single JAXBContext with many JAXB objects that are used and then share this JAXBContext within the application. JAXBContxet has one factory method to instantiate JAXBContext. This factory method can take in multiple JAXB classes into the context. There are two forms of instantion of JAXBContext as shown in below.

JAXBContext jc = JAXBContext.newInstance("com.toic.order.foo:com.toic.order.bar" );

JAXBContext jc = JAXBContext.newInstance(com.toic.order.foo.class, com.toic.order.bar.class" );

Where com.toic.order.foo and com.toic.order.bar are the qualified class names of two JAXB classes that are taken into the JAXB context.








No comments:

Post a Comment