Java APIs for XML-based Remote Procedure Call (JAX-RPC) help with Web service interoperability and accessibility by defining Java APIs that Java applications use to develop and access Web services. JAX-RPC fully embraces the heterogeneous nature of Web services -- it allows a JAX-RPC client to talk to another Web service deployed on a different platform and coded in a different language. Similarly, it also allows clients on other platforms and coded in different languages to talk to a JAX-RPC service. JAX-RPC also defines the mapping between WSDL service descriptions and Java interfaces. This article introduces the JAX-RPC technology and describes its client and server programming models. JAX-RPC hides the complexity of underlying protocols and message-level processing from application developers crafting Web services using the Java 2 platform. The API combines XML with Remote Procedure Call (RPC), which is a mechanism enabling clients to execute procedures on distributed or remote systems, so that developers can build Web services and clients. The JAX-RPC remote procedure calls are represented by an XML infoset and they are carried over a network transport. While the JAX-RPC APIs rely on a XML-based protocol and a network transport, the APIs themselves are independent of a specific protocol or transport. The current JAX-RPC implementation relies on the SOAP 1.1 protocol and HTTP 1.1 network transport. SOAP and WSDL are language-neutral standards defined by the World Wide Web Consortium (W3C). SOAP is a lightweight, XML-based protocol for exchange of information in a decentralized, distributed environment. The core of this protocol is a SOAP envelope. The envelope defines a framework for describing what is in a message and how to process it. SOAP defines a set of encoding rules for expressing instances of application-defined data types. It defines a convention for representing RPC requests and responses. The SOAP specification also defines a transport-binding framework for exchanging messages using an underlying protocol. WSDL specifies an XML format for describing a Web service as a set of endpoints operating on messages. The operations and messages are defined abstractly and then bound to a concrete network protocol (HTTP) and a message format (SOAP) to define an endpoint (or port). Related ports are combined into abstract endpoints, also called services. WSDL describes the Web service in a standard format: it specifies the service's location and the operations exposed by the port in the service. The JAX-RPC reference implementation, available in the Java Web Services Developer Pack Early Access 2 (JWSDP EA2) release, includes a tool that can parse existing WSDL files or generate WSDL files for a Web service. An Example Application: SunReg Web ServiceThis article uses an example enterprise Web service to illustrate the JAX-RPC concepts and programming models. This enterprise Web service, called SunReg, allows employees in an organization to inquire about and register for various courses offered by their internal education center. SunReg allows employees to send queries about the courses offered by the education center. The Web service provides the employee with information about each course, such as course description, the campus where the course is conducted, and the duration of the course. Employees can also use SunReg to enroll in specific courses of their interest. The employee and course information is stored in a database. Figure 1 shows a use case diagram for the SunReg Web service.
The SunReg application exposes these functions as Web services to all employees of the organization. Employees access the Web service using browser-based clients, such as applets or Java applications. In addition, a client on a different platform can access the Web service using a different language. This kind of interoperability is achieved because JAX-RPC adheres to SOAP 1.1 and WSDL 1.1 standards.
Figure 1. The SunReg Web service allows users to look for a course and register for a course. SunReg is developed, deployed, and published as a JAX-RPC Web service endpoint. Once developed, an endpoint is deployed in a container that supports the JAX-RPC runtime system, which in turn supports the underlying XML-based protocol and transport. The Web service endpoint can be a servlet-based endpoint deployed on a servlet container or a stateless session bean. JWSDP provides Tomcat as the servlet container, which is what we used for the SunReg Web service endpoint. JWSDP also provides a Java-to-WSDL mapping tool that can publish the service description along with the endpoint. Once the service is deployed and published, clients import the WSDL and typically use the WSDL-to-Java mapping tool to generate client-side artifacts for invoking this service endpoint. A Note About Software Versions Used in This ExampleIn this article, we use the JAX-RPC reference implementation that comes bundled with JWSDP EA2 for developing and deploying our Web service. The JAX-RPC implementation in JWSDP EA2 is based on the 0.7 version of the specification. The code examples in this article are compatible with the JWSDP EA2 release, current at this writing. The JWSDP first customer release, currently planned for mid-June, will incorporate the latest revisions of the JAX-RPC specification. The code samples here will need an update at that time; look for updates or advice about migrating applications on this Web site about that time.
Also note that we refer to the JWSDP installation directory as Developing a ServiceAs noted already, a JAX-RPC service endpoint represents a Web service endpoint. When you're ready to create a service endpoint, you:
Coding the Service Endpoint Interface
You start by defining two classes. First you define the class that represents the remote interface to the service; this is the service endpoint interface. It contains the signatures for the methods that a client may invoke on the service. The service endpoint interface extends the Code Sample 1: The service endpoint interface, SunRegPort
As required, the service endpoint interface,
Employees use
Employees use the
The classes
Figure 2: Course and Employee JAX-RPC Value types
Coding the Service Endpoint Class
The service endpoint class provides an actual implementation of the methods defined in the service endpoint interface. (You can also call a service implementation class a servant, as we do later in this article.) Code Sample 2 shows the service implementation class for Code Sample 2: The service implementation class SunRegImpl
The
Through the implementation of the
For example, SunReg can use the context passed in the Defining the Configuration File
Next, you need to define a configuration file to generate the various server-side artifacts required by the JAX-RPC runtime. This configuration file specifies the name of the service (or services) and its endpoint interface and class. This configuration file, called Code Sample 3: The configuration file for SunReg
Take a more detailed look at the significant attributes in the configuration file:
Defining the Deployment Descriptor
Because we intend to deploy SunReg on JWSDP as a WAR (Web archive file) file, we need to create a Code Sample 4: The web.xml for deploying the SunReg service on a JWSDP container
Notice in Code Sample 4 that the
The servlet class
Finally, you specify the Compiling the ServiceYou compile the service endpoint interface and service endpoint class using a Java compiler. Then you generate the various server-side artifacts required by the JAX-RPC runtime by inputting the configuration tool to the Java-to-WSDL mapping tool, xrpcc, which comes bundled with JWSDP. One of the required artifacts is the WSDL service description. Recall that a service endpoint is mapped to a WSDL service description. This WSDL document describes the service endpoint in terms of abstract definitions of port types, operations, messages, and concrete protocol binding.
You'll find the xrpcc tool in the directory
The options to the xrpcc tool are:
Alternatively, you can generate just the client-side and server-side artifacts by specifying either the Among the various artifacts generated by the xrpcc tool, stubs and ties are the most important. Stubs and ties are classes that enable communication between a service endpoint and a service client. The stub class sits on the client side, between the service client and the JAX-RPC client runtime system. The stub class is responsible for converting a request from a JAX-RPC service client to a SOAP message and sending it across to the service endpoint using the specified protocol (in our case HTTP). It also converts the response from the service endpoint, which it receives in the form of a SOAP message, to the format required by the client. Converting a client request to SOAP format is called marshalling; converting back from SOAP format to a client response is unmarshalling. Similarly, the tie class resides on the server side, between the service endpoint and the JAX-RPC runtime system. The tie class handles marshalling and unmarshalling the data between the service endpoint class and the SOAP format. A stub is a local object that acts as a proxy for the service endpoint, as illustrated in Figure 3.
Figure 3: JAX-RPC runtime and generated classes In addition to stubs and ties, the xrpcc tool generates its client-side implementation classes for the service, as explained in Invoking the Service.
The xrpcc tool also generates a properties file that associates each servant with a generated tie file (on the server side). The name of this file is formed by appending _Config.properties to the The xrpcc tool also generates a WSDL file that describes the service in a standard format.
Although it's not recommended to edit the
That line makes our WSDL service description available from the service endpoint simply by appending ?WSDL to the service endpoint. Note that we have to exclude the port information when accessing the WSDL from the endpoint. We also need to ensure that the Deploying the Service
Once the xrpcc tool has completed its job, you must package and deploy the service on a servlet container (we used Tomcat for the example application). The service endpoint interface, service endpoint class,
Figure 4: Deploying the service and related files. The developer writes two Java language files, two XML files, and the client application. Note that there are generated files in addition to the ones shown in Figure 4. All of the generated files are packaged to the WAR file. The structure of the WAR file for SunReg looks like Code Sample 5. Code Sample 5: The structure of the web.xml WAR file for the example SunReg application
In this
How might a client access the SunReg service? If the SunReg service is packaged into a WAR file named
Note that
Figure 5. The display that appears when a user accesses http://howru:8080/SunReg/jaxrpc in a browser
The browser shows all the ports supported at this endpoint. Each port is mapped to a Java interface. Thus, the Web browser displays
The WSDL description for the service can be accessed by clicking the "here" link or it can be found at the URL:
Invoking the ServiceThe JAX-RPC Web service endpoint is available to any type of client, regardless of the language or platform used by that client. A service client invokes a service endpoint using one of the following methods:
First let's discuss using the stub-based programming model to invoke the service. There are two approaches with this model. You can generate stubs from a service endpoint interface by running the xrpcc tool with either the Generating Stubs from the Service Endpoint Interface
To generate stubs from the service endpoint interface, you provide either the To access this Web service, you need to write three lines of code in the client application, as follows:
Code sample 6 shows an example. Code Sample 6: Accessing the Web Service
The xrpcc tool generates its client-side implementation classes for the service, as we discussed earlier. A client-side implementation class enables the client to get a handle to the stub of the service endpoint interface. The xrpcc tool forms the name of the client implementation file by appending _Impl to the
After getting a reference to the stub, you set the endpoint address on it by invoking the
When the client invokes a method on the service through the stub, the stub marshals the request into a SOAP message and transmits the SOAP packet to the JAX-RPC servlet deployed in the servlet container. The JAX-RPC servlet looks at the
An employee can browse the course information and can invoke Typically, an enterprise has a system administrator handle the complexities of generating stubs and editing the configuration files. The users of the application see a well-formed graphical user interface that includes hooks to the actual JAX-RPC application. An application can be run as an applet if the correct permissions are set. To run this application as an applet, you must give permissions to the applet sandbox security model, as shown in Code Sample 7. Code Sample 7: Setting up the application to run as an applet
Generating Stubs from WSDL
Because the WSDL service description for SunReg has already been published, we can generate the various client-side artifacts by importing the WSDL into the xrpcc tool. To do this, you use the
Because the Web service is already installed, starting from WSDL does not require generating server-side artifacts (such as tie files). The xrpcc tool does not expect service definition files as input; instead, it expects as input the XML configuration file with the information about the WSDL file. A WSDL file, which is the file published in the public registry, describes a complete Web service in a standard format. In addition, a WSDL file describes how to access the Web service: it contains the port offered by the service and the endpoint where the service is installed. A port is the WSDL equivalent to a remote Java interface (for example, Keep in mind that you do not need to know these WSDL details. Usually, you use a tool to generate WSDL files or to interpret already generated WSDL files. The xrpcc tool that comes bundled with JWSDP is one such tool. Returning to the SunReg example application, generating clients from the service endpoint interface is most useful when all employees access the same set of courses. It's conceivable that some departments within the organization may want to filter the set of courses available for their employees. To filter courses, they can use the xrpcc tool to import the WSDL file and generate stub files. They can then use these stubs to filter the courses. Let's look at how to do this.
To begin, assume that the SunReg service is deployed in a servlet-based container, just as in the description of generating stubs from the service endpoint interface earlier in this section. The endpoint has been published and we have added the Code Sample 8: The contents of the XML configuration file, config.xml
Notice that the
Using this information, the tool generates the necessary client-side artifacts. Note that the WSDL accessible by the
From the client application's perspective, invoking methods on this Web service is similar to that of the model where stubs were generated from the service endpoint interface. The client first invokes the
SunReg_Impl stub = SunRegIF_Stub(new
SunReg_Impl()).getSunRegIF();
Course[] courseArray = stub.getCourseInfo();
Invoking Dynamic Invocation InterfaceSometimes the xrpcc tool does not have the requisite tools to parse the WSDL and generate client-side artifacts. When this happens, the client uses a dynamic invocation interface (DII) instead. Using DII, a client can call a service or a remote procedure on a service without knowing the exact service name or the procedure's signature ahead of time. A DII client can discover this information at runtime, making use of a service broker that can dynamically look up the service and its remote procedures. You can refer to the Java Web Services Developer Pack tutorial for an example of a DII client. ConclusionThis article explains some of the basic JAX-RPC programming concepts. It describes the JAX-RPC client and server programming models and provides some simple examples to illustrate their use. The article is intended to give developers a good grasp of how to use JAX-RPC to develop or use Web services. Forthcoming articles will explore JAX-RPC and related topics in more depth. They will also bring the reader to the current revision of the JAX-RPC technology. Feel free to post your questions to the jaxrpc-interest alias. For More Information
About the AuthorsArun Gupta is leading the effort to test the JAX-RPC implementation at Sun Microsystems, Inc. He has been with the JAX-RPC group since its inception and has been a significant contributor in evolving the technology. Arun has also worked with CORBA and other distributed technologies in Java. You'll find him active at jaxrpc-interest alias. Beth Stearns is the principal partner of ComputerEase Publishing, a computer consulting firm she founded in 1982. Her client list includes Sun Microsystems, Inc., Silicon Graphics, Inc., Oracle Corporation, and Xerox Corporation. Among her publications are the "Java Native Interface" chapter in "The Java Tutorial Continued" book in the Addison Wesley Java series, "The EJB Programming Guide" for Inprise Corporation, and "Understanding EDT", a guide to Digital Equipment Corporation's text editor. Most recently, she co-authored with Vlada Matena of the Addison Wesley Java series book, "Applying Enterprise JavaBeans: Component-Based Development for the J2EE Platform." Beth has also contributed to two books recently published in the Addison-Wesley Java Series: "Designing Enterprise Applications with the J2EE Platform, Second Edition" and "The J2EE Tutorial." | ||||||||||||||||
|
| ||||||||||||