Dynamic proxy client  Document style client

Chapter 9: Developing Web Service Clients

Dynamic invocation interface client

The Dynamic Invocation Interface (DII) client does not require a WSDL file to generate static stubs or pass the WSDL file to the service factory to create the service; instead, the client must know a service’s address, operations, and parameters in advance. A DII client discovers service information dynamically at runtime by a given set of service operations and parameters as you will see in the example.

The significant JAX-RPC client APIs used are:


DII client example

This section contains a listing of several sample DII clients.

In addition, you can find a DII sample in %JAGUAR%\sample\wst_samples\JAXRPCClient\client\DIIClient.java

package client;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.ParameterMode;
import javax.xml.namespace.QName;

public class JAXRPC_DIIClient
{
    public static void main(String[] args)
    {
        try
        {
            /*  
	*  URL of the web service 
	*/
            String address = "http://localhost:8080/ws/services/AddNumbers";
            String namespaceURI = "urn:simpleJavaClass.AddNumbers";
            String serviceName = "AddNumbers";
            String portName = "AddNumbers";
            int num1 = 10;
            int num2 = 20;


            ServiceFactory factory = ServiceFactory.newInstance();

       /*
	* Create an instance of the Service with the given service QName 
	*/
            Service service = factory.createService(new QName(serviceName));

            Call call = service.createCall(new QName(portName));

            call.setTargetEndpointAddress(address);

            QName intQName = new QName("http://www.w3.org/2001/XMLSchema", "int");            /*
    	 * Set operation name to invoke.
    	 */
            call.setOperationName(new QName(namespaceURI,"add"));
            /*
    	 * Add parameters definitions in the call object.
    	 */
            call.addParameter("number1", intQName, ParameterMode.IN);
            call.addParameter("number2", intQName, ParameterMode.IN);

            /*
    	 * Set definition of the return type.
    	 */
            call.setReturnType(intQName);

            Object[] inParams = new Object[2];
            inParams[0] = new Integer(num1);
            inParams[1] = new Integer(num2);

            int value= ((Integer)call.invoke(inParams)).intValue();
            System.out.println("Result of adding " + num1 + " and " +  num2  + " is: " + value);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

Significant AXIS client APIs used for the following sample are:

The sample client is in %JAGUAR%\sample\wst_samples\DynamicClient\client\DynClient.java

package client;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.rpc.ParameterMode;
import java.net.URL;

/**
 * @author Sybase
 */
public class DynClient
{
    public static void main(String[] args) throws Exception
    {
        /*
    	 * URL of the web service.
    	 * See under eclipse ->Web services perspective ->'ws'->AddNumbers
    	 */
        String url = "http://localhost:8080/ws/services/AddNumbers";
        Integer number1 = new Integer(1);
        Integer number2 = new Integer(2);


        /*
    	 * Create an instance of the Call object.
    	 */
        Call call = (Call)service.createCall();
        System.out.println("Connecting to: " + url);
        call.setTargetEndpointAddress(new URL(url));
        /*
    	 * Set parameters definitions in the call object.
    	 */
        call.addParameter("n1", XMLType.XSD_INT, ParameterMode.IN);
        call.addParameter("n2", XMLType.XSD_INT, ParameterMode.IN);
        /*
    	 * Set definition of the return type.
    	 */
        call.setReturnType(XMLType.XSD_INT);
        /*
    	 * Name of the method to invoke.
    	 */
        call.setOperation("add");
        System.out.println("Adding: " + number1 + " & " + number2);
        /*
    	 * Finally invoke the method.
    	 */
        Integer i = (Integer)call.invoke(new Object[]
        {
            number1, number2
        }
        );
        System.out.println("Result: "+ i);
    }
}




Copyright © 2005. Sybase Inc. All rights reserved. Document style client