You are on page 1of 37

JAX-RPC

Topics
What is RPC?
JAX-RPC introduction
Creating JAX-RPC based web service
Creating JAX-RPC based web service client
Java-XML mapping for web service
Sample code basic web service and client based on
JAX-RPC
1 Copyright 2003 Ketan A. Pandya
Where are we?

Platform and
toolkit Toolkit and
independent (UDDI) Platform
Service dependent
registry
WSDL
WSDL
Find Publish
service service

Service SOAP Invoke Service


requestor service provider
1. Apache toolkit
1. Apache toolkit
2. JWSDP :JAX-
2. JWSDP: JAX-
2 Copyright 2003 Ketan A. Pandya RPC
What is RPC?

Remote Procedure Call


Term that refers to a program calling a function that resides on another
computer as if the function was local
RPC mechanism handles networking and packaging of data-parameter
and return value-

..some code..
Function() Function()
code..

RPC code Proprietary protocol RPC code

client server
3 Copyright 2003 Ketan A. Pandya
Shortcomings of (traditional) RPC

Proprietary protocol
Client and server needs to be using the same programming language
Not suitable for internet
No OO way of calling method

4 Copyright 2003 Ketan A. Pandya


RPC in Java Web Service

RPC in Java Web Service (jws) uses XML/SOAP-open standard-


Client and server both may be developed using different programming
language
RPC protocol is open standard

Service SOAP Invoke Service


requestor service provider

5 Copyright 2003 Ketan A. Pandya


Disadvantages of RPC in web service

Overhead of text formatted data exchange


Slower than traditional RPC
Currently web services are primarily uses HTTP to exchange data and
hence involves network connection overhead

6 Copyright 2003 Ketan A. Pandya


What is JAX-RPC?

JAX-RPC: Java APIs for XML based Remote Procedure Call


APIs for creating RPC based web services and a runtime environment for
web services applications

Web services Web services


client server
JAX-RPC JAX-RPC
APIs classes
JAX-RPC JAX-RPC
runtime SOAP runtime

7 Copyright 2003 Ketan A. Pandya


JAX-RPC introduction

JAX-RPC defines an APIs framework to create and deploy web service


client and server
Web service provider/server use JAX-RPC classes/interfaces to define
service end point (methods)
JAX-RPC supports exposing business methods as service
Web service requestor/client use JAX-RPC classes/interfaces to connect to
and make method/service call
JAX-RPC also comes with tool to create WSDL document from service
classes
JAX-RPC comes with tool to create classes to support client to use service
JAX-RPC defines mapping between Java and WSDL
8 Copyright 2003 Ketan A. Pandya
JAX-RPC architecture based on JWSDP

WSDL
document JWSDP
J2SE
Web services Web services
client server
Stubs/
Dyanmic proxy/
Tie classes
DII
JAX-RPC JAX-RPC
runtime SOAP
runtime

9 Copyright 2003 Ketan A. Pandya


JAX-RPC architecture based on JWSDP

JAX-RPC application architecture consist of following


JAX-RPC service
Business component implemented in Java
Can be accessed by non-Java client as well as Java client
Currently the service is deployed as Java servlet or EJB stateless
bean
public class DateServiceImpl implements DateServiceIF {

public String currentDate() {


.................
}
}
10 Copyright 2003 Ketan A. Pandya
JAX-RPC architecture based on JWSDP

JAX-RPC service
Web server

SOAP request JAXRPCEndpoint Web


servlet service
SOAP
response

11 Copyright 2003 Ketan A. Pandya


JAX-RPC architecture based on JWSDP

JAX-RPC service client


Client that can access service
Invocation model how service is accessed
WSDL
Stub based accesses local Java classes
Dynamic proxy
Dynamic Invocation Interface xrpcc
or
Client can use WSDL to generate stub classes wscompile

Java client Stub classes

12 Copyright 2003 Ketan A. Pandya


JAX-RPC architecture based on JWSDP

Serialization and deserialization


JAX-RPC serializes (converts) Java data type to XML (SOAP) and
deserializes from XML to Java
Custom data types (custom Java classes) can also be
serialized/deserialized

