📄 jaxrpc5.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Creating Web Service Clients with JAX-RPC</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="JAXRPC4.html" /> <link rel="Next" href="JAXRPC6.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JAXRPC4.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXRPC6.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp79960"> </a><h2 class="pHeading1">Creating Web Service Clients with JAX-RPC</h2><a name="wp79964"> </a><p class="pBody">This section shows how to create and run these types of clients:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80245"> </a><div class="pSmartList1"><li>Static stub</li></div><a name="wp80246"> </a><div class="pSmartList1"><li>Dynamic proxy</li></div><a name="wp80247"> </a><div class="pSmartList1"><li>Dynamic invocation interface (DII)</li></div><a name="wp117498"> </a><div class="pSmartList1"><li>J2EE application client</li></div></ul></div><a name="wp80249"> </a><p class="pBody">When you run these client examples, they will access the <code class="cCode">MyHelloService</code> that you deployed in the preceding section.</p><a name="wp79965"> </a><h3 class="pHeading2">Static Stub Client Example</h3><a name="wp79972"> </a><p class="pBody">This example resides in the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/staticstub/</code> directory.</p><a name="wp81536"> </a><p class="pBody"><code class="cCode">HelloClient</code> is a stand-alone program that calls the <code class="cCode">sayHello</code> method of the <code class="cCode">MyHelloService</code>. It makes this call through a stub, a local object which acts as a proxy for the remote service. Because the stub is created before runtime (by <code class="cCode">wscompile</code>), it is usually called a <em class="cEmphasis">static stub</em>.</p><a name="wp81540"> </a><h4 class="pHeading3">Coding the Static Stub Client</h4><a name="wp81565"> </a><p class="pBody">Before it can invoke the remote methods on the stub the client performs these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81547"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">Stub</code> object<code class="cCode">:</code></li></div><a name="wp82797"> </a><p class="pBodyRelative"><code class="cCode">(Stub)(new MyHelloService_Impl().getHelloIFPort())</code></p><a name="wp81549"> </a><p class="pBodyRelative">The code in this method is implementation-specific because it relies on a <code class="cCode">MyHelloService_Impl</code> object, which is not defined in the specifications. The <code class="cCode">MyHelloService_Impl</code> class will be generated by <code class="cCode">wscompile</code> in the following section.</p><a name="wp82816"> </a><div class="pSmartList1"><li>Sets the endpoint address that the stub uses to access the service:</li></div><a name="wp82821"> </a><p class="pBodyRelative"><code class="cCode">stub._setProperty<br />(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);</code></p><a name="wp82824"> </a><p class="pBodyRelative">At runtime, the endpoint address is passed to <code class="cCode">HelloClient</code> in <code class="cCode">args[0]</code> as a command-line parameter, which <code class="cCode">asant</code> gets from the <code class="cCode">endpoint.address</code> property in the <code class="cCode">build.properties</code> file. This address must match the one you set for the service in <a href="JAXRPC4.html#wp117474">Specifying the Endpoint Address</a>.</p><a name="wp81553"> </a><div class="pSmartList1"><li>Casts <code class="cCode">stub</code> to the service endpoint interface, <code class="cCode">HelloIF</code>:</li></div><a name="wp81554"> </a><p class="pBodyRelative"><code class="cCode">HelloIF hello = (HelloIF)stub;</code></p></ol></div><a name="wp81544"> </a><p class="pBody">Here is the full source code listing for the <code class="cCode">HelloClient.java</code> file, which is located in the directory <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/staticstub/src/</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package staticstub;import javax.xml.rpc.Stub;public class HelloClient { private String endpointAddress; public static void main(String[] args) { System.out.println("Endpoint address = " + args[0]); try { Stub stub = createProxy(); stub._setProperty (javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]); HelloIF hello = (HelloIF)stub; System.out.println(hello.sayHello("Duke!")); } catch (Exception ex) { ex.printStackTrace(); } } private static Stub createProxy() { // Note: MyHelloService_Impl is implementation-specific. return (Stub) (new MyHelloService_Impl().getHelloIFPort()); }}<a name="wp82748"> </a></pre></div><a name="wp117527"> </a><h4 class="pHeading3">Building and Running the Static Stub Client</h4><a name="wp117528"> </a><p class="pBody">Before performing the steps in this section, you must first create and deploy <code class="cCode">MyHelloService</code> as described in <a href="JAXRPC4.html#wp115211">Creating a Web Service with JAX-RPC</a>.</p><a name="wp117532"> </a><p class="pBody">To build and package the client, go to the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/staticstub/</code> directory and type the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build<a name="wp117533"> </a></pre></div><a name="wp117534"> </a><p class="pBody">The preceding command invokes three <code class="cCode">asant</code> tasks: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp117535"> </a><div class="pSmartList1"><li><code class="cCode">generate-stubs</code> </li></div><a name="wp117536"> </a><div class="pSmartList1"><li><code class="cCode">compile-client</code> </li></div><a name="wp117537"> </a><div class="pSmartList1"><li><code class="cCode">package-client</code> </li></div></ul></div><a name="wp117538"> </a><p class="pBody">The <code class="cCode">generate-stubs</code> task runs the <code class="cCode">wscompile</code> tool as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">wscompile -gen:client -d build -classpath build config-wsdl.xml<a name="wp117539"> </a></pre></div><a name="wp117542"> </a><p class="pBody">This <code class="cCode">wscompile</code> command reads the <code class="cCode">MyHelloService.wsdl</code> file that was generated in <a href="JAXRPC4.html#wp79980">Building the Service</a>. The <code class="cCode">wscompile</code> command generates files based on the information in the WSDL file and on the command-line flags. The <code class="cCode">-gen:client</code> flag instructs <code class="cCode">wscompile</code> to generate the stubs, other runtime files such as serializers, and value types. The <code class="cCode">-d</code> flag tells the tool to write the output to the <code class="cCode">build/staticstub</code> subdirectory. The tool reads the following <code class="cCode">config-wsdl.xml </code>file, which specifies the location of the WSDL file:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version="1.0" encoding="UTF-8"?><configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <wsdl location="build/MyHelloService.wsdl" packageName="staticstub"/></configuration><a name="wp117544"> </a></pre></div><a name="wp117545"> </a><p class="pBody">The c<code class="cCode">ompile-client</code> task compiles <code class="cCode">src/HelloClient.java</code> and writes the class file to the <code class="cCode">build </code>subdirectory.</p><a name="wp117546"> </a><p class="pBody">The <code class="cCode">package-client</code> task packages the files created by the ge<code class="cCode">nerate-stubs</code> and <code class="cCode">compile-client</code> tasks into the <code class="cCode">dist/client.jar</code> file. Except for the <code class="cCode">HelloClient.class</code>, all of the files in <code class="cCode">client.jar</code> were created by <code class="cCode">wscompile</code>. Note that <code class="cCode">wscompile</code> generated the <code class="cCode">HelloIF.class</code> based on the information it read from the <code class="cCode">MyHelloService.wsdl</code> file.</p><a name="wp117547"> </a><p class="pBody">To run the client, type the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant run<a name="wp117548"> </a></pre></div><a name="wp117549"> </a><p class="pBody">The client should display the following line:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Hello Duke!<a name="wp117550"> </a></pre></div><a name="wp79973"> </a><h3 class="pHeading2">Dynamic Proxy Client Example</h3><a name="wp80274"> </a><p class="pBody">This example resides in the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/dynamicproxy/</code> directory.</p><a name="wp80442"> </a><p class="pBody">The client in the preceding section used a static stub for the proxy. In contrast, the client example in this section calls a remote procedure through a <span style="font-style: oblique">dynamic proxy</span>, a class that is created during runtime. Although the source code for the static stub client relied on an implementation-specific class, the code for the dynamic proxy client does not have this limitation. </p><a name="wp80455"> </a><h4 class="pHeading3">Coding the Dynamic Proxy Client </h4><a name="wp81580"> </a><p class="pBody">The <code class="cCode">DynamicProxyHello</code> program constructs the dynamic proxy as follows:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81581"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">Service</code> object named <code class="cCode">helloService:</code></li></div><a name="wp81582"> </a><p class="pBodyRelative"><code class="cCode">Service helloService =<br /> serviceFactory.createService(helloWsdlUrl,<br /> new QName(nameSpaceUri, serviceName));</code></p><a name="wp81583"> </a><p class="pBodyRelative">A <code class="cCode">Service</code> object is a factory for proxies. To create the <code class="cCode">Service</code> object (<code class="cCode">helloService</code>), the program calls the <code class="cCode">createService</code> method on another type of factory, a <code class="cCode">ServiceFactory</code> object.</p><a name="wp81584"> </a><p class="pBodyRelative">The <code class="cCode">createService</code> method has two parameters, the URL of the WSDL file and a <code class="cCode">QName</code> object. At runtime, the client gets information about the service by looking up its WSDL. In this example, the URL of the WSDL file points to the WSDL that was deployed with <code class="cCode">MyHelloService</code>:</p><a name="wp81585"> </a><p class="pBodyRelative"><code class="cCode">http://localhost:8080/hello-jaxrpc/hello?WSDL</code></p><a name="wp81586"> </a><p class="pBodyRelative">A <code class="cCode">QName</code> object is a tuple that represents an XML qualified name. The tuple is composed of a namespace URI and the local part of the qualified name. In the <code class="cCode">QName</code> parameter of the <code class="cCode">createService</code> invocation, the local part is the service name, <code class="cCode">MyHelloService</code>.</p><a name="wp81587"> </a><div class="pSmartList1"><li>The program creates a proxy (<code class="cCode">myProxy</code>) with a type of the service endpoint interface (<code class="cCode">HelloIF</code>):</li></div><a name="wp81588"> </a><p class="pBodyRelative">d<code class="cCode">ynamicproxy.HelloIF myProxy = <br /> (dynamicproxy.HelloIF)helloService.getPort(<br /> new QName(nameSpaceUri, portName), <br /> dynamicproxy.HelloIF.class);</code></p><a name="wp81589"> </a><p class="pBodyRelative">The <code class="cCode">helloService</code> object is a factory for dynamic proxies. To create <code class="cCode">myProxy</code>, the program calls the <code class="cCode">getPort</code> method of <code class="cCode">helloService</code>. This method has two parameters: a <code class="cCode">QName</code> object that specifies the port name and a <code class="cCode">java.lang.Class</code> object for the service endpoint interface (<code class="cCode">HelloIF</code>). The <code class="cCode">HelloIF</code> class is generated by <code class="cCode">wscompile</code>. The port name (<code class="cCode">HelloIFPort</code>) is specified by the WSDL file.</p></ol></div><a name="wp81577"> </a><p class="pBody">Here is the listing for the <code class="cCode">HelloClient.java</code> file, located in the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/dynamicproxy/src/</code> directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package dynamicproxy;import java.net.URL;import javax.xml.rpc.Service;import javax.xml.rpc.JAXRPCException;import javax.xml.namespace.QName;import javax.xml.rpc.ServiceFactory;import dynamicproxy.HelloIF;public class HelloClient {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -