Thread safety when using spring WebServiceTemplate and Jaxb2Marshaller

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

The created WebServiceTemplate is used in a multithreaded way, that is, several threads can simultaneously call SendAndReceive. My question is: is my configuration safe? I'm worried about Jaxb2Marshaller. Javadoc says that Jaxb2Marshallers are not necessarily thread safe. How can I use Jaxb2Marshaller in a secure thread without reinitializing the Jaxb context?

Aside: look at the spring-ws configuration example in spring link makes me think that Jaxb2Marshaller is thread safe, but Javadoc seems to contradict this.

+4
source share
2 answers

The javadoc for Jaxb2Marshaller doesn't mention security thread anyway, so I'm not sure why you don't think so. If it were not thread safe, javadoc would have said it very clearly.

Your configuration of the WebServiceTemplate , SaajSoapMessageFactory and Jaxb2Marshaller completely fine and completely thread safe.

+6
source

Create some Jaxb2Marshaller (say five), put them in the pool (use LinkedBlockingQueue ). When you create a thread, pass it a queue.

When a thread needs one, take() one of the queue / pool. When the pool is empty, threads will block on this call.

When a thread executes with Jaxb2Marshaller , put() , it returns to the queue, so other threads can use it.

If you find that thread blocks too often expect a marshaller, add more to the queue (see first step). This way you can easily configure the pool (or even configure it). Then the queue will automatically distribute them.

0
source

All Articles