Web-Service Example Service Oriented Architecture 1
Fundamentals • Roles – Service provider – Service Consumer – Registry • Operations – Publish (by provider) – Find (by requester) – Bind (by requester or invoker) • 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 WSDL Sea c Search for o Service Service services Description Retrieve service description Invoke service Client Service SOAP Messages (Standard encoding using XML Schema data types) 2
Life Cycle of Web Services • Build – 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 of the Web service by the service requestor i i f h W b i b h i • 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 ); 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 • 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 Port (e.g. http:/ / host/ svc) Binding Binding (e.g. SOAP) p portType yp 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"> i " V l " O d " " <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> /i - <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"> i "E h " <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 • 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 y g g 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 • Service oriented architecture SOAP Request Message SOAP Request <?xml version='1.0' encoding='UTF-8'?> <soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' p p p p p g 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> <country2 xsi:type='xsd:string'>japan</country2> 2 i ' d i ' j / 2 </n:getRate> </soap:Body> </soap:Envelope> 10
SOAP Response Message SOAP Response <?xml version='1.0' encoding='UTF-8'?> ?xml version 1.0 encoding UTF 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 // i li t > 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 • Web Service , building blocks for distributed systems, by Graham Glass • IBM documentation on www.ibm.com 13
Recommend
More recommend