Download JAX-RPC Service

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
3. Implementing Web Services
General Approach
3.Stores service descriptions
as binding templates&URL
Web Services Broker
UDDI based
Registry Services
4.Locates services
and its binding info
Service delivery
-SOAP Clients
Web Services requester
2.Register/publish
services
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.
Service container
-SOAP Interfaces
-WSDL Descriptions
Web Services Provider
1.Create SOAP proxy interfaces
and WSDL based service
descriptions
1
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.
XML schema
compiling
Java class
Java class
Conforms to
Instance Of
<xml>
<>
</>
</xml>
marshalling
Java Object
unmarshalling
Java Object
XML
document
2
 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
3. Soap/http request
1. Lookup
service
Client
2. Return
information
4. Soap/http response
Application server
-JAX-RPC runtime
-JAX-RPC API
-Service endpoint
UDDI
 Java
provides the Java API for XML Messaging (JAXM)
-Lightweight XML messaging API for application-to-application (A2A) integration and business-tobusiness (B2B) communication.
-Support asynchronous communications
Sender Application
JAXM API
Receiver Application
JAXM API
JAXM Messaging
Provider
HTTP
SOAP
message
JAXM Messaging
Provider
HTTP
3
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.
JWSDP Environment
J2SE Environment
WSDL
JAX-RPC Client
Invocation
JAX-RPC-based Service
Stubs/Dynamic Proxy/DII
TIES
JAX-RPC Runtime
JAX-RPC Runtime
Request/Response
Soap over HTTP
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:
1.
2.
3.
4.
5.
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:
1.
2.
3.
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>>
<<request>>
client: StockTrader
StockClient
<<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:
· 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
10
Usage for Dynamic Invocation:
1.
2.
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
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.
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