Sunday 31 January 2010

Web service clients and concurrency

Web services are probably the most common mechanism for distributed systems integration online at the moment.

In the Java world there are tools available to generate classes to act as remote proxies and interfaces.

Apache Axis and Axis2 provide a wsdl2java command line utility for generating these Java classes from a web service's WSDL file/URL.

What some people may not consider is that the implementation details of the stub which their client code will call, may not actually be safe to be called from multiple threads at the same time.

So, if you have a multithreaded application - such as a web app - where multiple threads could potentially be making calls on the web service via a shared instance of the generated stub then it is possible that some state that is set up as part of the processing of one thread could be changed by another thread.

It may not be apparent from looking at the generated code, but ultimately that code extends code buried within a jar that you do not have control over.

There are several approaches to avoiding the problem:
- create an instance of the stub for each call
- create and access stubs in a thread local way
- set up a pool of stubs, much the same way as database connections are often pooled.

I'm going to try out CommonsPoolTargetSource for the pooling approach.

No comments:

Post a Comment