Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webservice - client service instantiation

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

like image 207
Cris Avatar asked Jul 08 '11 16:07

Cris


People also ask

What is@ WebService annotation?

The @WebService annotation marks a Java class as implementing a Web service or marks a service endpoint interface (SEI) as implementing a web service interface. Important: A Java class that implements a web service must specify either the @WebService or @WebServiceProvider annotation.

What is web service client in Java?

A web service client accesses a stateless session bean through the bean's web service endpoint implementation class. By default, all public methods in the bean class are accessible to web service clients. The @WebMethod annotation may be used to customize the behavior of web service methods.

What is JAX WS client?

Java API for XML Web Services (JAX-WS) is a technology for building web services and clients that communicate using XML.

What is a web service client?

A web services client is an application capable of sending and receiving SOAP messages. Such an application serializes or deserializes the SOAP messages to a programming language type system enabling programmatic processing.


2 Answers

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.

like image 175
PapaLazarou Avatar answered Sep 22 '22 06:09

PapaLazarou


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.

like image 28
jtahlborn Avatar answered Sep 21 '22 06:09

jtahlborn