JAX-RPC stands for java API for “XML based RPC”

XML based RPC

  1. XML-RPC is a sample,portable way to make procedure calls over HTTP.It can be used with perl,java,python,c,c++,PHP and many other progrmming languages.
  2. An XML-RPC message is always HTTP-POST request.The body(payload) of the Http Request Message is XML message.
  3. A procedure executes on the server and value it returns is also formatted in XML.
  4. Procedure parameters can be scalars,numbers,strings,dates,etc.,and can also be complex record and list structures.

Request example

Here's an example of an XML based RPC request
POST/RPC2 HTTP/1.1
User_Agent:Frontier/5.12
Host:betty.userland.com
Content-Type:text/xml
Content-length:181
<?xml version="1.0"?>
<methodCall>
   <methodName>getStateName</methodName>
      <params>
           <param>
             <value><integer>41</integer></value>
           </param>
     </params>
</methodCall>

The payload in XML,with a single<methodCall> structure

SOAP based RPC

  1. The RPC-style messaging is common in old distributed technologies including EJB,CORBA,RMI,DCOM,etc.Hence,SOAP defines standard XML format for RPC-style messaging.
  2. The SOAP RPC specifies how methods and their parameters and return values are represented within the <soap:Body> element of a soap message.
  3. The method name becomes direct sub element of <soap:Body> and every parameter name becomes child element of the method name i.e.,rpc-style messaging uses method names and their parameter names

To invoke RPC,the following information is needed:

  • Method(or procedure) name (must be namespace qualified)
  • Values to method parameters
  • Only one method name in soap body(payload)

Example:SOAP-RPC Request message

<soap:Body>
	<tns:contact xmlns:tns="http://www.java2learn.com/ss">
	   <s1>Learning</s1>
	   <s2>Java</s2>
	</tns:concat>
</soap:Body>

Example: SOAP-RPC Response message

<soap:Body>
   <ans1:concatResponse xmlns:ans1="http://www.java2learn.com/ss">
	 <result>Learning Java</result>
   </ans1:concatResponse>
</soap:Body>
  1. Both RPC Request and RPC Response are carried in the <soap:Body> element.
  2. The main purpose of using JAX-RPC is to make communication between Java and non-Java platforms easir i.e., a JAX-RPC client can access a Web Service that is not running on the java platform and vice versa.
  3. This flexibility is possible because JAX-RPC uses Web Services technologies defined by the world wide web consortium(W3C) such as HTTP,SOAP,XML,WSDL.
  4. We can write clients using JAX-RPC to access web services that run in non-Java environments.
  5. We can write services using JAX-RPC and host them on a J2EE application server,so that non-Java clients can access our service.

Default Type Mapping

JAX-RPC defined its own type mapping between Java types and Xml types i.e .,it is the responsibility of JAX-RPC to provide its own Serialization and DeSerialization mechanisms.

Simple Types

The following table provides mapping between simple XML types and corresponding Java types:

XML Types Java Types
xsd:byte Byte
xsd:short Short
xsd:int Int
xsd:long Long
xsd:integer java.math.BigInteger
xsd:float Float
xsd:double Double
xsd:decimal java.math.BigDecimal
xsd:Boolean Boolean
xsd:string java.lang.String
xsd:date java.util.Calender
ComplexType

The following example shows a struct of the type Book and its schema fragment and instance:

<!--XML Schema fragment-->
<xsd:complexType name="book">
   <xsd:sequence>
      <xsd:element name="authors" type="xsd:string" maxOccurs="10"/>
	  <xsd:element name="preface" type="xsd:string"/>
	  <xsd:element name="price" type="xsd:float"/>
   </xsd:sequence>
</xsd:complexType>

//JavaBean class

	
public class Book {
	private String[] authors;
	private String perface;
	private float price;
	//setter and getter methods
}

Service Description

The JAX-RPC uses WSDL specification to describe services in neutral format(interoperability)i.e.,JAX-RPC defines its own mapping between WSDL definitions and Java definitions.
The following table describes mapping between wsdl definitions and Java definitions:

Wsdl definition Java definition
<wsdl:portType> Endpoint Interface
<wsdl:operation> Method in Endpoint Interface
<wsdl:part>element in<wsdl:input> Method IN parameters
<wsdl:part>element in<wsdl:output> Return value or OUT parameter
<wsdl:fault> Exception Type
<wsdl:types> contains xml types or elements Java Classes
<wsdl:binding> Generated Stub class
<wsdl:service> Service Interface

Example:

<!--Input message-->
<wsdl:meesage name="stockQuoteInput">
    <wsdl:part name="ticketSymbol" type="xsd:string"/>
</wsdl:message>

