Web-Service Example Service Oriented Architecture 1
Roles Service provider Service Consumer Registry Operations Publish (by provider) Find (by requester) Bind (by requester or invoker) Fundamentals Web Service = Description + Service Service Description Details of interfaces (data types, operations, bindings, location, ) Details of implementation Published to registry Service Software module deployed for invocation UDDI Search for services Retrieve service description WSDL Service Description Client Invoke service Service SOAP Messages (Standard encoding using XML Schema data types) 2
Build Life Cycle of Web Services creation & publication of a service interface Deploy proceed to the creation and deployment of service deployment of the executables for the Web service into an execution environment Run invocation i of the Web service by the service requestor Manage administration of the Web service application Security, availability, performance, OoS Step 1: Provide Interface of Service /** * Exchange rate service from country1 to country2 */ public interface IExchange { float getrate( String country1, String country2 ); void setvalue( String country, double value ); double getvalue( String country ); 3
Step 2: Provide Implementation of Service public class Exchange implements IExchange { float getrate( String country1, String country2){ return getvalue(country1)/ getvalue(country2); void setvalue( String country, double value ){.. double getvalue( String country ){. Step 3: Publish the Service Step 3.1 : generate standard web service description using standard tool (java2wsdl) Step 3.2 : start web server on specified url Step 3.3 : publish service for SOAP client 4
Step 3.1: Create WSDL from interface file java2wsdl examples.publish.exchange -e http://localhost:8004/glue/urn:exchange Result : - create and write to file Exchange.wsdl value (blue = mandatory) meaning classname name of java class to process -d directory directory to write files, "." by default -e endpoint endpoint of service -g include GET/POST binding -m map-file read mapping instructions -n namespace override default namespace of service -r description description of web service -s include SOAP binding -x command-file command file to execute WSDL Template Service Port (e.g. http://host/svc) Binding (e.g. SOAP) Port Binding porttype operation(s) inmesage outmessage Abstract interface 5
- <definitions name="exchange" targetnamespace="http://www.themindelectric.com/wsdl/exchange/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" - xmlns=http://schemas.xmlsoap.org/wsdl/ - ------ > - <message name="getvalue0soapin"> - <part name="country" type="xsd:string"> </part> </message> - <message name="getvalue0soapout"> - <part name="result" type="xsd:double"> </part> </message> - <message name="setvalue1soapin"> - <part name="country" type="xsd:string"></part> - <part name="value" type="xsd:double"> </part> </message> <message name="setvalue1soapout" /> - <message name="getrate2soapin"> - <part name="country1" type="xsd:string"> </part> - <part name="country2" type="xsd:string"></part> </message> - <message name="getrate2soapout"> - <part name="result" type="xsd:double"> </part> </message> - <porttype name="exchangesoap"> - <operation name="getvalue" " parameterorder="country"> " <input name="getvalue0soapin" message="tns:getvalue0soapin" /> <output name="getvalue0soapout" message="tns:getvalue0soapout" /> </operation> - <operation name="setvalue" parameterorder="country value"> <input name="setvalue1soapin" message="tns:setvalue1soapin" /> <output name="setvalue1soapout" message="tns:setvalue1soapout" /> </operation> - <operation name="getrate" parameterorder="country1 country2"> <input name="getrate2soapin" message="tns:getrate2soapin" /> <output name="getrate2soapout" message="tns:getrate2soapout" /> </operation> </porttype> 6
<binding name="exchangesoap" type="tns:exchangesoap"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> - <operation name="getvalue"> <soap:operation soapaction="getvalue" style="rpc" /> - <input name="getvalue0soapin"> <soap:body use="encoded" namespace="http://tempuri.org/book.soap.exchange" encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> - <output name="getvalue0soapout"> <soap:body use="encoded" namespace="http://tempuri.org/book.soap.exchange" encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> </binding> <service name="exchange"> " <documentation>an interface for getting exchange rates.</documentation> <port name="exchangesoap" binding="tns:exchangesoap"> <soap:address location="http://199.174.21.187:8004/soap/exchange" /> </port> </service> </definitions> public class ExchangeServer { public static void main( String[] args ) throws Exception { // step 3.2 start server HTTP.startup(http://localhost:8004/soap); // initialize instance of Exchange Exchange exchange = new Exchange(); exchange.setvalue( usa, 1); exchange.setvalue( japan, 0.4); // step 3.3 publish Registry.publish( urn:exchange, new exchange()); 7
Step 4: Client Invocation of Service Step 4.1 : proxy client stub is generated dynamically using standard tool (wsdl2java) Step 4.2 : client implementation uses the service by binding and utilizing web service on specified url Step 4: Client Interface of Service package book.soap;. import packages public class ExchangeClient { public static void main( String[] args ) throws Exception { String url = "http://localhost:8004/glue/urn:exchange.wsdl"; // Step 4.1 and 4.2 IExchange exchange = (IExchange)Registry.bind( url, IExchange.class ); // invoke the web service as if it was a local java object exchange.getrate( "usa", "japan" ); System.out.println( "usa/japan exchange rate = " + rate ); 8
SOAP based messaging 9
SOAP Service oriented architecture SOAP Request SOAP Request Message <?xml version='1.0' encoding='utf-8'?> <soap:envelope p xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' p p p p xmlns:xsi='http://www.w3.org/2001/xmlschema-instance' xmlns:xsd='http://www.w3.org/2001/xmlschema' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' soap:encodingstyle='http://schemas.xmlsoap.org/soap/encoding/'> <soap:body> <n:getrate xmlns:n='http://tempuri.org/examples.service.exchange'> <country1 xsi:type='xsd:string'>usa</country1> 2 i ' d i ' j / 2 <country2 xsi:type='xsd:string'>japan</country2> </n:getrate> </soap:body> </soap:envelope> 10
SOAP Response Message SOAP Response <?xml version='1.0' encoding='utf-8'?> 8? <soap:envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/xmlschema-instance' xmlns:xsd='http://www.w3.org/2001/xmlschema' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' soap:encodingstyle='http://schemas.xmlsoap.org/soap/encoding/'> <soap:body> <n:getrateresponse xmlns:n='http://tempuri.org/examples.service.exchange'> <Result xsi:type='xsd:double'>2.5</result> </n:getrateresponse> </soap:body> </soap:envelope> Demo - // running server > java book.soap.exchangeserver // running client > java book.soap.exchangeclient 11
Complete Web Service Description Stack Service Provider Service Consumer 12
WSDL to UDDI Mapping Reference Web Service, building blocks for distributed systems, by Graham Glass IBM documentation on www.ibm.com 13