Java – Reuse the SOAP client with Axis

Reuse the SOAP client with Axis… here is a solution to the problem.

Reuse the SOAP client with Axis

I’m using Apache Axis to make SOAP requests to a service. I’ve noticed that sometimes it takes a few seconds to get a response, even though the service is currently just a simple echo. So I’m wondering if it takes time to establish a connection, even if the server does keep the HTTP/1.1 connection active.

Should clients somehow be reused between requests, or is it possible to get a new client for each request?

Here is my code. Should I keep locator and/or client between requests or can I forget it?

MyExampleServiceLocator locator = new MyExampleServiceLocator();
MyExampleServicePort client = locator.getMyExampleServicePort(url);
MyExampleRequest request = buildMyExampleRequest();
MyExampleResponse response = client.send(request);    This takes time sometimes

Solution

  • For complex services, instantiating a locator can be costly. Therefore, you should always try to reuse it as much as possible. Locators should be thread-safe, so you can use them as singletons. Note, however, that in Axis 1.4 there is at least one locator-related thread safety issue: AXIS-2498
  • Creating a new stub (client) is less expensive, but reusing stubs is fine. They should also be thread-safe (at least in Axis 1.4), except in scenarios where stubs are used in a stateful manner (such as HTTP sessions).
  • Axis’ default HTTP transport supports only HTTP 1.0 and creates a new connection for each request.

Related Problems and Solutions