<!--Output message-->
<wsdl:meesage name="stockQuoteOutput">
    <wsdl:part name="lastTradePrice" type="xsd:string"/>
</wsdl:message>

<!--Service specific exception i.e., fault message-->
<wsdl:message name="InvalidTickerException">
    <wsdl:part name="tikerSymbol" type="xsd:string"/>
</wsdl:message>

<wsdl:portType name="StockQuoteProvider">
   <wsdl:operation name="GetStockQuote">
        <wsdl:input message="tns:StockQuoteInput"/>
        <wsdl:output message="tns:StockQuoteOutput"/>
        <wsdl:fault message="tns="tns:InvalidTickerException" name="InvalidTickerException"/>
    </wsdl:operation>
</wsdl:portType>

The above <wsdl:portType> definition maps to the following Service Endpoint Interface(SEI):
//EndPointInterface

public interface StockQuoteProvider extends java.rmi.Remote{
  public float getStockQuote(String tickerSymbol)
     throws java.rmi.RemoteException,InvalidTickerException;
}

//project specific exception

public class InvalidTickerException extends Exception {
 public InvalidTickerException(String tickerSymbool){
     super(tickerSymbol);
 }
}

JAX-RPC Client and Server side programming models

The JAX-RPC defines standards to write both client-side and server-side programming models
JAX-RPC defines two server-side programming models for creating J2EE Web service endpoint as:

  1. JAX-RPC Service endpoints
    This is the simplest type of endpoint to develop .Which is a full-fledged J2EE component,Which runs inside j2EE servlet container
  2. EJB endpoints
    Allows stateless EJBs to act as endpoints.EJB endpoints are valuable if we want to expose existing enterprise bean as Web Service to non java clients.Which runs inside EJB container

JAX-RPC defines three client-side programming models that java applications can use to access Web Services:

  1. Generate Stub
  2. Dynamic proxies
  3. DLL(Dynamic Invocation Interface)
Generated Stub

The JAX-RPC compiler(wscompile) read WSDL document and generate both Endpoint interface and Stub class.

  1. The Endpoint interface extends java.rmi.Remote interface.
  2. The Generated Stub class implements both:
    • EndPoint interface
      Contains business logic method details.
    • javax.xml.rpc.Stub interface.
      The methods in this interface used for setting properties related to network communication and authentication.Using these properties we can set the end point address,password,etc at runtime.Since ,the client side stub implements Endpoint Interface,hence stub is proxy of actual object i.e.,the generated stubs make web service endpoints looks like local java objects.

GeneratedStub

What stub class does at runtime?

  1. When any of the stub’s methods is invoked it will convert the method call into a soap message and sends that message to the web service endpoint(called as serialization)i.e., the client side object is serialized as soap message.
  2. Convert the received SOAP response message into method return value(called deserialization)i.e.,the soap message is deserialized as return value.

Stub
The stub class perform this translation according to the <wsdl:binding>configuration specified in wsdl document such as messaging style,encoding style,etc.

Service Interface
  1. The Service interface mapswith <wsdl:service> element,Which contains address details
  2. The name of the service interface is same as ‘name’ attribute of the <wsdl:service> element
  3. The service interface defines an accessor(getter) methods for every <wsdl:port> element declared in wsdl document.
  4. The method names are same as ‘name’ attribute of the <wsdl:port> element.The return type is always Endpoint Interface.
  5. The Service interface is automatically generated by JAX-RPC compiler(wscompile),which extends javax.rml.rpc.Service interface ,which defines methods normally used with dynamic proxy and DII programming.

Example:Service Interface

public interface CatalogService extends javax.xml.rpc.Service{//Generated by JAX-PRC compiler
   public Catalog getCatalogPort() throws ServiceException;
}
The service interface is implemented by a class which is generated by the JAX-PRC compiler,which is used to instantiate stubs

Example:

public class CatalogService_Impl implements CatalogService {//Generated by JAX-RPC compiler
   @Override
    public Catalog getCatalogPort(){
     ....
     Catalog_Stub stub=new Catalog_Stub(...);
     ....
     return stub;
   }
}

Sample Clent Code

public class CatalogServiceStubClient{
   public static void main(String[] args)throws RemoteException {
        Catalog stub=new CatalogService_Impl().getCatalogPort();
        float price=stub.getBookPrice("ISBN1001");
        System.out.println("The price is:"+price);
   }
}

Dynamic Proxy:

  1. Invokes target service endpoint dynamically at runtime without using the local stub class.
  2. The getPort() method on the javax.xml.rpc.Service interface enables a dynamic proxy to be created.
  3. We are not required to generate client side stub class,service interface,and its implementation class using JAX-RPC compiler.