uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

3.6.3 Examples of implementation using JAX-WS API

You can use the JAX-WS API supported by the JAX-WS functionality of Cosminexus to implement a Web Service client. For the support range of the JAX-WS APIs, see 19.2 Support range of the JAX-WS APIs.

Organization of this subsection
(1) Example of implementing a Web Service client using JAX-WS API
(2) Reusing the javax.xml.ws.Service object and the port

(1) Example of implementing a Web Service client using JAX-WS API

An example of implementing a Web Service client using JAX-WS API is as follows:

package com.example.sample.client;
 
import com.example.sample.TestJaxWs;
import com.example.sample.TestJaxWsService;
 
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.WebServiceException;
public class TestClient {
 public static void main( String[] args ) {
 
 // Specify the WSDL URL and service name
 URL url = null;
 try {
  url = new URL("http://localhost:8085/fromwsdl/test?wsdl");
 } catch (MalformedURLException e) {
  // Exception processing
 }
 QName serviceName =
  new QName("http://example.com/sample", "TestJaxWsService");
 // Generating a Service instance
 Service service = Service.create(url, serviceName);
 System.out.println(service.getWSDLDocumentLocation());
 
 QName portName = null;
 
 // Display the list of port names
 Iterator it = service.getPorts();
 while(it.hasNext()) {
  portName = (QName)it.next();
  System.out.println(portName);
 }
 
 
 // Acquire a port
 TestJaxWs port = (TestJaxWs)service.getPort(TestJaxWs.class);
 
 // Acquire the sending context
 Map<java.lang.String, java.lang.Object>context = 
((BindingProvider)port).getRequestContext();
 System.out.println(context.entrySet());
 
 // Invoke the service method
 try {
  port.jaxWsTest1( "TEST", 1c23);
 } catch (WebServiceException e) {
  // Exception processing
 }
 }
}

(2) Reusing the javax.xml.ws.Service object and the port

The generation of a javax.xml.ws.Service object and the acquisition of a port require processing cost, so we recommend that you reuse the generated javax.xml.ws.Service object. You need not generate javax.xml.ws.Service objects more than once to acquire a port.

Similarly, acquisition of the port also incurs processing costs and therefore we recommend the reuse of acquired ports. You need not acquire the port more than once for invoking the Web method of port multiple times. However, when sharing the port with multiple threads, the changes in the property of request context of the port to be shared must be performed before operating multiple threads. If the changes are performed when operating multiple threads, communication might fail and an invalid SOAP message might be sent.

To implement a Web Service client using servlets and EJBs, generate the javax.xml.ws.Service object or acquire the port in each of the initialization methods, and reuse the object. Changes in the request context property of the port must be executed in each of the initialization methods.