SOA using Java Web Services and Ajax Mark D. Hansen Author of “SOA Using Java Web Services” http://soabook.com
Mark Hansen’s Background Consultant, Entrepreneur, MIT PhD Book and code examples at http://soabook.com SOA-J open source project at http://soa-j.org
Outline What is SOA? What are Web Services? What are Java Web Services (JWS)? What is AJAX? How can you implement SOA with JWS and AJAX? Demo (eBay, Amazon, Yahoo!)
What is SOA? Loose coupling Service contract Autonomy Abstraction Reusability Composability Statelessness Discoverability “Service-Oriented Architecture”, Thomas Erl, Chapter 3 pg 34 - 37
What is a Web Service? A provider that enables access to the service via either: SOAP XML / HTTP A description or contract that specifies the request/response message structure. WSDL XML Schema (insufficient) WADL (new)
What are Java Web Services (JWS)? The provider is written in Java and deployed using a Java container. The consumer is written in Java. Java Web Service APIs are used: JAX-WS JAXB WS-Metadata (JSR-181) WSEE (JSR-109) JAX-RPC is deprecated starting in Java EE 5
Client Side JWS
Server Side JWS
JAX-WS Maps WSDL to a Java API package samples; import javax.jws.WebService; @WebService public class Hello { public String sayHello(String s) { return "Hello: " + s; } <definitions ... targetNamespace="http://samples/" name="HelloService"> <types> ... </types> <message name="sayHello"> ... </message> <portType name="Hello"> <operation name="sayHello"> <input message="tns:sayHello"/> <output message="tns:sayHelloResponse"/> </operation> </portType> <binding name="HelloPortBinding" type="tns:Hello"> <soap:binding ... /> <operation name="sayHello"> ... </operation> </binding> <service name="HelloService"> <port name="HelloPort“ ... </port> </service> </definitions>
JAXB Maps XML Schema to Java Classes
What is AJAX? Asynchronous JavaScript and XML An Interaction Model A Set of Technologies for Rich Client Development ... A Composite Application Framework for Flexible Business Process Management ??? “Ajax In Action”, Dave Crane et al., Chapter 2 pg 33
SOA Using JWS and Ajax
SOAShopper Architecture
SOAShopper – Integrating Yahoo!, Amazon, and eBay Demo SOAShopper – Integrating Yahoo!, Amazon, and eBay
eBay WSDL
Ant Task to Compile eBay WSDL
Using the Generated eBay API
A Client Binding Example public List<Offer> offerSearch( String keywords, Category category, Price lowprice, Price highprice) { ShopperImp binding = BindingService.getBinding( ShopperImp.class, EBayAPIInterface.class); return binding.offerSearch(keywords, category, lowprice, highprice); }
Implementing a Binding Service public abstract class BindingService { public static <C> C getBinding( Class<C> client, Class<?> service) { ... }
Flash Demo
Using the Dojo Table Widget <table dojoType="filteringTable" id="fromSOAShopperData" multiple="true" alternateRows="true" cellpadding="0" cellspacing="0" border="0" style="margin-bottom:24px;"> <thead> <tr> <th field="source" dataType="String">Source</th> <th field="thumbnail" dataType="html" align="center">Image</th> <th field="price" dataType="String">Price</th> <th field="summary" dataType="String">Summary</th> <th field="url" dataType="html">Link</th> </tr> </thead> </table>
Invoking the REST Endpoint function retrieveURL(url) { restURL = url; showSearchingMsg(restURL); if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = processStateChange; try { req.open("GET", url, true); req.setRequestHeader('Content-type','text/xml'); } catch (e) { alert(e); } req.send(null); } else if (window.ActiveXObject) { // IE req = new ActiveXObject("Microsoft.XMLHTTP"); ...
Loading the Dojo Table function populateTableFromLiveSOAShopperData() { try { var w = dojo.widget.byId("fromSOAShopperData"); w.store.setData(theSOAShopperLiveData); } catch(e) { alert(e); }
Resources SOA Using Java Web Services http://soabook.com Thomas Erl’s SOA Book http://www.thomaserl.com/books.asp Ajax In Action http://www.manning.com/crane/ SOA-J open source project http://soa-j.org