150 likes | 163 Views
Learn about creating, publishing, locating, and invoking web services, implementing JAX-RPC services, and configuring clients and endpoints in Java.
E N D
Web Services Broker UDDI based Registry Services Web Services requester Service delivery -SOAP Clients Web Services Provider Service container -SOAP Interfaces -WSDL Descriptions 3. Implementing Web Services General Approach 3.Stores service descriptions as binding templates&URL 2.Register/publish services 4.Locates services and its binding info 5.Invoke & obtain WSDL 6.Exchange data using SOAP RPC/Messaging A Web service can be created as a new application or from using an existing application by re-purposing them as services. 1.Create SOAP proxy interfaces and WSDL based service descriptions 1
XML schema compiling Java class Java class Conforms to Instance Of marshalling unmarshalling <xml> <> </> </xml> Java Object Java Object XML document Web Services Support in J2EE Java Architecture for XML Binding (JAXB) provides Application developers with a way to generate programming objects based on XML definitions and vice versa, more specifically: -Marshalling: converting a Java object tree into an XML document. -Unmarshalling: converting an XML document into a Java object tree. -Validation: checking an XML document for conformance with a DTD. 2
3. Soap/http request 4. Soap/http response Application server -JAX-RPC runtime -JAX-RPC API -Service endpoint Client 2. Return information 1. Lookup service UDDI Sender Application Receiver Application JAXM API JAXM API JAXM Messaging Provider JAXM Messaging Provider SOAP message HTTP HTTP Java provides the Java API for XML Remote Procedure Calls (JAX-RPC) -Set of high-level Java APIs enabling XML-based Java applications to interoperate using RPC. -Used for synchronous communications Java provides the Java API for XML Messaging (JAXM) -Lightweight XML messaging API for application-to-application (A2A) integration and business-to- business (B2B) communication. -Support asynchronous communications 3
JWSDP Environment J2SE Environment WSDL JAX-RPC Client Invocation Stubs/Dynamic Proxy/DII JAX-RPC Runtime JAX-RPC-based Service TIES JAX-RPC Runtime Request/Response Soap over HTTP JAX-RPC Application Architecture -A typical JAX-RPC application architecture model involves: • JAX-RPC Service: Java business component (e.g. servlet, bean). • JAX-RPC Service client: access exposed Web service. • Serialization/Deserialization:Java-to-XML and XML-to-Java mappings • JAX-RPC Runtime Environment: runtime mechanisms and execution environment for the Web services and service clients. 4
JAX-RPC-based Service Implementation -JAX-RPC services can be implemented using Java classes ( RMI) or by using a WSDL document. Developing a JAX-RPC Service from Java Classes -The key steps include the following: • Define the Remote interface (Service Definition). • Implement the remote interface (Service Implementation). • Configure the Service. • Generate the Stubs and ties. • Package and deploy the service. Developing a JAX-RPC-based Service from a WSDL Document -This consists of developing a JAX-RPC-based service using a WSDL document exposed by an existing Web services environment. -The key steps include the following: • Create a service configuration referring to the WSDL. • Generate the client-side stubs and server-side ties. • Package and deploy the service. 5
JAX-RPC Client Implementation • Can be implemented either as stub-based or dynamic proxy-based or dynamic invocation interface based. Stub-based Client -Uses the local stub classes generated (automatically) from the WSDL file. Dynamic Proxy-based Client -Enables the invocation of a target service endpoint dynamically at runtime, without requiring a local stub class. Dynamic Invocation Interface (DII) Client -Enables the client to discover and invoke target services dynamically at runtime. -The client uses a set of operations and parameters, and a search criterion to discover the target service. Hence a client can invoke a service and its methods without knowing in advance its data types. 6
4. Sample Implementation SOA for Sample Implementation <<participant>> StockClient <<request>> client: StockTrader <<participant>> StockBroker <<service>> broker: StockTrader <<interface>> StockTrader getStockQuote(index:int): double 7
Web Service Implementation Service Definition import java.rmi.*; public interface StockTrader extends java.rmi.Remote { public double getStockQuote(int id) throws java.rmi.RemoteException; } Service Implementation import java.rmi.*; public class StockTraderImpl implements StockTrader { public double getStockQuote(int id) { return ((new Quote(id)).getValue()); } } 8
Configuring the Service -Service configuration requires creating a configuration file in an XML format that provides the following information: The name of the service. The name of the package containing the stubs and ties. The target namespace for the generated WSDL and its XML schema and class names of the remote interface and its implementation. -Sample configuration file: serviceconfig.xml <?xml version=“1.0” encoding=“UTF-8”?> <configuration xmlns=http://java.sun.com/xml/ns/jax-rpc/ri/config> <service name=“StockTrader” targetNamespace=http://www.binkadi.com/jws/wsdl typeNamespace=http://www.binkadi.com/jws/types packageName=“com.binkadi.jws.stock.jaxrpc”> <interface name=“jws.binkadi.jaxrpc.StockTrader” servantName=“jws.binkadi.jaxrpc.StockTraderImpl”/> </service> </configuration> 9
JAX-RPC Client-Side APIs (javax.xml.rpc) -There are just a few key classes and interfaces that are needed to write Web service clients or service implementations: · Serviceinterface: A factory for stubs or dynamic invocation/proxy objects that are used to actually invoke methods · ServiceFactory class: A factory for Services · Callinterface: Used for dynamic invocation · Stub interface: Base interface for stubs 10
Usage for Dynamic Invocation: • The client creates a ServiceFactory • From the ServiceFactory, the client instantiates a Service ServiceFactory sFactory = ServiceFactory.newInstance(); Service service = serviceFactory.createService(new Qname(qService)); - The Service is a factory object that creates the port. The port is a remote interface (java.rmi.Remote) into the Web service. - In the case of dynamic RPC, the Service object is used to create Call objects, which specify which method to call on the Web services port. Call call = service.createCall(target_port); call.setTargetEndpointAddress(target_endpoint); call.setOperationName(newQname(BODY_NAMESPACE_VALUE,”getStockPrice”)); 11
Creating and running an RPC-style Web service client • Obtain the interface details of the Web service you want to invoke. · by getting a WSDL file that describes the Web service 2. Optionally generate Web service client invocation code (stubs). · This step is optional as there is usually a dynamic invocation API available where a stub is not required to invoke a Web service. 3. Write the client. · Static call: create or obtain a reference to a stub object and then call its Web service-exposed methods. · Dynamic call: create a call object and invoke a method on the web service (a stub is not required). 4. Run the client. · To run the client, your Web service runtime's client code will need to be in your classpath. 12
Stub-based Client -Uses the local stub classes generated by the WSDL compiler. //import the Stub interface import javax.xml.rpc.Stub; public class StockClient { public static void main(String[] args) { try { //obtain the instance of Stub interface StockTrader_Stub stub = (StockTrader_Stub) (new StockTrader_Impl().getStockTraderPort()); //Configure the stub setting required properties stub._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, http://www.binkadi.com/jws/jaxrpc/stocktrader); //Execute the remote method System.out.println(stub.getStockQuote(1233465)); } catch (Exception ex) { ex.printStackTrace(); } } } 13
Dynamic Web service client 1. Import the needed classes: import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.Service; 2. Create a Service object: Service service = (Service) Class.forName("org.apache.axis.client.Service").newInstance(); 3. Use the Service to create the call object: Call call = (Call) service.createCall(); 4. Set the URL location of the Service: String endpoint ="http://localhost:6080/SimpleRPC/services/StockTrader"; call.setTargetEndpointAddress(endpoint); 5. Invoke the call object: Object [] arguments = new Object[0]; String stockId = (String) call.invoke(arguments); 14
Example: import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.Service; public class StockClient { public static void main(String[] args) throws Exception{ String endpoint = "http://localhost:6080/SimpleRPC/services/StockTader"; String namespaceURI = "http://server.simplerpc.stock.lpc"; Service service = (Service) Class.forName("org.apache.axis.client.Service").newInstance(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName(new QName(namespaceURI,"getQuote")); Object [] arguments = new Object[0]; String quote = (String) call.invoke(arguments); System.out.println(“Stock quote: " + quote); } } 15