Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 145 146 147 148 149 150 < 151 > 152 153 154 155 156 157 .. 166 >> Next

001: <?xml version="1.0" encoding="UTF-8" ?>
002: <definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
003: xmlns:tns="http://org.javapitfalls.item48/wsdl/SimpleTest"
Listing 48.2 WSDL for a simple Web service
Performance Pitfalls in JAX-RPC Application Clients 419
005 xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
004 xmlns:soap=”http://schemas.xmlsoap.org/wsdl/soap/”
006 name=”SimpleTest”
007 targetNamespace=”http://org.javapitfalls.item48/wsdl/SimpleTest”>
008 <types />
009 <message name=”SimpleTestIF_doStuff”>
010 <part name=”String_1” type=”xsd:string” />
011 </message>
012 <message name=”SimpleTestIF_doStuffResponse”>
013 <part name=”result” type=”xsd:string” />
014 </message>
015 <portType name=”SimpleTestIF”>
016 <operation name=”doStuff” parameterOrder=”String_1”>
017 <input message=”tns:SimpleTestIF_doStuff” />
018 <output message=”tns:SimpleTestIF_doStuffResponse” />
019 </operation>
020 </portType>
021 <binding name=”SimpleTestIFBinding” type=”tns:SimpleTestIF”>
022 <operation name=”doStuff”>
023 <input>
024 <soap:body
025 encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/”
026 use=”encoded”
027 namespace=”http://org.javapitfalls.item48/wsdl/SimpleTest”
028 />
029 </input>
030 <output>
031 <soap:body
032 encodingStyle=”http://schemas.xmlsoap.org/soap/encoding/”
033 use=”encoded”
034 namespace=”http://org.javapitfalls.item48/wsdl/SimpleTest”
035 />
036 </output>
037 <soap:operation soapAction=”” />
038 </operation>
039 <soap:binding transport=”http://schemas.xmlsoap.org/soap/http”
040 style=”rpc” />
041 </binding>
042 <service name=”SimpleTest”>
043 <port name=”SimpleTestIFPort” binding=”tns:SimpleTestIFBinding”>
044 <soap:address xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/”
045 location=”http://localhost:8080/simpletest-jaxrpc/simpletest” />
046 </port>
047 </service>
048 </definitions>
Listing 48.2 (continued)
420 Item 48
Next, we will get to the meat of this pitfall: writing different clients that will call the doStuff() method on the "SimpleTest" Web service. In the next sections, we show different approaches to building JAX-RPC clients.
A Simple Client That Uses Precompiled Stub Classes
The first, and easiest, way to call an RPC-style Web service is by using precompiled stubs. To create these stubs, the Java Web Services Developer Pack contains a tool called "wscompile." As a result, a client can communicate with the Web service interface using the java.xml.rpc.Stub interface. The wscompile tool is run against a configuration file listing details about the Web services (the URL of the WSDL, the package name, etc). When the wscompile tool runs successfully, it processes the WSDL for our Web service and generates proxy stubs so that our client can invoke methods on our SimpleTestIF interface at runtime.
Listing 48.3 shows a client that uses precompiled stubs. Lines 37 to 40 show the static createProxy() method that returns the stub that is cast to the SimpleTestIF interface in line 16. As a result, you do not have to know anything about SOAP or WSDL. Instead, you write code like you're using RMI. Note that in lines 27 and 28, we are printing out the invocation setup time. This will be used later in this pitfall item to compare pre-invocation times with our other techniques.
001 package 8 4 m e t i s l l a f t i p a v a j & r o
002
003 import javax.xml.rpc.*;
004 import javax.xml.namespace.*;
005
006 public class NoDynamicStuffClient
007 {
008 public static void main(String[] args)
009 {
010 try
011 {
012 long initial, afterproxy, prelnvokeTime, invokeTime;
013
014 initial = System.currentTimeMillis();
015
016 SimpleTestIF simpletest = (SimpleTestIF)createProxy();
017
018 afterproxy = System.currentTimeMillis();
019 prelnvokeTime = afterproxy - initial;
020
021 //Now, invoke our method
022
023 String response =
024 simpletest.doStuff(”Hi there from NoDynamicStuffClient!");
025 //Print out stats
026
Listing 48.3 A simple client using precompiled stubs
Performance Pitfalls in JAX-RPC Application Clients 421
027 System.out.println("Invocation setup took "
028 + preInvokeTime + " milliseconds.");
029
030 }
031 catch ( Exception ex )
032 {
033 ex.printStackTrace();
034 }
035 }
036
037 private static Stub createProxy()
038 {
039 return(Stub)(new SimpleTest_Impl(). getSimpleTestIFPort());
040 }
041 }
Listing 48.3 (continued)
A Client That Uses Dynamic Proxies for Access
JAX-RPC includes the concept of using dynamic proxies—a second way for clients to access Web services. A dynamic proxy class is a class that implements a list of interfaces specified at runtime, and using this technique, does not require pregeneration of the proxy class. Listing 48.4 shows an example of building such a proxy. In line 30, we create a new instance of ServiceFactory. We then specify the service in lines 32 and 33 by passing the URL for our WSDL in the example, as well as the javax.xml.name-space.QName, which represents the value of a qualified name as specified in the XML Schema specification. By calling the getPort() method on our javax.xml.rpc .Service class on lines 35 to 37, we have generated a proxy class that is cast to our original interface class from Listing 48.1.
Previous << 1 .. 145 146 147 148 149 150 < 151 > 152 153 154 155 156 157 .. 166 >> Next