13 Copyright 2003 Ketan A. Pandya


JAX-RPC architecture based on JWSDP

xrpcc or wscompile tool


Used for
1. Creating WSDL from web service class
2. Creating stubs from WSDL for client
3. Creating classes for server side

14 Copyright 2003 Ketan A. Pandya


JAX-RPC Server Side

package helloservice;
The service
import java.rmi.Remote; endpoint
interface
Import java.rmi.RemoteException;
public interface HelloIF extends Remote {
public String sayHello(String s) throws
RemoteException;
}
15 Copyright 2003 Ketan A. Pandya
Implement the service

package helloservice;

public class HelloImpl implements HelloIF {


public String message ="Hello";
public String sayHello(String s) {
return message + s;
}
}

16 Copyright 2003 Ketan A. Pandya


Provide a config-interface.xml

<?xml version="1.0" encoding="UTF-8"?>


<configuration
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<service
name="MyHelloService"
targetNamespace="urn:Foo"
typeNamespace="urn:Foo"
packageName="helloservice">
<interface name="helloservice.HelloIF"/>
</service>
</configuration>
17 Copyright 2003 Ketan A. Pandya
Three steps to build

1. compile-service
2. generate-wsdl
3. generate-mapping from service classes
package names to namespace URIs in the
WSDL and create ties (skeletons)

J2EE1.4 provides an ant task to perform all


18 three steps Copyright 2003 Ketan A. Pandya
On the client side

1. Static stubs
compiled by wscompile before runtime
2. Dynamic stubs
has an interface but fetches the WSDL at runtime
3. Dynamic Invocation Interface
knows no interface - the method names
and signatures
4. A J2EE Application Client
Locate the local web service with JNDI

19 Copyright 2003 Ketan A. Pandya


J2EE Application Client

import javax.xml.rpc.Stub;
import javax.naming.*;

public class HelloClient {

private String endpointAddress; Ask JNDI for a


reference to a
public static void main(String[] args) { stub for the object

System.out.println("Endpoint address = " + args[0]);

try {
Context ic = new InitialContext();
MyHelloService myHelloService = (MyHelloService)
ic.lookup("java:comp/env/service/MyJAXRPCHello");

20 Copyright 2003 Ketan A. Pandya


HelloIF helloPort = myHelloService.getHelloIFPort();

((Stub)helloPort)._setProperty
(Stub.ENDPOINT_ADDRESS_PROPERTY,args[0]);

System.out.println(helloPort.sayHello("Jake!"));
System.exit(0);

} catch (Exception ex) {


ex.printStackTrace();
System.exit(1);
}
}
}

21 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based web service

Steps to create JAX-RPC based service


1. Define the remote interface with business method for a service
public interface DateServiceIF extends Remote {
public String currentDate() throws RemoteException;
}

Method of the interface must throw RemoteException

22 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based web service

2. Implement the remote interface


public class DateServiceImpl implements DateServiceIF {
public String currentDate() {
.business logic
}
}

The service may implement lifecycle interface ServiceLifeCycle


that has init() and destory() method

23 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based web service

3. Compile the interface and implementation


javac -d WEB-INF\classes *.java

24 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based web service

4. Create configuration file that describes web service


This file will be used to create server side support classes using
xrpcc or wscompile tool
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<service name="DateService"
targetNamespace="http://wsa.simple/wsdl"
typeNamespace="http://wsa.simple/types"
packageName="wsa.simple">
<interface name="wsa.simple.service.DateServiceIF"
servantName="wsa.simple.service.DateServiceImpl"/>
</service>
</configuration>
25 Copyright 2003 Ketan A. Pandya
Creating JAX-RPC based web service

5. Generate server side classes using xrpcc or wscompile tool


Supply config file created in step-3 as argument
%JWSDP_HOME%\jaxrpc-1.0.3\bin\xrpcc -classpath .\WEB-INF\classes -server -d
.\WEB-INF\classes DateServiceConfig.xml

Generates server side


Configuration file that has classes
service
interface/implementation
class details Destination directory
for generated classes

26 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based web service

6. Create war file and copy it to %JWSDP_HOME%/webapps directory


jar cvf dateservice.war WEB-INF

