0

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.

lexicore
  • 42,748
  • 17
  • 132
  • 221
Abzelhan
  • 510
  • 1
  • 7
  • 26

1 Answers1

1

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.

Matt Pavlovich
  • 4,087
  • 1
  • 9
  • 17
  • 2
    And to answer the OP question; Spring uses a single context - either during initialization or on first use (controlled by `lazyInit`) and creates a new marshaller from the context for each use. – Gary Russell Jun 25 '18 at 20:53
  • 2
    Thanks for the answer) – Abzelhan Jun 26 '18 at 06:10