Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
JAX-RPC
(Is Changed to JAX-
WS from 2.0)
1
06/15/2005
Sang Shin
sang.shin@sun.com
Java™ Technology Evangelist
Sun Microsystems, Inc.
www.javapassion.com/webservices
2
2
06/15/2005
3
06/15/2005
Revision History
? 10/01/2002: version 1, Created (Sang Shin)
?
01/30/2004: version 2, updated (Sang Shin)
? 10/07/2004: version 3, updated to reflect J2EE 1.4 sample code (Sang Shin)
?
Things to do
– speaker notes need to be added to some slides
4
06/15/2005
Topics
? Background on remote communication
? What is and Why JAX-RPC?
? Development steps of a JAX-RPC Service
? Type Mapping
? Client Programming
? Service Endpoint Model
? Runtime Services
? Packaging & Deployment
5
06/15/2005
6
06/15/2005
Background on
Remote
Communication
7
06/15/2005
Response
Client System Server System
? RPC, COM, CORBA, RMI:
– Synchronous communication: calling process
blocks until there is a response
– More tightly coupled (than non-RPC model):
client must find recipients and know method and
its arguments
– Non persistent
8
First, lets understand some background.
Remote Procedure Call is not a new technology. We have done
this between networked systems for a long time. Most recently,
we have also seen some efforts to standardize the development
using CORBA and COM.
• OMG – Common Object Request Broker Architecture
(CORBA) provided a platform neutral remote procedure call
using Interface Definition Language (IDL) and the Stubs and
Ties
• COM on the Microsoft environment.
• Java Remote Method Invocation (RMI) also provided
distributed programming within Java environment and later
with IIOP to support other environments
• These technologies were not interoperable and now with the
explosion of the web and the standatdisation of the HTTP
protocol, the industry has now decided to come up with one
standard so that we can interoperate between different systems
even though the implementations can be different.
06/15/2005
Stub Tie
9
06/15/2005
rmic compiler
11
11
06/15/2005
Common Interface
? Service is described in IDL (Interface
Description Language)
– IDL of CORBA service
– Java RMI interface in RMI (Language specific)
– WSDL for Web service
? Used by tools to statically or dynamically
generate and/or configure interfaces,
proxies, and ties in a network
technology specific environment
12
12
06/15/2005
13
13
06/15/2005
Why
XML-based RPC?
14
14
06/15/2005
15
15
06/15/2005
16
06/15/2005
What is
JAX-RPC?
17
17
06/15/2005
What is JAX-RPC?
? Java ™ API for XML-based RPC
– Web Services operations are performed by
exchanging SOAP 1.1 messages
? Services are described using WSDL
– WSDL is the contract between service provider
and client (only contract needed)
? Web service endpoints and clients use
JAX-RPC programming model
? Key technology for Web Services in the
J2EE 1.4 platform
18
18
06/15/2005
?Keep the design of JAX-RPC APIs and mechanisms extensible and modular.
This will enable support for future versions of various XML specifications,
including XMLP (SOAP 1.2)
19
06/15/2005
JAX-RPC Architecture
JAX-RPC JAX-RPC
Client WSDL Document Service Endpoint
JavaïWSDL WSDLóJava
Generated Code
Container Container
SOAP
HTTP
20
20
06/15/2005
21
06/15/2005
Development Steps of
JAX-RPC Service
(HelloService Example)
22
22
06/15/2005
Implementation classes
Servlet-based endpoint model
Optional handler and serializer classes
tool
WSDL Service contract
23
23
06/15/2005
Implementation classes
24
24
06/15/2005
JAX-RPC
Web Service Endpoint
JAX-RPC
Service Endpoint
WSDL<-->Java
Server-Side
JAX-RPC
Runtime System
SOAP
HTTP
25
25
06/15/2005
These are the basic steps for creating theWeb service and its client.
26
06/15/2005
27
06/15/2005
import java.rmi.Remote;
import java.rmi.RemoteException;
28
28
06/15/2005
29
06/15/2005
30
30
06/15/2005
31
31
06/15/2005
32
32
06/15/2005
MyHelloService.wsdl
<?xml version="1.0" encoding="UTF-8"?>
34
06/15/2005
MyHelloService.wsdl (continued)
<binding name="HelloIFBinding" type="tns:HelloIF">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="sayGoodbye">
<soap:operation soapAction=""/>
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="urn:Foo"/></input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="urn:Foo"/></output></operation>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="urn:Foo"/></input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="urn:Foo"/></output></operation></binding>
<service name="MyHelloService">
<port name="HelloIFPort" binding="tns:HelloIFBinding">
<soap:address
location="REPLACE_WITH_ACTUAL_URL "/></port></service></definitions> 35
35
06/15/2005
mapping.xml (page 1)
<?xml version="1.0" encoding="UTF-8"?>
<java-wsdl-mapping xmlns="http://java.sun.com/xml/ns/j2ee" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://www.ibm.com/webservices/xsd/j2ee_jaxrpc_mapping_1_1.xsd">
<package-mapping>
<package-type>helloservice</package-type>
<namespaceURI>urn:Foo</namespaceURI>
</package-mapping>
<service-interface-mapping>
<service-interface>helloservice.MyHelloService</service-interface>
<wsdl-service-name
xmlns:serviceNS="urn:Foo">serviceNS:MyHelloService</wsdl-service-name>
<port-mapping>
<port-name>HelloIFPort</port-name>
<java-port-name>HelloIFPort</java-port-name>
</port-mapping>
</service-interface-mapping>
36
36
06/15/2005
mapping.xml (continued)
<service-endpoint-interface-mapping>
<service-endpoint-interface>helloservice.HelloIF</service-endpoint-interface>
<wsdl-port-type xmlns:portTypeNS="urn:Foo">portTypeNS:HelloIF</wsdl-port-type>
<wsdl-binding xmlns:bindingNS="urn:Foo">bindingNS:HelloIFBinding</wsdl-binding>
<service-endpoint-method-mapping>
<java-method-name>sayGoodbye</java-method-name>
<wsdl-operation>sayGoodbye</wsdl-operation>
<method-param-parts-mapping>
<param-position>0</param-position>
<param-type>java.lang.String</param-type>
<wsdl-message-mapping>
<wsdl-message xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:HelloIF_sayGoodbye
</wsdl-message>
<wsdl-message-part-name>String_1</wsdl-message-part-name>
<parameter-mode>IN</parameter-mode>
</wsdl-message-mapping>
</method-param-parts-mapping>
<wsdl-return-value-mapping>
<method-return-value>java.lang.String</method-return-value>
<wsdl-message xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:HelloIF_sayGoodbyeResponse
</wsdl-message>
<wsdl-message-part-name>result</wsdl-message-part-name>
</wsdl-return-value-mapping>
</service-endpoint-method-mapping>
37
37
06/15/2005
mapping.xml
<service-endpoint-method-mapping>
<java-method-name>sayHello</java-method-name>
<wsdl-operation>sayHello</wsdl-operation>
<method-param-parts-mapping>
<param-position>0</param-position>
<param-type>java.lang.String</param-type>
<wsdl-message-mapping>
<wsdl-message xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:HelloIF_sayHello
</wsdl-message>
<wsdl-message-part-name>String_1</wsdl-message-part-name>
<parameter-mode>IN</parameter-mode>
</wsdl-message-mapping>
</method-param-parts-mapping>
<wsdl-return-value-mapping>
<method-return-value>java.lang.String</method-return-value>
<wsdl-message
xmlns:wsdlMsgNS="urn:Foo">wsdlMsgNS:HelloIF_sayHelloResponse
</wsdl-message>
<wsdl-message-part-name>result</wsdl-message-part-name>
</wsdl-return-value-mapping>
</service-endpoint-method-mapping>
</service-endpoint-interface-mapping>
38
</java-wsdl-mapping>
38
06/15/2005
39
39
06/15/2005
40
40
06/15/2005
41
41
06/15/2005
Type Mapping
42
42
06/15/2005
43
06/15/2005
Type Mapping
Mapping of XML Data
types to Java Data types
44
44
06/15/2005
XML data types that can be mapped to Java data types include simple
types such as the ones defined in W3C XML schema. For example, string
type element will be mapped into java.lang.String class while dateTime
will be mapped into java.util.Calendar class.
45
06/15/2005
// Java Representation
public class EyeColor implements java.io.Serializable {
// Constructor
protected EyeColor(String value) { ... }
public static final String _green = “green”;
public static final String _blue = “blue”;
public static final EyeColor green = new EyeColor(_green);
public static final EyeColor blue = new EyeColor(_blue);
public String getValue() { ... }
public static EyeColor fromValue(String value)
throws java.lang.IllegalStateException { ... }
public boolean equals(Object obj) { ... }
public int hashCode() { ... }
}
46
46
06/15/2005
// Java
public class Book implements java.io.Serializable {
// ...
public String getAuthor() { ... }
public void setAuthor(String author) { ... }
public String getPreface() { ... }
public void setPreface(String preface) { ... }
public float getPrice() { ... }
public void setPrice(float price) { ... }
}
47
47
06/15/2005
Type Mapping
Mapping of Java Data
types to XML Data types
48
48
06/15/2005
49
49
06/15/2005
Supported Types
? Subset of J2SE classes
? Collections
? Primitives
? Arrays
? Value types
? JavaBeans
50
50
06/15/2005
51
51
06/15/2005
Collections
? List
– ArrayList, LinkedList, Stack, Vector
? Map
– HashMap, Hashtable, Properties, TreeMap
? Set
– HashSet, TreeSet
52
52
06/15/2005
53
53
06/15/2005
Arrays
? Arrays with members of supported JAX-
RPC types
? Examples
– int[]
– String[]
– BigDecimal[][]
54
54
06/15/2005
Value Types
? A value type is a class whose state may
be passed between a client and remote
service as a method parameter or return
value
? Example
– Book class which contains the fields Title,
Author, and Publisher
55
A value type is a class whose state may be passed between a client and
remote service as a method parameter or return value. For example, in
an application for a university library, a client might call a remote
procedure with a value type parameter named Book, a class that
contains the fields Title, Author, and Publisher.
55
06/15/2005
The value type may contain public, private, or protected fields. The field
of a value type must meet these requirements:
56
06/15/2005
// private fields
private String id;
57
57
06/15/2005
JavaBeans
? Must have a getter and setter method for
each bean property
? The type of the bean property must be a
supported JAX-RPC type
58
58
06/15/2005
59
06/15/2005
WSDL to Java
Mapping
60
60
06/15/2005
61
06/15/2005
Port "Bar"
62
62
06/15/2005
63
63
06/15/2005
64
64
06/15/2005
65
65
06/15/2005
66
06/15/2005
WSDL portType/operation/message
? A wsdl:portType maps into a Java Interface
(Service Definition Interface) that extends
java.rmi.Remote
? A wsdl:operation is mapped into a method of
the Service definition interface
? wsdl:message's are mapped into parameters
of the method
? wsdl:type's of wsdl:message's are mapped
into the types of the parameters
67
67
06/15/2005
In this example, the upper part of the slide contains abstract part of an example WSDL
document in which portType, operation, and message elements are defined. The
bottom part of this slide is the Java representation of these elements.
So in this example, the name of the portType is used as a name of Java interface type,
and as I said before, this Java interface type is called as Service definition interface. In
this example, they are highlighted in red color. The name of the operation element
which is highlighted in blue color gets translated into a method with a same name. And
we have a single operation, GetLastTradePrice so we get a single method called
getLastTradePrice() in the service definition interface. The input and output messages
and their types are translated as parameters of the method.
Please also note that the service definition interface extends java.rmi.Remote and each
method throws java.rmi.RemoteException.
Again please note that Service Definition Interface represents only the abstract part of a
WSDL document, that is, it does not talk about runtime aspect such as how the service
is to be bound to a particular XML or transport protocol.
68
06/15/2005
69
06/15/2005
70
06/15/2005
WSDL binding/port/service
? wsdl:service is mapped into an
implementation of javax.xml.rpc.Service
interface
? JAX-RPC runtime provides the
implementation
71
71
06/15/2005
WSDL binding/port/service
? A javax.xml.rpc.Service class acts as
a factory of
– Instance of a generated stub class
– Dynamic proxy for a service port
– Instance of the type javax.xml.rpc.Call for the
dynamic invocation of a remote operation on
a service port
72
72
06/15/2005
<service name="StockQuoteService">
<documentation>My first service</documentation>
<port name="StockQuotePort" binding="tns:StockQuoteBinding ">
<soap:address location="http://example.com/stockquote"/>
</port>
</service>
73
73
06/15/2005
javax.xml.rpc.Service
package javax.xml.rpc;
public interface Service {
public java.rmi.Remote getPort(QName portName,
Class proxyInterface)
throws JAXRPCException;
The getPort method returns a dynamic proxy for the specified service port. A service client
uses this dynamic proxy to invoke operations on the target service port. The
proxyInterface parameter specifies the service definition interface that is supported by the
created dynamic proxy.
The multiple variants of the method createCall create instances of the javax.xml.rpc.Call.
The javax.xml.rpc.Call interface provides support for the dynamic invocation of an
operation on the target service port. A client side JAX-RPC 1.0 implementation is required
to implement the Call interface. The javax.xml.rpc.Service interface acts as a factory for
the creation of Call instances.
74
06/15/2005
javax.xml.rpc.Call
package javax.xml.rpc;
public interface Call {
static public final int PARAM_MODE_IN = 1;
static public final int PARAM_MODE_OUT = 2;
static public final int PARAM_MODE_INOUT = 3;
public void addParameter(String paramName, QName xmlType, int parameterMode);
public void setReturnType(QName xmlType);
public void removeAllParameters();
public String getOperationName();
public void setOperationName(String operationName);
public QName getPortTypeName();
public void setPortTypeName(QName portType);
public String getTargetEndpointAddress();
public void setTargetEndpointAddress(String address);
public void setProperty(String name, Object value) throws JAXRPCException;
public Object getProperty(String name);
public void removeProperty(String name);
// Remote Method Invocation methods
public Object invoke(Object[] params) throws java.rmi.RemoteException;
public void invokeOneWay(Object[] params) throws javax.xml.rpc.JAXRPCException;
}
75
75
06/15/2005
76
76
06/15/2005
77
77
06/15/2005
SOAP Binding
Support in JAX-RPC
78
78
06/15/2005
79
06/15/2005
80
06/15/2005
Client Programming
Model
81
81
06/15/2005
82
06/15/2005
83
06/15/2005
84
06/15/2005
85
85
06/15/2005
86
06/15/2005
<<interface>> <<interface>>
javax.xml.rpc.Stub com.example.stockQuoteProvider
com.example.StockServiceSoapBinding_Stub
87
87
06/15/2005
Before it can invoke the remote methods on the stub the client performs these
steps:
(Stub)(new MyHelloService_Impl().getHelloIFPort())
stub._setProperty
(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);
88
06/15/2005
Stub Configuration
? Stub instance must be configured
– XML protocol binding (compile time configuration)
– endpoint address (can be set at runtime)
? Can be configured in two ways
– Static configuration (Compile time) based on
the WSDL description of a target service
endpoint
? wsdl:binding, soap:binding, wsdl:port
89
06/15/2005
90
90
06/15/2005
91
06/15/2005
92
92
06/15/2005
The client in the preceding section used a static stub for the proxy. In contrast,
the client example in this section calls a remote procedure through a dynamic
proxy, a class that is created during runtime. Although the source code for the
static stub client relied on an implementation-specific class, the code for the
dynamic proxy client does not have this limitation.
93
06/15/2005
94
Before it can invoke the remote methods on the stub the client performs these
steps:
(Stub)(new MyHelloService_Impl().getHelloIFPort())
stub._setProperty
(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);
94
06/15/2005
The createService method has two parameters, the URL of the WSDL file and a
QName object. At runtime, the client gets information about the service by
looking up its WSDL. In this example, the URL of the WSDL file points to the
WSDL that was deployed with MyHelloService:
http://localhost:8080/hello-jaxrpc/hello?WSDL
A QName object is a tuple that represents an XML qualified name. The tuple is
composed of a namespace URI and the local part of the qualified name. In the
QName parameter of the createService invocation, the local part is the service
name, MyHelloService.
95
06/15/2005
The createService method has two parameters, the URL of the WSDL file and a
QName object. At runtime, the client gets information about the service by
looking up its WSDL. In this example, the URL of the WSDL file points to the
WSDL that was deployed with MyHelloService:
http://localhost:8080/hello-jaxrpc/hello?WSDL
A QName object is a tuple that represents an XML qualified name. The tuple is
composed of a namespace URI and the local part of the qualified name. In the
QName parameter of the createService invocation, the local part is the service
name, MyHelloService.
96
06/15/2005
import java.net.URL;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import dynamicproxy.HelloIF;
97
97
06/15/2005
Service helloService =
serviceFactory.createService(helloWsdlUrl,
new QName(nameSpaceUri, serviceName));
dynamicproxy.HelloIF myProxy =
(dynamicproxy.HelloIF)
helloService.getPort(
new QName(nameSpaceUri, portName),
dynamicproxy.HelloIF.class);
System.out.println(myProxy.sayHello("Buzz"));
98
98
06/15/2005
ant build
* generate-interface
* compile-client
* package-dynamic
The generate-interface task runs wscompile with the -import option. The
wscompile command reads the MyHelloService.wsdl file and generates the
service endpoint interface class (HelloIF.class). Although this wscompile
invocation also creates stubs, the dynamic proxy client does not use these stubs,
which are required only by static stub clients.
99
06/15/2005
100
100
06/15/2005
101
With the dynamic invocation interface (DII), a client can call a remote
procedure even if the signature of the remote procedure or the name of the
service are unknown until runtime. In contrast to a static stub or dynamic proxy
client, a DII client does not require runtime classes generated by wscompile.
However, as you'll see in the following section, the source code for a DII client
is more complicated than the code of the other two types of clients.
101
06/15/2005
102
102
06/15/2005
Before it can invoke the remote methods on the stub the client performs these
steps:
(Stub)(new MyHelloService_Impl().getHelloIFPort())
stub._setProperty
(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);
103
06/15/2005
104
Service service =
factory.createService(new QName(qnameService));
<service name="MyHelloService">
104
06/15/2005
<portType name="HelloIF">
105
06/15/2005
106
call.setTargetEndpointAddress(endpoint);
106
06/15/2005
107
107
06/15/2005
call.setOperationName(new QName(BODY_NAMESPACE_VALUE,
"sayHello"));
call.addParameter("String_1", QNAME_TYPE_STRING,
ParameterMode.IN);
108
To specify the return type, the program invokes the setReturnType method on the
Call object. The parameter of setReturnType is a QName object that represents an
XML string type.
To indicate the method parameter, the program invokes the addParameter method
on the Call object. The addParameter method has three arguments: a String for
the parameter name (String_1), a QName object for the XML type, and a
ParameterMode object to indicate the passing mode of the parameter (IN).
108
06/15/2005
109
109
06/15/2005
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.ParameterMode;
110
06/15/2005
try {
ServiceFactory factory = ServiceFactory.newInstance();
Service service =
factory.createService(
new QName(qnameService));
call.setProperty(Call.SOAPACTION_USE_PROPERTY,
new Boolean(true));
call.setProperty(Call.SOAPACTION_URI_PROPERTY
"");
call.setProperty(ENCODING_STYLE_PROPERTY,
URI_ENCODING);
QName QNAME_TYPE_STRING =
new QName(NS_XSD, "string"); 111
call.setReturnType(QNAME_TYPE_STRING);
111
06/15/2005
112
112
06/15/2005
Service Endpoint
Model
113
113
06/15/2005
114
114
06/15/2005
So in summary, over J2EE platform, the client view is defined in JAX-RPC specification.
And the server view can be in two different forms, one in servlet-based, and the other is
stateless session bean based. The servlet based endpoint model is defined in JAX-RPC
specification while stateless session bean based endpoint model is defined in EJB 2.1
specification. Servlet endpoint runtime is provided by web container while stateless session
bean runtime is provided by EJB container.
115
06/15/2005
Service Developer
? Generates service definition interface
? Implements service definition interface
(service endpoint class)
? Service endpoint class
– May implement ServiceLifecycle interface
– Can access servlet context via
javax.servlet.ServletContext interface
? Creates *.war package
116
116
06/15/2005
Service Deployer
? Handles
– Protocol binding
– Port assignment
? Multiple protocol binds for a single service
endpoint definition
? Creates web.xml
? Creates complete WSDL document which
contains concrete binding information
– This WSDL document can be published
? Creates and deploy assembled *.war file
117
117
06/15/2005
Protocol and
Transport Binding
118
118
06/15/2005
119
06/15/2005
JAX-RPC Runtime
System
120
120
06/15/2005
121
06/15/2005
Programming
Model
122
122
06/15/2005
123
06/15/2005
124
124
06/15/2005
125
125
06/15/2005
126
126
06/15/2005
Package Relationship
javax.xml.rpc javax.xml.messaging
javax.xml.soap
127
127
06/15/2005
Resources
128
128
06/15/2005
Resources
? JAX-RPC Home
– http://java.sun.com/xml/jax-rpc/index.html
? Java Web Services Developer Pack Download
– http://java.sun.com/webservices/downloads/webservicespack.html
? Java Web Services Developer Pack Tutorial
– http://java.sun.com/webservices/downloads/webservicetutorial.html
? J2EE 1.4 SDK
– http://java.sun.com/j2ee/1.4/download-dr.html
129
129
06/15/2005
Passion!
130
130