7. Start web server by executing %JWSDP_HOME%\bin\startup.bat


8. Check if web services is deployed
URL: http://localhost:8080/dateservice/jaxrpc

27 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based client

Steps to create JAX-RPC based client


1. Create client side code that uses web service
2. Create client side stubs from config file (created on server side).
1. This could be done from WSDL as well
%JWSDP_HOME%\jaxrpc-1.0.3\bin\xrpcc -client -keep -classpath ..\service\WEB-INF\classes -d
. ..\service\DateServiceConfig.xml3.

3. Compile client code


4. Run client code

28 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based client from wsdl

Steps to create JAX-RPC based client from wsdl


1. Create config.xml: specify wsdl file location and package name
<?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<wsdl location="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"
packageName="client">
</wsdl>
</configuration>

29 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based client from wsdl

2. Use wscompile or xrpcc to create stubs


%JWSDP_HOME%\jaxrpc-1.0.3\bin\wscompile -gen:client -keep -d . config.xml

Generate
Client code

Keeps Java files

Destination
directory

Input file

30 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based client from wsdl

Relevant files generated from wsdl


<portType name="CurrencyExchangePortType">
<operation name="getRate">
<input message="tns:getRateRequest" />
<output message="tns:getRateResponse" /></operation>
</portType>
<service name="CurrencyExchangeService">
<port name="CurrencyExchangePort" binding="tns:CurrencyExchangeBinding">
<soap:address location="http://services.xmethods.net:80/soap"/>
</port>
</service>

CurrentcyExchangePortType.class

CurrentcyExchangeService_Impl.class
31 Copyright 2003 Ketan A. Pandya
Creating JAX-RPC based client from wsdl

3. Create client code that invokes method



CurrencyExchangePortType teststub =
(CurrencyExchangePortType )
(new CurrencyExchangeService_Impl().getCurrencyExchangePort());

teststub.getRate("canada","usa");
.
Code snippet that invokes method on a web service using
stub classes

32 Copyright 2003 Ketan A. Pandya


Creating JAX-RPC based client from wsdl

3. Compile and run client code


javac -classpath .;%JWSDP_HOME%\jaxrpc-1.0.3\lib\jaxrpc-
api.jar;%JWSDP_HOME%\jaxrpc-1.0.3\lib\jaxrpc-ri.jar;.; -d . *.java

java TestClient

You will need to include various jar files from JWSDP into
classpath

33 Copyright 2003 Ketan A. Pandya


Dynamic invocation interface client

Another way for client to invoke web service method dynamically without
creating stubs
Flexible: web service parameters can be specified as command line
parameter or as config/property file
Somewhat similar to Apache client

34 Copyright 2003 Ketan A. Pandya


Creating Dynamic invocation interface client

Create client code


1. Specify service, port, location and operation
2. Specify service urn
call.setOperationName(new QName(BODY_NAMESPACE_VALUE,
"getRate"));

3. Specify parameter and return value types


4. Add parameters
5. Make call

35 Copyright 2003 Ketan A. Pandya


Creating Dynamic invocation interface client

Service service = sFactory.createService(new QName(qService));


QName port = new QName(qPort);
Call call = service.createCall(port);
call.setTargetEndpointAddress(target_endpoint);

Service name:
CurrencyExchangeService

Port:
CurrencyExchangePortType
Location
http://services.xmethods.com/soap

36 Copyright 2003 Ketan A. Pandya


Creating Dynamic invocation interface client

QName QNAME_TYPE_STRING = new QName(NS_XSD, "string");


QName QNAME_TYPE_FLOAT = new QName(NS_XSD, "float");
call.setReturnType(QNAME_TYPE_FLOAT);
call.setOperationName(new QName(BODY_NAMESPACE_VALUE,
"getRate"));
call.addParameter("param1", QNAME_TYPE_STRING, ParameterMode.IN);
call.addParameter("param2", QNAME_TYPE_STRING, ParameterMode.IN);
String[] params = {"canada", "usa"};
Object respObj = (Object)call.invoke(params); Set parameter data type

Specify operation name


Add parameters

37 Copyright 2003 Ketan A. Pandya

You might also like