uCosminexus Application Server, Web Service Development Guide

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

3.6.2 Example of dispatch-based implementation

Use APIs of the javax.xml.ws.Dispatch interface, the JAX-WS 2.2 specifications, JAXB 2.2 specifications, and SAAJ 1.3 specifications that are supported by Cosminexus, and perform the development.

Organization of this subsection
(1) Examples of implementing a dispatch-based Web Service client
(2) Referenced endpoint address
(3) Reusing a service class and dispatch

(1) Examples of implementing a dispatch-based Web Service client

The following is an example of implementing a dispatch-based Web Service client:

package com.example.sample.client;
 
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
public class TestClient {
    public static void main( String[] args ) {
        QName port = new QName( "http://sample.com", "AddNumbersImplPort" );
        SOAPBody soapBody = null;
        
        // Generate a service
        Service service = Service.create(
            new QName("http://sample.com", "UserInfoPort") );
        
        // Add a port to the service
        service.addPort( port, SOAPBinding.SOAP11HTTP_BINDING,
            "http://localhost:80/dispatch_provider/UserInfoService" );
        
        // Generate a dispatch
        Dispatch<SOAPMessage> dispatch = service.createDispatch(
            port, SOAPMessage.class, Service.Mode.MESSAGE );
        
        SOAPMessage request = null;
        try{
            // Generate a request message using the APIs of the SAAJ 1.3 specifications
            request = MessageFactory.newInstance().createMessage();
            SOAPBody reqSoapBody = request.getSOAPBody();
            SOAPElement soapElement = null;
            // Add elements to the SOAP Body
            SOAPBodyElement requestRoot= reqSoapBody.addBodyElement(
                new QName( ... ) );
            soapElement = requestRoot.addChildElement(
                new QName( ... ) );
            soapElement.addTextNode( ... );
            // Add an attachment
            File attachment = new File( ... );
            FileDataSource fds = new FileDataSource( attachment );
            AttachmentPart apPart = request.createAttachmentPart( new DataHandler( fds ) );
            request.addAttachmentPart(apPart);
        }
        catch( SOAPException e ){
            // Exception processing
        }
        // Specify the created request message, and invoke the Web Service using the dispatch
        SOAPMessage response = dispatch.invoke( request );
            
        try{
            // Perform the required processing for the response message
            SOAPBody resSoapBody = response.getSOAPBody();
            ...
        }
        catch( SOAPException e ){
            // Exception processing
        }
    }
}

When using a SOAP fault sent from the provider implementation class for implementing a Web Service client, invoke the invoke() method within the try-catch block and acquire the javax.xml.ws.soap.SOAPFaultException exception. You can also acquire the SOAP fault from the javax.xml.ws.soap.SOAPFaultException exception. The following is an implementation example:

package com.example.sample.client;
 
import javax.xml.namespace.QName;
...
import javax.xml.ws.soap.SOAPBinding;
import javax.xml.ws.soap.SOAPFaultException;
 
public class TestClient {
    public static void main( String[] args ) {
...
        try{
            // Generate the request message using the APIs of SAAJ 1.3 specifications
            ...
        }
        catch( SOAPException e ){
            // Exception processing
        }
        SOAPMessage response = null;
        try{
            // Specify the created request message, and invoke a Web Service using the dispatch
            response = dispatch.invoke( request );
        }
        catch( SOAPFaultException e ){
            // Processing for acquiring the SOAP fault
            SOAPFault fault = e.getFault();
            // Perform the required processing for the acquired SOAP fault
            String faultCode = fault.getFaultCode();
            ...
        }
        try{
            // Perform the required processing for the response message
            ...
        }
        catch( SOAPException e ){
            e.printStackTrace();
        }
    }
}

(2) Referenced endpoint address

You can specify and change a URL (endpoint address) of the Web Service to be connected with the javax.xml.ws.service.endpoint.address property of the message context. For the examples about specifying and changing the endpoint addresses, see 3.6.1(5)(c) Invoking a method of the port.

(3) Reusing a service class and dispatch

The generation of a service class and dispatch requires processing cost, so we recommend that you reuse the generated service class and the dispatch. You need not generate a service class more than once to add a port and generate a dispatch. Also, you need not acquire a dispatch more than once to invoke a method of the dispatch multiple times However, when sharing the dispatch with multiple threads, the changes in the property of request context of the dispatch 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.

For implementing a Web Service client with servlets and EJBs, we recommend that you acquire service classes and the dispatch using each of the initialization methods, and then reuse them. Changes in the request context property of dispatch must be performed in each of the initialization methods.