The JavaTM Web Services Tutorial
Home
TOC
Index
PREV TOP NEXT
Divider

A Dynamic Proxy Client Example

The client in the section, A Simple Example: HelloWorld, used a static stub for the proxy. In contrast, the client example in this section calls a remote procedure through a dynamic proxy, a class that is created during runtime. Before creating the proxy class, the client gets information about the service by looking up its WSDL document.

Dynamic Proxy HelloClient Listing

Here is the full listing for the HelloClient.java file of the <JWSDP_HOME>/docs/tutorial/examples/jaxrpc/proxy directory.

package proxy;

import java.net.URL;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;

public class HelloClient {

    public static void main(String[] args) {
        try {

            String UrlString =
                "http://localhost:8080/ProxyHelloWorld.wsdl";
            String nameSpaceUri = "http://proxy.org/wsdl";
            String serviceName = "HelloWorld";
            String portName = "HelloIFPort";

            URL helloWsdlUrl = new URL(UrlString);
            
            ServiceFactory serviceFactory =
                ServiceFactory.newInstance();
            
            Service helloService =
                serviceFactory.createService(helloWsdlUrl, 
                new QName(nameSpaceUri, serviceName));
            
            HelloIF myProxy = (HelloIF) helloService.getPort(
                new QName(nameSpaceUri, portName), 
                proxy.HelloIF.class); 

            System.out.println(myProxy.sayHello("Buzz"));

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

Building and Running the Dynamic Proxy Example

Perform the following steps:

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

The client should display the following line:

A dynamic proxy hello to Buzz!
 
Divider
Home
TOC
Index
PREV TOP NEXT
Divider

This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.

All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.