Published on


Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Remote Procedure Calls and Web Services Zachary G. Ives University of Pennsylvania CIS 455 / 555 – Internet and Web Systems March 3, 2009
  2. 2. Today <ul><li>Reminder HW2 Milestone 1 due tonight </li></ul><ul><li>Distributed programming, concluded: RPC and Web Services </li></ul>
  3. 3. Some Common Modes of Building Distributed Applications <ul><li>Data-intensive: </li></ul><ul><ul><li>XQuery (fetch XML from multiple sites, produce new XML) </li></ul></ul><ul><ul><ul><li>Turing-complete functional programming language </li></ul></ul></ul><ul><ul><ul><li>Good for Web Services; not much support for I/O, etc. </li></ul></ul></ul><ul><ul><li>MapReduce (built over DHT or distributed file system) </li></ul></ul><ul><ul><ul><li>Single filter (map), followed by single aggregation (reduce) </li></ul></ul></ul><ul><ul><ul><li>Languages over it: Sawzall, Pig Latin, Dryad, … </li></ul></ul></ul><ul><li>Message passing / request-response: </li></ul><ul><ul><ul><li>e.g., over a DHT, sockets, or message queue </li></ul></ul></ul><ul><ul><li>Communication via asynchronous messages </li></ul></ul><ul><ul><li>Processing in message handler loop </li></ul></ul><ul><li>Function calls: </li></ul><ul><ul><li>Remote procedure call / remote method invocation </li></ul></ul>
  4. 4. Fully Synchronous Request/Response: Remote Procedure Calls <ul><li>Remote procedure calls have been around forever, including: </li></ul><ul><ul><li>COM+ </li></ul></ul><ul><ul><li>CORBA </li></ul></ul><ul><ul><li>Java RMI </li></ul></ul><ul><ul><li>The basic idea: put a function elsewhere in the system, call in distributed fashion but using standard languages, methods </li></ul></ul><ul><li>An RPC API defines a format for: </li></ul><ul><ul><li>Initiating a call on a particular server, generally in a reliable way </li></ul></ul><ul><ul><li>Sending parameters ( marshalling ) to the server </li></ul></ul><ul><ul><li>Receiving a return value, which may require marshalling as well </li></ul></ul><ul><li>And an RPC call is synchronous (i.e., it generally blocks) </li></ul>
  5. 5. A Remote Procedure Call Visualized time working server is busy request function server waits for req. client blocks RPC Server RPC Client
  6. 6. How RPC Generally Works <ul><li>You write an application with a series of functions </li></ul><ul><li>One of these functions, F , will be distributed remotely </li></ul><ul><li>You call a “stub generator” </li></ul><ul><ul><li>A caller stub emulates the function F : </li></ul></ul><ul><ul><ul><li>Opens a connection to the server </li></ul></ul></ul><ul><ul><ul><li>Requests F , marshalling all parameters </li></ul></ul></ul><ul><ul><ul><li>Receives F ’s return status and parameters </li></ul></ul></ul><ul><ul><li>A server stub emulates the caller: </li></ul></ul><ul><ul><ul><li>Receives a request for F with parameters </li></ul></ul></ul><ul><ul><ul><li>Unmarshals the parameters, invokes F </li></ul></ul></ul><ul><ul><ul><li>Takes F ’s return status (e.g., protection fault), return value, and marshals it back to the client </li></ul></ul></ul>
  7. 7. Passing Value Parameters <ul><li>Steps involved in doing remote computation through RPC </li></ul>2-8
  8. 8. RPC Components <ul><li>Generally, you need to write: </li></ul><ul><ul><li>Your function, in a compatible language </li></ul></ul><ul><ul><li>An interface definition , analogous to a C header file, so other people can program for F without having its source </li></ul></ul><ul><li>Generally, software will take the interface definition and generate the appropriate stubs </li></ul><ul><ul><li>(In the case of Java, RMIC knows enough about Java to run directly on the source file) </li></ul></ul><ul><li>The server stubs will generally run in some type of daemon process on the server </li></ul><ul><ul><li>Each function will need a globally unique name or GUID </li></ul></ul>
  9. 9. Parameter Passing Can Be Tricky Because of References <ul><li>The situation when passing an object by reference or by value. </li></ul>2-18
  10. 10. What Are the Hard Problems with RPC? Esp. Inter-Language RPC? <ul><li>Resolving different data formats between languages (e.g., Java vs. Fortran arrays) </li></ul><ul><li>Reliability, security </li></ul><ul><li>Finding remote procedures in the first place </li></ul><ul><li>Extensibility/maintainability </li></ul><ul><li>(Some of these might look familiar from when we talked about data exchange!) </li></ul>
  11. 11. Web Services <ul><li>Goal: provide an infrastructure for connecting components, building applications in a way similar to hyperlinks between data </li></ul><ul><li>It’s another distributed computing platform for the Web </li></ul><ul><ul><li>Goal: Internet-scale, language-independent, upwards-compatible where possible </li></ul></ul><ul><li>This one is based on many familiar concepts </li></ul><ul><ul><li>Standard protocols: HTTP </li></ul></ul><ul><ul><li>Standard marshalling formats: XML-based, XML Schemas </li></ul></ul><ul><ul><li>All new data formats are XML-based </li></ul></ul>
  12. 12. Three Parts to Web Services <ul><li>“ Wire” / messaging protocols </li></ul><ul><ul><li>Data encodings, RPC calls or document passing, etc. </li></ul></ul><ul><li>Describing what goes on the wire </li></ul><ul><ul><li>Schemas for the data </li></ul></ul><ul><li>“ Service discovery” </li></ul><ul><ul><li>Means of finding web services </li></ul></ul>
  13. 13. The Protocol Stacks of Web Services Enhanced + expanded from a figure from IBM’s “Web Services Insider”, http://www-106.ibm.com/developerworks/webservices/library/ws-ref2/ Other extensions SOAP Attachments WS-Security WS-AtomicTransaction, WS-Coordination SOAP, XML-RPC XML XML Schema Service Description (WSDL) Service Capabilities (WS-Capability) Message Sequencing Orchestration (WS-BPEL) Inspection Directory (UDDI) Wire Format Stack Discovery Stack Description Stack WS-Addressing High-level state transition + msging diagrams between modules
  14. 14. Messaging Protocol: SOAP <ul><li>Simple Object Access Protocol: XML-based format for passing parameters </li></ul><ul><ul><li>Has a SOAP header and body inside an envelope </li></ul></ul><ul><ul><li>As a defined HTTP binding ( POST with content-type of application/soap+xml ) </li></ul></ul><ul><ul><li>A companion SOAP Attachments encapsulates other (MIME) data </li></ul></ul><ul><ul><li>The header defines information about processing: encoding, signatures, etc. </li></ul></ul><ul><ul><ul><li>It’s extensible, and there’s a special attribute called mustUnderstand that is attached to elements that must be supported by the callee </li></ul></ul></ul><ul><ul><li>The body defines the actual application-defined data </li></ul></ul>
  15. 15. A SOAP Envelope <ul><li><SOAP-ENV:Envelope xmlns:SOAP-ENV=“http://www.w3.org/2001/12/soap-envelope” xmlns:xsd=“http://www.w3.org/www.w3.org/2001/XMLSchema-instance”> </li></ul><ul><li><SOAP-ENV:Header> </li></ul><ul><ul><li><t:Transaction xmlns:t=“www.mytrans.com” SOAP-ENV:mustUnderstand=“1” /> </li></ul></ul><ul><li></SOAP-ENV:Header> </li></ul><ul><li><SOAP-ENV:Body> </li></ul><ul><ul><li><m:PlaceOrder xmlns:m=“www.somewhere/there”> <orderno xsi:type=“xsd:string”>12</orderno> </m:PlaceOrder> </li></ul></ul><ul><li></SOAP-ENV:Body> </li></ul><ul><li></SOAP-ENV: Envelope> </li></ul>
  16. 16. Making a SOAP Call <ul><li>To execute a call to service PlaceOrder: </li></ul><ul><ul><li>POST /PlaceOrder HTTP/1.1 </li></ul></ul><ul><ul><li>Host: my.server.com </li></ul></ul><ul><ul><li>Content-Type: application/soap+xml; charset=“utf-8” </li></ul></ul><ul><ul><li>Content-Length: nnn </li></ul></ul><ul><ul><li><SOAP-ENV:Envelope> </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li></SOAP-ENV:Envelope> </li></ul></ul>
  17. 17. SOAP Return Values <ul><li>If successful, the SOAP response will generally be another SOAP message with the return data values, much like the request </li></ul><ul><li>If failure, the contents of the SOAP envelop will generally be a Fault message, along the lines of: </li></ul><ul><ul><li><SOAP-ENV:Body> </li></ul></ul><ul><ul><li><SOAP-ENV:Fault xmlns=“mynamespace”> </li></ul></ul><ul><ul><li><faultcode>SOAP-ENV:Client</faultcode> </li></ul></ul><ul><ul><li><faultstring>Could not parse message</faultstring> </li></ul></ul><ul><ul><li>… </li></ul></ul>
  18. 18. How Do We Declare Functions? <ul><li>WSDL is the interface definition language for web services </li></ul><ul><ul><li>Defines notions of protocol bindings, ports, and services </li></ul></ul><ul><ul><li>Generally describes data types using XML Schema </li></ul></ul><ul><li>In CORBA, this was called an IDL </li></ul><ul><li>In Java, the interface uses the same language as the Java code </li></ul>
  19. 19. A WSDL Service Service Port Port Port PortType Operation Operation PortType Operation Operation PortType Operation Operation Binding Binding Binding
  20. 20. Web Service Terminology <ul><li>Service: the entire Web Service </li></ul><ul><li>Port: maps a set of port types to a transport binding (a protocol, frequently SOAP, COM, CORBA, …) </li></ul><ul><li>Port Type: abstract grouping of operations, i.e. a class </li></ul><ul><li>Operation: the type of operation – request/response, one-way </li></ul><ul><ul><li>Input message and output message; maybe also fault message </li></ul></ul><ul><li>Types: the XML Schema type definitions </li></ul>
  21. 21. Example WSDL <ul><li><service name=“POService”> </li></ul><ul><ul><li><port binding=“my:POBinding”> </li></ul></ul><ul><ul><ul><li><soap:address location=“http://yyy:9000/POSvc”/> </li></ul></ul></ul><ul><ul><li></port> </li></ul></ul><ul><li></service> </li></ul><ul><li><binding xmlns:my=“…” name=“POBinding”> </li></ul><ul><ul><li><soap:binding style=“rpc” transport=“ http://www.w3.org/2001/...” /> </li></ul></ul><ul><ul><li><operation name=“POrder”> </li></ul></ul><ul><ul><ul><li><soap:operation soapAction=“POService/POBinding” style=“rpc” /> </li></ul></ul></ul><ul><ul><ul><li><input name=“POrder”> </li></ul></ul></ul><ul><ul><ul><ul><li><soap:body use=“literal” … namespace=“POService” …/> </li></ul></ul></ul></ul><ul><ul><ul><li></input> </li></ul></ul></ul><ul><ul><ul><li><output name=“POrderResult”> </li></ul></ul></ul><ul><ul><ul><ul><li><soap:body use=“literal” … namespace=“POService” …/> </li></ul></ul></ul></ul><ul><ul><ul><li></output> </li></ul></ul></ul><ul><ul><li></operation> </li></ul></ul><ul><li></binding> </li></ul>
  22. 22. JAX-RPC: Java and Web Services <ul><li>To write JAX-RPC web service “endpoint”, you need two parts: </li></ul><ul><ul><li>An endpoint interface – this is basically like the IDL statement </li></ul></ul><ul><ul><li>An implementation class – your actual code </li></ul></ul><ul><li>public interface BookQuote extends java.rmi.Remote { </li></ul><ul><li>public float getBookPrice(String isbn) throws java.rmi.RemoteException; </li></ul><ul><li>} </li></ul><ul><li>public class BookQuote_Impl_1 implements BookQuote { </li></ul><ul><li>public float getBookPrice(String isbn) { return 3.22; } </li></ul><ul><li>} </li></ul>
  23. 23. Different Options for Calling <ul><li>The conventional approach is to generate a stub, as in the RPC model described earlier </li></ul><ul><li>You can also dynamically generate the call to the remote interface, e.g., by looking up an interesting function to call </li></ul><ul><li>Finally, the “DII” (Dynamic Instance Invocation) method allows you to assemble the SOAP call on your own </li></ul>
  24. 24. Creating a Java Web Service <ul><li>A compiler called wscompile is used to generate your WSDL file and stubs </li></ul><ul><ul><li>You need to start with a configuration file that says something about the service you’re building and the interfaces that you’re converting into Web Services </li></ul></ul>
  25. 25. Example Configuration File <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><configuration xmlns=&quot;http://java.sun.com/xml/ns/jax- rpc/ri/config&quot;> </li></ul><ul><ul><li><service name=&quot;StockQuote&quot; targetNamespace=&quot;http://example.com/stockquote.wsdl&quot; typeNamespace=&quot;http://example.com/stockquote/types&quot; packageName=&quot;stockqt&quot;> </li></ul></ul><ul><ul><ul><li><interface name=&quot;stockqt.StockQuoteProvider&quot; servantName=&quot;stockqt.StockQuoteServiceImpl&quot;/> </li></ul></ul></ul><ul><ul><li></service> </li></ul></ul><ul><li></configuration> </li></ul>
  26. 26. Starting a WAR <ul><li>The Web Service version of a Java JAR file is a Web Archive, WAR </li></ul><ul><li>There’s a tool called wsdeploy that generates WAR files </li></ul><ul><li>Generally this will automatically be called from a build tool such as Ant </li></ul><ul><li>Finally, you may need to add the WAR file to the appropriate location in Apache Tomcat (or WebSphere, etc.) and enable it </li></ul><ul><li>See http://java.sun.com/developer/technicalArticles/WebServices/WSPack2/jaxrpc.html for a detailed example </li></ul>
  27. 27. Finding a Web Service <ul><li>UDDI: Universal Description, Discovery, and Integration registry </li></ul><ul><li>Think of it as DNS for web services </li></ul><ul><ul><li>It’s a replicated database, hosted by IBM, HP, SAP, MS </li></ul></ul><ul><li>UDDI takes SOAP requests to add and query web service interface data </li></ul>
  28. 28. What’s in UDDI <ul><li>White pages: </li></ul><ul><ul><li>Information about business names, contact info, Web site name, etc. </li></ul></ul><ul><li>Yellow pages: </li></ul><ul><ul><li>Types of businesses, locations, products </li></ul></ul><ul><ul><li>Includes predefined taxonomies for location, industry, etc. </li></ul></ul><ul><li>Green pages – what we probably care the most about: </li></ul><ul><ul><li>How to interact with business services; business process definitions; etc </li></ul></ul><ul><ul><li>Pointer to WSDL file(s) </li></ul></ul><ul><ul><li>Unique ID for each service </li></ul></ul>
  29. 29. Data Types in UDDI <ul><li>businessEntity : top-level structure describing info about the business </li></ul><ul><li>businessService : name and description of a service </li></ul><ul><li>bindingTemplate : how to access the service </li></ul><ul><li>tModel (t = type/technical) : unique identifier for each service-template specification </li></ul><ul><li>publisherAssertion : describes relationship between businessEntities (e.g., department, division) </li></ul>
  30. 30. Relationships between UDDI Structures publisherAssertion businessEntity businessService bindingTemplate tModel n 2 1 n 1 n m n
  31. 31. Example UDDI businessEntity <ul><li><businessEntity businessKey=“0123…” xmlns=“urn:uddi-org:api_v2”> </li></ul><ul><ul><li><discoveryURLs> </li></ul></ul><ul><ul><ul><li><discoveryURL useType=“businessEntity”> </li></ul></ul></ul><ul><ul><ul><ul><li>http://uddi.ibm.com/registery/uddiget?businessKey=0123 ... </li></ul></ul></ul></ul><ul><ul><ul><li></discoveryURL> </li></ul></ul></ul><ul><ul><li><name>My Books</name> </li></ul></ul><ul><ul><li><description>Technical Book Wholesaler</description> </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li><businessServices> </li></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><li></businessServices> </li></ul></ul><ul><ul><li><identifierBag> </li></ul></ul><ul><ul><ul><li><!– keyedReferences to tModels  </li></ul></ul></ul><ul><ul><li></identifierBag> </li></ul></ul><ul><ul><li><categoryBag> … </categoryBag> </li></ul></ul><ul><li></businessEntity> </li></ul>
  32. 32. UDDI in Perspective <ul><li>Original idea was that it would just organize itself in a way that people could find anything they wanted </li></ul><ul><li>Today UDDI is basically a very simple catalog of services, which can be queried with standard APIs </li></ul><ul><ul><li>It’s not clear that it really does what people really want: they want to find services “like Y” or “that do Z” </li></ul></ul>
  33. 33. The Problem: With UDDI and Plenty of Other Situations <ul><li>There’s no universal, unambiguous way of describing “what I mean” </li></ul><ul><ul><li>Relational database idea of “normalization” doesn’t convert concepts into some normal form – it just helps us cluster our concepts in meaningful ways </li></ul></ul><ul><ul><li>“ Knowledge representation” tries to encode definitions clearly – but even then, much is up to interpretation </li></ul></ul><ul><li>The best we can do: describe how things relate </li></ul><ul><ul><li>pollo = chicken = poulet = 雞 = 鸡 = jī = मुर्गी = murg </li></ul></ul><ul><ul><li>Note that this mapping may be imprecise or situation-specific! </li></ul></ul><ul><ul><ul><li>Calling someone a chicken, vs. a chicken that’s a bird </li></ul></ul></ul>
  34. 34. This Brings Us Back to XQuery, Whose Main Role Is to Relate XML <ul><li>Suppose we define an XML schema for our target data and our source data </li></ul><ul><li>A view is a stored query </li></ul><ul><ul><li>Function from a set of (XML) sources to an XML output </li></ul></ul><ul><ul><li>In fact, in XQuery, a view is actually called a function </li></ul></ul><ul><li>Can directly translate between XML schemas or structures </li></ul><ul><ul><li>Describes a relationship between two items </li></ul></ul><ul><ul><ul><li>Transform 2 into 6 by “add 4” operation </li></ul></ul></ul><ul><ul><ul><li>Convert from S1 to S2 by applying the query described by view V </li></ul></ul></ul><ul><li>Often, we don’t need to transfer all data – instead, we want to use the data at one source to help answer a query over another source… </li></ul>
  35. 35. Lazy Evaluation: A Virtual View Source2.xml Source1.xml Virtual XML doc. XQuery Query Form Browser/App Server(s) Query Results XQuery Source2.xml Source1.xml Composed XQuery HTML XSLT
  36. 36. Let’s Look at Some Simple Mappings <ul><li>Beginning with examples of using XQuery to convert from one schema to another, e.g., to import data </li></ul><ul><li>First: let’s review what our XQuery mappings need to accomplish… </li></ul>
  37. 37. Challenges of Mapping Schemas <ul><li>In a perfect world, it would be easy to match up items from one schema with another </li></ul><ul><ul><li>Each element would have a simple correspondence to an element in the other schema </li></ul></ul><ul><ul><li>Every value would clearly map to a value in the other schema </li></ul></ul><ul><li>Real world: as with human languages, things don’t map clearly! </li></ul><ul><ul><li>Different decompositions into elements </li></ul></ul><ul><ul><li>Different structures </li></ul></ul><ul><ul><li>Tag name vs. value </li></ul></ul><ul><ul><li>Values may not exactly correspond </li></ul></ul><ul><ul><li>It may be unclear whether a value is the same </li></ul></ul><ul><li>It’s a tough job, but often things can be mapped </li></ul>
  38. 38. Example Schemas <ul><li>Bob’s Movie Database </li></ul><ul><li><movie> <title>…</title> <year>…</year> <director>…</director> <editor>…</editor> <star>…</star>* </movie>* </li></ul><ul><li>Mary’s Art List </li></ul><ul><li><workOfArt> <id>…</id> <type>…</type> <artist>…</artist> <subject>…</subject> <title>…</title> </workOfArt>* </li></ul>Want to map data from one schema to the other
  39. 39. Mapping Bob’s Movies  Mary’s Art <ul><li>Start with the schema of the output as a template: </li></ul><ul><li><workOfArt> <id>$i</id> <type>$y</type> <artist>$a</artist> <subject>$s</subject> <title>$t</title> </workOfArt> </li></ul><ul><li>Then figure out where to find the values in the source, and create XPaths </li></ul>
  40. 40. The Final Schema Mapping <ul><li>Mary’s Art  Bob’s Movies </li></ul><ul><ul><li>for $m in doc(“movie.xml”)//movie, $a in $m/director/text(), $i in $m/title/text(), $t in $m/title/text() return <workOfArt> <id>$i</id> <type>movie</type> <artist>$a</artist> <title>$t</title> </workOfArt> </li></ul></ul>Note the absence of subject … We had no reasonable source, so we are leaving it out.