views:

139

answers:

2

I am using spring WebServiceTemplate as a web service client programmatically i.e. without instantiating a spring container. I am using Jaxb2Marshaller for marshaling/unmarshaling. In my application, I create a single instance of the SaajSoapMessageFactory and one instance of Jaxb2Marshaller. I also create a single instance of the WebServiceTemplate and assign the previously created instances of SaajSoapMessageFactory and Jaxb2Marshaller.

The WebServiceTemplate I created is used in a multi threaded way i.e. multiple threads can call marshalSendAndReceive at the same time. My question is - is my configuration thread safe? I am concerned about the Jaxb2Marshaller. The javadoc says Jaxb2Marshallers are not necessarily thread safe. How can I use the Jaxb2Marshaller in a thread safe way without reinitializing the Jaxb context?

As an aside: looking at the example spring-ws configuration in the spring reference leads me to believe that the Jaxb2Marshaller is thread safe but the Javadoc seems to contradict that.

A: 

Create several of them (say five), put them into a pool (a queue) and when a thread needs one, it removes it from the pool. When the pool is empty, a few threads will have to wait for the active threads to complete their work and return the marshaller.

Aaron Digulla
+2  A: 

The javadoc for Jaxb2Marshaller makes no mention of thread-safety one way or another, so I'm not sure why you think it's not. If it wasn't thread-safe, the javadoc would say that very clearly.

Your configuration of WebServiceTemplate, SaajSoapMessageFactory and Jaxb2Marshaller singletons is perfectly fine, and entirely thread-safe.

skaffman
The definition of the createMarshaller() [http://static.springsource.org/spring-ws/sites/1.5/apidocs/org/springframework/oxm/jaxb/AbstractJaxbMarshaller.html#createMarshaller%28%29] method sowed the seed of doubt in my mind. It says "JAXB marshallers are not necessarily thread safe." I know they are talking about javax.xml.bind.Marshaller but I am not sure how Jaxb2Marshaller uses this class(or implementations of this interface).
neesh
@neesh: Ah, OK. This is true, `Marshaller` and `Unmarshaller` objects are not thread-safe, that's why Spring-WS doesn't share them between threads. This is about correct use of the JAXB API.
skaffman