Search code examples
javaspring-bootjaxbspring-jmsjaxb2

Jaxb2Marshaller how it holds JAXBContext


I use MarshallingMessageConverter as implementation of MessageConverter bean. I'm using Jaxb2Marshaller as an implementation of org.springframework.oxm.Marshallerclass. I set classes to be bound in this way:

marshaller.setClassesToBeBound(new Class[]{Class1.class,Class2.class})

Also i can set classes by using JAXBContext class and retrieve Marshaller and Unmarashaller:

JAXBContext.newInstance(Class1.class,Class2.class)
jaxbContext.createMarshaller();
jaxbContext.createUnmarshaller();

This is similar to Jaxb2Marshaller because it has internal field of JAXBContext. Creating new JAXBContext for every parse request is very expensive.

My question is: If i declare Marshaller by using Jaxb2Marshaller all classes to be bind in this call marshaller.setClassesToBeBound(new Class[]{Class1.class,Class2.class}) will be reused in each parse request and will be thread safe, or JAXBContext from this classes will be created again for each request? I use this Jaxb2Marshaller as Bean.


Solution

  • Per the JAXB spec-- JAXBContext is thread safe. Marshaller and Unmarshaller are not and should be created once-per use, or wrapped with some sort of thread-safe service to synchronize access to the marshaller and unmarshaller.