Home arrow Java & J2EE arrow Page 5 - Using RPC-Style Web Services with J2EE

RPC-Oriented Web Services - Java

Web Services provide functionality to the Internet, and are seen as the wave of the future. In this article, Martin Bond explains how to use Web Services protocols to join J2EE application components with any other software that supports those protocols. This excerpt is from Chapter (Day) 20, from Teach Yourself J2EE in 21 Days, second edition, by Martin Bond, et. al. (Sams, 2003, ISBN: 0672325586)

TABLE OF CONTENTS:
  1. Using RPC-Style Web Services with J2EE
  2. Web Service Overview
  3. Web Service Technologies and Protocols
  4. Web Services for J2EE
  5. RPC-Oriented Web Services
  6. Creating a Simple Service
  7. The WSDL File
  8. Creating the Web Service WAR
  9. The Web Services Deployment Descriptor
  10. Building More Robust Web Services
  11. Exposing EJBs Through Web Service Protocols
  12. Web Service Compiler Configuration File
  13. Configuring the EJB Component
  14. Other Considerations for Web Services
  15. Summary
By: Sams Publishing
Rating: starstarstarstarstar / 13
December 08, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Remote Procedure Calls (RPCs) made over Web-based protocols are essentially no different from those made over other protocols, such as IIOP, DCOM, or JRMP. The calls are usually synchronous (in other words, the client waits for the method to return before continuing). Zero or more parameters of varying types are passed into the call to provide information to process, and zero or more return values are generated to deliver the outputs of the remote method to the client. The remote method calls are delivered to some form of dispatcher at the remote server that determines which method should be called and arranges for the smooth flow of parameters and return values.

For RPC-style operation, SOAP implementations conform to the preceding description. The difference with SOAP (and other Web-based RPC mechanisms, such as XML-RPC) is that it uses standard, general-purpose transports, such as HTTP, together with a text-based method call description in XML. All the parameters and return values are encoded in XML as part of the SOAP body, while information about the service and method to call are provided in the transport header and possibly the SOAP header. When sent over HTTP, the SOAP header and body are wrapped in another XML document—the SOAP envelope—and this envelope forms the body of an HTTP POST request.

An HTTP-based SOAP message will be delivered to a SOAP router that takes the form of an HTTP servlet (for a Java implementation). The SOAP router will examine the HTTP and SOAP header information and decide how it should forward the message body. This will involve instantiating or calling a particular component or class that will receive the message. The SOAP router, or its helper classes, will also perform the conversion of the XML-based parameters into Java objects and primitives that can be passed as part of the service invocation. Figure 20.5 shows the operation of such a SOAP router. Note that the description of the Web Service is used by both the client and server to help determine the correct mapping between Java and XML for method calls and parameter types.

This is all good, but why go to this effort? Why not use an existing RPC mechanism, such as RMI, or just use HTTP itself?

The justification for not using RMI or CORBA to integrate applications relates to commonality and security. There are at least three different distributed object protocols (CORBA, RMI, and DCOM), each of which has its adherents. The use of HTTP and XML provides a common protocol that is not tied to any vendor. Also, the traditional RPC protocols listed have great difficulty in penetrating most firewalls (not surprising, given their capability to invoke random functionality). However, HTTP (and SMTP) has general right of access through most firewalls, which makes it easier to integrate applications across organizational boundaries (after the security questions are sorted out).

bond

Figure 20.5
A Java-based SOAP router.


Caution - From a developer's perspective, one of SOAP's greatest assets is its capability to penetrate firewalls. However, from an administrator's point of view, this presents the same types of problem as traditional RPC, namely the capability to target a random function call at an exposed server. Although the principle of SOAP is only a small step up from the invocation of server-side functionality such as CGI, great care should be taken to ensure adequate security when exposing Web Services. The overall security story for Web Services is still a work in progress.


Although raw HTTP is a good transport, it was created to exchange simple HTML messages. This does not provide the sophistication required for a distributed invocation environment. The use of a defined XML message format brings structure to this environment and allows for the interoperability of Web Service clients and servers from different vendors—something that escaped CORBA until comparatively recently.

So, Web Service protocols have some large advantages over traditional RPC protocols. However, you need to be careful about where you apply Web Service protocols. Such protocols are primarily intended for the integration of coarse-grained components or application functionality. As mentioned earlier, this might be exposed at the same system boundary as an HTML interface on the same functionality. Internally, the system might use RMI CORBA or DCOM to distribute functionality across several tiers. This is fine; there is no need to replace all distribution mechanisms with Web Service protocols. Web Service interfaces should usually be exposed only at the system boundary, not within the system itself.

Now that you understand the architecture and motivation for RPC-style Web Services, you can create a Java-based Web Service client and server.

The JAX-RPC API

JAX-RPC defines a set of classes, interfaces, principles, and mechanisms for making SOAP-based RPC calls. The main parts are

  • Type conversion between Java types and XML types—For simple types, such as strings and primitive types, very little work is required. More effort is required from either the tools or the developer when mapping more complex types between Java and XML.

  • Mapping between SOAP operations defined in WSDL and Java remote method calls—Essentially, WSDL interfaces are represented in Java by Remote interfaces.

  • The server environment and how calls are received and directed—The receiving Java class has access to particular information about the SOAP call provided through a service context. The lifecycle of a servlet-based service is defined.

  • A client- and server-side SOAP message handler mechanism similar in concept to servlet filters.

  • Client programming model describing the relationship between interfaces and stubs, and how these are obtained in different environments such as J2SE and J2EE.

The essential principle is that JAX-RPC tries to make the development of SOAP-based clients and servers fairly simple and intuitive for Java developers. To start your journey through Web Services, the first example is a simple, servlet-based JAX-RPC service and a standalone client for it.

This chapteris fromTeach Yourself J2EE in 21 Days, second edition, byMartin Bond et. al.(Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.



 
 
>>> More Java & J2EE Articles          >>> More By Sams Publishing
 

blog comments powered by Disqus
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: