15
1 3. Implementing Web Services 1.Create SOAP proxy interfaces and WSDL based service descriptions 2.Register/publish services 3.Stores service descriptions as binding templates&URL 4.Locates services and its binding info 5.Invoke & obtain WSDL 6.Exchange data using SOAP RPC/Messaging Web Services requester Service delivery -SOAP Clients Web Services Broker UDDI based Registry Services Web Services Provider Service container -SOAP Interfaces -WSDL Descriptions General Approach service can be created as a pplication or from using an ing application by rposing them as services.

1 3. Implementing Web Services 1.Create SOAP proxy interfaces and WSDL based service descriptions 2.Register/publish services 3.Stores service descriptions

Embed Size (px)

Citation preview

1

3. Implementing Web Services

1.Create SOAP proxy interfacesand WSDL based service descriptions

2.Register/publishservices

3.Stores service descriptionsas binding templates&URL

4.Locates services and its binding info

5.Invoke & obtain WSDL

6.Exchange data usingSOAP RPC/Messaging

Web Services requester

Service delivery-SOAP Clients

Web Services Broker

UDDI based Registry Services

Web Services Provider

Service container-SOAP Interfaces

-WSDL Descriptions

General Approach

A Web service can be created as a new application or from using an existing application by re-purposing them as services.

2

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.

marshalling

unmarshalling

<xml><></>

</xml>

XML schema

Instance Of

XML document

Java Object

Java Object

compiling

Conforms to

Java classJava class

3

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

UDDI

Application server

-JAX-RPC runtime-JAX-RPC API-Service endpoint

3. Soap/http request

4. Soap/http response1. Lookup service

2. Returninformation

Client

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

JAXM API JAXM API

JAXM MessagingProvider

JAXM MessagingProviderSOAP

message HTTPHTTP

Receiver ApplicationSender Application

4

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 mappingsJAX-RPC Runtime Environment: runtime mechanisms and execution environment for the Web services and service clients.

J2SE Environment

JAX-RPC ClientInvocation

Stubs/Dynamic Proxy/DII

JAX-RPC Runtime

JWSDP Environment

JAX-RPC-based Service

TIES

JAX-RPC Runtime

WSDL

Request/Response

Soap over HTTP

5

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:1. Define the Remote interface (Service Definition).2. Implement the remote interface (Service Implementation).3. Configure the Service.4. Generate the Stubs and ties.5. 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.

6

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.

7

4. Sample Implementation

SOA for Sample Implementation

<<participant>>

StockBroker

<<participant>>

StockClient

<<interface>>

StockTrader

getStockQuote(index:int): double

<<service>> broker: StockTrader

<<request>> client: StockTrader

8

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()); }}

Web Service Implementation

9

Configuring the Service

-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>

-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.

10

· Service interface: A factory for stubs or dynamic invocation/proxy objects that are used to actually invoke methods

· ServiceFactory class: A factory for Services

· Call interface: Used for dynamic invocation

· Stub interface: Base interface for stubs

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:

11

Usage for Dynamic Invocation:

1. The client creates a ServiceFactory 2. 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”));

12

1. 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.

Creating and running an RPC-style Web service client

13

//Execute the remote method System.out.println(stub.getStockQuote(1233465)); } catch (Exception ex) { ex.printStackTrace(); } }}

Stub-based Client -Uses the local stub classes generated by the WSDL compiler.

//import the Stub interfaceimport 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);

14

4. Set the URL location of the Service:String endpoint ="http://localhost:6080/SimpleRPC/services/StockTrader"; call.setTargetEndpointAddress(endpoint);

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();

5. Invoke the call object:Object [] arguments = new Object[0];String stockId = (String) call.invoke(arguments);

15

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); } }