The JavaTM Web Services Tutorial
Home
TOC
PREV TOP NEXT

The Dynamic Invocation Interface

With the dynamic invocation interface (DII), a client can call a remote procedure even if the signature of the remote procedure or the name of the service are unknown until runtime.

When to Use DII

Although DII clients are flexible, they are more complex than clients that use static stubs. (For an example of a client with static stubs, see Coding the Client.) Compared to clients with static stubs, clients with DII are more difficult to code, debug, and test. Therefore, a client should use DII only if it cannot use static stubs.

However, there are two cases that require the flexibility of a DII client. The first case is a service broker that dynamically discovers services, configures the remote calls, and executes the calls. For example, an application for an online clothing store might access a service broker that specializes in shipping. This broker would use the Java API for XML Registries (JAXR) to locate the services of the shipping companies that meet certain criteria, such as low cost or fast delivery time. At runtime, the broker uses DII to call remote procedures on the web services of the shipping companies. As an intermediary between the clothing store and the shipping companies, the broker offers benefits to all parties. For the clothing store, it simplifies the shipping process, and for the shipping companies, it finds customers.

The second case requiring DII is less common: a development environment that does not support the generation of static stubs.

A DII Client Example

The source code for this example is in the HelloClient.java file of the docs/tutorial/examples/jaxrpc/dynamic directory.

DII Classes and Interfaces

The HelloClient program uses the following interfaces and classes for dynamic invocation.

DII HelloClient Listing

Here is the full listing for the HelloClient.java file of the docs/tutorial/examples/jaxrpc/dynamic directory. Note how much longer the DII client is than the static stub client shown in Coding the Client.

package dynamic;	
	
import javax.xml.rpc.Call;	
import javax.xml.rpc.Service;	
import javax.xml.rpc.JAXRPCException;	
import javax.xml.rpc.namespace.QName;	
import javax.xml.rpc.ServiceFactory;	
import javax.xml.rpc.ParameterMode;	
	
public class HelloClient {	
	
    private static String qnameService = "Hello";	
    private static String qnamePort = "HelloIF";	
	
    private static String BODY_NAMESPACE_VALUE = 	
        "http://hello.org/wsdl";	
    private static String ENCODING_STYLE_PROPERTY =	
        "javax.xml.rpc.encodingstyle.namespace.uri"; 	
    private static String NS_XSD = 	
        "http://www.w3.org/2001/XMLSchema";	
    private static String URI_ENCODING =	
         "http://schemas.xmlsoap.org/soap/encoding/";	
	
    public static void main(String[] args) {	
        try {	
            String endpoint= args[0];	
            	
            ServiceFactory factory = 	
                ServiceFactory.newInstance();	
            Service service = 	
                factory.createService(new QName(qnameService));	
	
            QName port = new QName(qnamePort);	
    	
            Call call = service.createCall();	
            call.setPortTypeName(port);	
            call.setTargetEndpointAddress(endpoint);	
    	
            call.setProperty(Call.SOAPACTION_USE_PROPERTY, 	
                new Boolean(true));	
            call.setProperty(Call.SOAPACTION_URI_PROPERTY, 	
                "");	
            call.setProperty(ENCODING_STYLE_PROPERTY,	
                URI_ENCODING);	
            QName QNAME_TYPE_STRING = new QName(NS_XSD, 	
                "string");	
            call.setReturnType(QNAME_TYPE_STRING);	
	
            call.setOperationName(new QName	
                (BODY_NAMESPACE_VALUE, "sayHello"));	
            call.addParameter("String_1", QNAME_TYPE_STRING, 	
                ParameterMode.PARAM_MODE_IN);	
            String[] params = { new String("Duke!") };	
	
            String result = (String)call.invoke(params);	
            System.out.println(result);	
	
        } catch (Exception ex) {	
            ex.printStackTrace();	
        }	
    }	
}
 

Building and Running the DII Example

Perform the following steps:

  1. If you haven't already done so, follow the instructions in Setting Up.
  2. Go to the docs/tutorial/examples/jaxrpc/dynamic directory.
  3. Type the following commands:
       ant build	
       ant install	
       ant run
     
    

The client should display the following line:

A dynamic hello to Duke!
 
Home
TOC
PREV TOP NEXT