Web Services ClientsA JAX-RPC Client


The full JAX-RPC programming model is available to J2EE applications and clients. I won't cover the full range of client programming techniques, but let's look briefly at the client used so far to test the deployed web services. The client, shown in the following listing, illustrates the dynamic proxy invocation mechanism:

 package org.jboss.chap12.client; import org.jboss.chap12.hello.Hello; import javax.xml.rpc.Service; import javax.xml.rpc.ServiceFactory; import javax.xml.namespace.QName; import java.net.URL; public class HelloClient {     public static void main(String[] args)         throws Exception     {         String urlstr   = args[0];         String argument = args[1];         System.out.println("Contacting webservice at " + urlstr);         URL url =  new URL(urlstr);         QName qname = new QName("http://hello.chap12.jboss.org/",                                 "HelloService");         ServiceFactory factory = ServiceFactory.newInstance();         Service        service = factory.createService(url, qname);         Hello          hello   = (Hello) service.getPort(Hello.class);         System.out.println("hello.hello(" + argument + ")");         System.out.println("output:" + hello.hello(argument));     } } 

This JAX-RPC client uses the Hello service endpoint interface and creates a dynamic proxy to speak to the service advertised by the WSDL at the URL passed in as a commandline argument. For illustrative purposes, here is another variation of web services invocation that doesn't use the service endpoint interface. This is known as the dynamic invocation interface (DII). Using DII, it's possible to refer to a specific port and operation by name. Think of it as reflection for web services. The client code is shown in the following listing:

 package org.jboss.chap12.client; import org.jboss.chap12.hello.Hello; import javax.xml.rpc.Service; import javax.xml.rpc.ServiceFactory; import javax.xml.namespace.QName; import java.net.URL; public class HelloClient {     public static void main(String[] args)         throws Exception     {         String urlstr   = args[0];         String argument = args[1];         System.out.println("Contacting webservice at " + urlstr);         URL url =  new URL(urlstr);         QName qname = new QName("http://hello.chap12.jboss.org/",                                 "HelloService");         ServiceFactory factory = ServiceFactory.newInstance();         Service        service = factory.createService(url, qname);         Hello          hello   = (Hello) service.getPort(Hello.class);         System.out.println("hello.hello(" + argument + ")");         System.out.println("output:" + hello.hello(argument));     } } 

The following two commands can be used to run DII client against both the JSE and EJB web services you have created:

 [examples]$ ant -Dchap=chap12 -Dex=1b run-example [examples]$ ant -Dchap=chap12 -Dex=2b run-example 



JBoss 4. 0(c) The Official Guide
JBoss 4.0 - The Official Guide
ISBN: B003D7JU58
EAN: N/A
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net