14

Do you know how costly is to create a webservice client service instance ?

 JavaWebService service = new JavaWebService();
 SomePort port = service.getJavaWebServicePort(); 

Creating the service once and after that reusing same port in a multi threaded environment (webapp) is not dangerous ?

Read that the port getPort and port itself is not thread safe but also creating each time a service it might be problematic if it is a costly operation.

Any idea ?

THanks

Mustafa Ulu
  • 185
  • 1
  • 7
Cris
  • 4,947
  • 6
  • 44
  • 73

2 Answers2

16

In the JAX-WS reference implementation (Metro), the creation of the JavaWebService is inexpensive (in our generated clients, we tend to find this takes around 20ms).

The first creation of SomePort is quite expensive (circa 200ms for us); subsequent calls to getSomePort() on the same JavaWebService instance are substantially quicker (circa 3ms for us).

So, an implementation that creates a JavaWebService every time it needs to get a SomePort will carry a degree of expense. In short, the answer to the question is "Quite costly".

However, even though the methods on SomePort are not thread safe, the methods on JavaWebService are. So, the sensible usage pattern (at least with Metro - thread-safety is implementation specific due to a somewhat lacking specification) is to reuse JavaWebService as you will only incur the expensive getSomePort() call once.

Update

This agrees with two posts by Andreas Leow, an employee from Oracle Germany, one of the posters in the thread referenced by @PapaLazarou in the comment below, who wrote regarding the Service object,

You can create just one single static Service instance per WSDL: any single Service object is fully thread-safe and can be shared by as many concurrent threads as you like.

and about the usage of ports,

While I am almost 100% certain that CXF JAX-WS Ports are thread-safe, Metro's Port objects definitely are not thread-safe.

PapaLazarou
  • 185
  • 1
  • 6
  • 2
    Do you have a reference for this? So far all I can find are posts that the returned ports are not thread-safe and CXF's page saying that "clients" (I assume they mean ports) are not thread-safe. What I'm seeking is some *official* statement that Metro services (not ports) are thread-safe, specifically that calls to `getPort` return separate instances. – David Harkness Aug 10 '12 at 18:17
  • 1
    I found a number of post by the Metro devs that indicated this but have struggled to locate them again. The only post I've managed to locate recently is [this](http://metro.1045641.n5.nabble.com/JAX-WS-clients-td5709817.html) one. FYI, my independent concurrent tests also indicated this to be the case. – PapaLazarou Jan 21 '13 at 16:41
  • According to the CXF documentation (http://cxf.apache.org/faq.html#FAQ-AreJAX-WSclientproxiesthreadsafe?) "CXF proxies are thread safe for MANY use cases." See the documentation for further explanations. – Joern Aug 17 '18 at 06:55
4

If you are using jax-ws, then you cannot share a port across threads (they are not thread-safe). if you are concerned about the overhead of creating a port (and have measured it and confirmed that it is a bottleneck in your application), then you could create a connection pool of ports.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • I read that they are not thread safe on cxf site,they provide a thread safe getPort...but i use MEtro stack JaxWS.So creating the service might be a heavy operation ? (will start doing some tests but did not wanted to reinvent the wheel) – Cris Jul 08 '11 at 17:01