⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jaxrpc5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
    public static void main(String[] args) {        try {            String UrlString = args[0] + &quot;?WSDL&quot;;            String nameSpaceUri = &quot;urn:Foo&quot;;            String serviceName = &quot;MyHelloService&quot;;            String portName = &quot;HelloIFPort&quot;;            System.out.println(&quot;UrlString = &quot; + UrlString);            URL helloWsdlUrl = new URL(UrlString);                        ServiceFactory serviceFactory =                ServiceFactory.newInstance();                        Service helloService =                serviceFactory.createService(helloWsdlUrl,                 new QName(nameSpaceUri, serviceName));                        dynamicproxy.HelloIF myProxy =                 (dynamicproxy.HelloIF)                 helloService.getPort(                new QName(nameSpaceUri, portName),                 dynamicproxy.HelloIF.class);             System.out.println(myProxy.sayHello(&quot;Buzz&quot;));        } catch (Exception ex) {            ex.printStackTrace();        }     } } <a name="wp82998"> </a></pre></div><a name="wp80536"> </a><h4 class="pHeading3">Building and Running the Dynamic Proxy Client</h4><a name="wp91169"> </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="wp91173"> </a><p class="pBody">To build and package the client, go to the <code class="cVariable">&lt;INSTALL&gt;</code><code class="cCode">/j2eetutorial14/examples/jaxrpc/dynamicproxy/</code> directory and type the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build<a name="wp117575"> </a></pre></div><a name="wp80565"> </a><p class="pBody">The preceding command runs these tasks: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp83064"> </a><div class="pSmartList1"><li><code class="cCode">generate-interface</code></li></div><a name="wp83065"> </a><div class="pSmartList1"><li><code class="cCode">compile-client</code></li></div><a name="wp83066"> </a><div class="pSmartList1"><li><code class="cCode">package-dynamic</code></li></div></ul></div><a name="wp83129"> </a><p class="pBody">The <code class="cCode">generate-interface</code> task runs <code class="cCode">wscompile with the -import</code> option. The <code class="cCode">wscompile</code> command reads the <code class="cCode">MyHelloService.wsdl</code> file and generates the service endpoint interface class (<code class="cCode">HelloIF.class</code>). Although this <code class="cCode">wscompile</code> invocation also creates stubs, the dynamic proxy client does not use these stubs, which are required only by static stub clients. </p><a name="wp83146"> </a><p class="pBody">The c<code class="cCode">ompile-client</code> task compiles the <code class="cCode">src/HelloClient.java</code> file.</p><a name="wp80588"> </a><p class="pBody">The <code class="cCode">package-dynamic</code> task creates the <code class="cCode">dist/client.jar</code> file, which contains <code class="cCode">HelloIF.class</code> and <code class="cCode">HelloClient.class</code>.</p><a name="wp80598"> </a><p class="pBody">To run the client, type the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant run<a name="wp117580"> </a></pre></div><a name="wp83160"> </a><p class="pBody">The client should display the following line:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Hello Buzz<a name="wp83161"> </a></pre></div><a name="wp79975"> </a><h3 class="pHeading2">Dynamic Invocation Interface (DII) Client Example</h3><a name="wp79976"> </a><p class="pBody">This example resides in the <code class="cVariable">&lt;INSTALL&gt;</code><code class="cCode">/j2eetutorial14/examples/jaxrpc/dii/</code> directory.</p><a name="wp81601"> </a><p class="pBody">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. In contrast to a static stub or dynamic proxy client, a DII client does not require runtime classes generated by wscompile. However, as you'll see in the following section, the source code for a DII client is more complicated than the code of the other two types of clients.</p><a name="wp81602"> </a><p class="pBody">This example is for advanced users who are familiar with WSDL documents. (See <a  href="JAXRPC7.html#wp89837">Further Information</a>.)</p><a name="wp80611"> </a><h4 class="pHeading3">Coding the DII Client</h4><a name="wp81622"> </a><p class="pBody">The <code class="cCode">DIIHello</code> program performs these steps:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81623"> </a><div class="pSmartList1"><li>Creates a <code class="cCode">Service</code> object.</li></div><a name="wp81624"> </a><p class="pBodyRelative"><code class="cCode">Service service =<br />    factory.createService(new QName(qnameService));</code></p><a name="wp81625"> </a><p class="pBodyRelative">To get a <code class="cCode">Service</code> object, the program invokes the <code class="cCode">createService</code> method of a <code class="cCode">ServiceFactory</code> object. The parameter of the <code class="cCode">createService</code> method is a <code class="cCode">QName</code> object that represents the name of the service, <code class="cCode">MyHelloService</code>. The WSDL file specifies this name as follows:</p><a name="wp81626"> </a><p class="pBodyRelative"><code class="cCode"> &lt;service name=&quot;MyHelloService&quot;&gt;</code></p><a name="wp81627"> </a><div class="pSmartList1"><li>From the <code class="cCode">Service</code> object, creates a <code class="cCode">Call</code> object:</li></div><a name="wp81628"> </a><p class="pBodyRelative"><code class="cCode">QName port = new QName(qnamePort);<br />Call call = service.createCall(port);</code></p><a name="wp81629"> </a><p class="pBodyRelative">A <code class="cCode">Call</code> object supports the dynamic invocation of the remote procedures of a service. To get a <code class="cCode">Call</code> object, the program invokes the <code class="cCode">Service</code> object's <code class="cCode">createCall</code> method. The parameter of <code class="cCode">createCall</code> is a <code class="cCode">QName</code> object that represents the service endpoint interface, <code class="cCode">MyHelloServiceRPC</code>. In the WSDL file, the name of this interface is designated by the <code class="cCode">portType</code> element:</p><a name="wp81630"> </a><p class="pBodyRelative"><code class="cCode"> &lt;portType name=&quot;HelloIF&quot;&gt;</code></p><a name="wp81631"> </a><div class="pSmartList1"><li>Sets the service endpoint address on the <code class="cCode">Call</code> object:</li></div><a name="wp81632"> </a><p class="pBodyRelative"><code class="cCode">call.setTargetEndpointAddress(endpoint);</code></p><a name="wp81633"> </a><p class="pBodyRelative">In the WSDL file, this address is specified by the <code class="cCode">&lt;soap:address&gt;</code> element.</p><a name="wp81635"> </a><div class="pSmartList1"><li>Sets these properties on the <code class="cCode">Call</code> object:</li></div><a name="wp81636"> </a><p class="pBodyRelative"><code class="cCode">SOAPACTION_USE_PROPERTY<br />SOAPACTION_URI_PROPERTY<br />ENCODING_STYLE_PROPERTY</code></p><a name="wp81648"> </a><p class="pBodyRelative">To learn more about these properties, refer to the SOAP and WSDL documents listed in <a  href="JAXRPC7.html#wp89837">Further Information</a>.</p><a name="wp81652"> </a><div class="pSmartList1"><li>Specifies the method's return type, name, and parameter:</li></div><a name="wp81653"> </a><p class="pBodyRelative"><code class="cCode">QName QNAME_TYPE_STRING = new QName(NS_XSD, &quot;string&quot;);<br />call.setReturnType(QNAME_TYPE_STRING);<br /><br />call.setOperationName(new QName(BODY_NAMESPACE_VALUE,<br />    &quot;sayHello&quot;));<br /><br />call.addParameter(&quot;String_1&quot;, QNAME_TYPE_STRING,<br />    ParameterMode.IN);</code></p><a name="wp81654"> </a><p class="pBodyRelative">To specify the return type, the program invokes the <code class="cCode">setReturnType</code> method on the <code class="cCode">Call</code> object. The parameter of <code class="cCode">setReturnType</code> is a <code class="cCode">QName</code> object that represents an XML string type.</p><a name="wp81655"> </a><p class="pBodyRelative">The program designates the method name by invoking the <code class="cCode">setOperationName</code> method with a <code class="cCode">QName</code> object that represents <code class="cCode">sayHello</code>.</p><a name="wp81656"> </a><p class="pBodyRelative">To indicate the method parameter, the program invokes the <code class="cCode">addParameter</code> method on the <code class="cCode">Call</code> object. The <code class="cCode">addParameter</code> method has three arguments: a <code class="cCode">String</code> for the parameter name (<code class="cCode">String_1</code>), a <code class="cCode">QName</code> object for the XML type, and a <code class="cCode">ParameterMode</code> object to indicate the passing mode of the parameter (<code class="cCode">IN</code>).</p><a name="wp81657"> </a><div class="pSmartList1"><li>Invokes the remote method on the <code class="cCode">Call</code> object:</li></div><a name="wp81658"> </a><p class="pBodyRelative"><code class="cCode">String[] params = { &quot;Murphy&quot; };<br />String result = (String)call.invoke(params);</code></p><a name="wp81659"> </a><p class="pBodyRelative">The program assigns the parameter value (<code class="cCode">Murphy</code>) to a <code class="cCode">String</code> array (<code class="cCode">params</code>) and then executes the <code class="cCode">invoke</code> method with the <code class="cCode">String</code> array as an argument.</p></ol></div><a name="wp81615"> </a><p class="pBody">Here is the listing for the <code class="cCode">HelloClient.java</code> file, located in the <code class="cVariable">&lt;INSTALL&gt;</code><code class="cCode">/j2eetutorial14/examples/jaxrpc/dii/src/</code> directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package dii;import javax.xml.rpc.Call;import javax.xml.rpc.Service;import javax.xml.rpc.JAXRPCException;import javax.xml.namespace.QName;import javax.xml.rpc.ServiceFactory;import javax.xml.rpc.ParameterMode;public class HelloClient {    private static String qnameService = &quot;MyHelloService&quot;;    private static String qnamePort = &quot;HelloIF&quot;;    private static String BODY_NAMESPACE_VALUE =         &quot;urn:Foo&quot;;    private static String ENCODING_STYLE_PROPERTY =         &quot;javax.xml.rpc.encodingstyle.namespace.uri&quot;;     private static String NS_XSD =         &quot;http://www.w3.org/2001/XMLSchema&quot;;    private static String URI_ENCODING =         &quot;http://schemas.xmlsoap.org/soap/encoding/&quot;;    public static void main(String[] args) {        System.out.println(&quot;Endpoint address = &quot; + args[0]);        try {            ServiceFactory factory =                 ServiceFactory.newInstance();            Service service =                 factory.createService(                new QName(qnameService));                QName port = new QName(qnamePort);                Call call = service.createCall(port);            call.setTargetEndpointAddress(args[0]);                call.setProperty(Call.SOAPACTION_USE_PROPERTY,                 new Boolean(true));            call.setProperty(Call.SOAPACTION_URI_PROPERTY                 &quot;&quot;);            call.setProperty(ENCODING_STYLE_PROPERTY,                URI_ENCODING);            QName QNAME_TYPE_STRING =                         new QName(NS_XSD, &quot;string&quot;);            call.setReturnType(QNAME_TYPE_STRING);            call.setOperationName(                new QName(BODY_NAMESPACE_VALUE,&quot;sayHello&quot;));            call.addParameter(&quot;String_1&quot;, QNAME_TYPE_STRING,                 ParameterMode.IN);            String[] params = { &quot;Murph!&quot; };            String result = (String)call.invoke(params);            System.out.println(result);        } catch (Exception ex) {            ex.printStackTrace();        }    }}<a name="wp80691"> </a></pre></div><a name="wp80626"> </a><h4 class="pHeading3">Building and Running the DII Client</h4><a name="wp80627"> </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="wp80631"> </a><p class="pBody">To build and package the client, go to the <code class="cVariable">&lt;INSTALL&gt;</code><code class="cCode">/j2eetutorial14/examples/jaxrpc/dii/</code> directory and type the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build<a name="wp117600"> </a></pre></div><a name="wp80624"> </a><p class="pBody">This <code class="cCode">build</code> task compiles <code class="cCode">HelloClient</code> and packages it into the <code class="cCode">dist/client.jar</code> file. Unlike the previous client examples, the DII client does not require files generated by <code class="cCode">wscompile</code>.</p><a name="wp80809"> </a><p class="pBody">To run the client, type this command:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant run<a name="wp117605"> </a></pre></div><a name="wp83188"> </a><p class="pBody">The client should display this line:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -