How do I improve performance of application that uses the JAXBContext.newInstance operation?

后端 未结 5 1119
太阳男子
太阳男子 2020-12-30 01:59

I use the JAXBContext.newInstance operation in my JBoss based web application. This operation, as I understand, is very heavyweight. I only require two unique instances of t

5条回答
  •  感情败类
    2020-12-30 02:23

    A JAXB implementation (Metro, EclipseLink MOXy, Apache JaxMe, etc) typically initializes its metadata during the JAXBContext.newInstance call. All OXM tools need to initialize mapping metadata at some point and try to minimize the cost of this operation. Since it is impossible to do it with zero cost, it is best to only do it once. Instances of JAXBContext are thread safe, so yes you only need to create it once.

    From the JAXB 2.2 Specification, Section 4.2 JAXB Context:

    To avoid the overhead involved in creating a JAXBContext instance, a JAXB application is encouraged to reuse a JAXBContext instance. An implementation of abstract class JAXBContext is required to be thread-safe, thus, multiple threads in an application can share the same JAXBContext instance.

    Instances of Marshaller and Unmarshaller are not thread safe and must not be shared among threads, they are lightweight to create.

提交回复
热议问题