|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Book Excerpt Web Services Messaging with JAX-RPC
From Chapter 5
By: Mark D. Hansen
Aug. 3, 2004 12:00 AM
The last chapter introduced simple SOAP messaging. In this chapter, we move to the next level of sophistication and introduce a true messaging framework that forms the backbone of the EAI infrastructure developed in this book. This messaging framework enables Java applications to send and receive Java objects to and from web services using SOAP as the transport. It also enables asynchronous messaging whereby the sender of a message does not have to block waiting for the receiver to process it. We call our framework the Web Services Messaging Framework (WSMF). It is implemented using J2EE and Web Services standards and serves as the core messaging service used by all the non-JMS EAI applications developed in the remainder of this book. The WSMF is not a commercial-quality messaging framework, but rather an illustrative example of how to implement Web Services oriented messaging within the J2EE environment. In this chapter, you will encounter the major issues associated with implementing SOAP messaging in Java and some solutions that can be applied to your own EAI projects. Our WSMF is implemented using the Java API for XML-based RPC (JAX-RPC). As the reader is probably aware, JAX-RPC was created to provide Java developers with a standard for incorporating into Java applications the XML-based Remote Procedure Call (RPC) functionality described in Section 7 of the SOAP 1.1 specification. JAX-RPC was not intended for messaging. The reasons that we have chosen JAX-RPC as the standard for our messaging framework are discussed in Section 1.1. In addition to JAX-RPC, this chapter introduces two additional standards: JAXB and WSDL. The Java Architecture for XML Binding (JAXB) provides a standard approach, based on XML Schema, for serializing and deserializing between XML documents and Java objects. The Web Services Description Language (WSDL) is a W3C standard interface definition language for specifying web services APIs using XML. Typically, a web services client reads the WSDL to determine how to structure SOAP messages that can be accepted as valid requests by the service. This chapter assumes that the reader has a basic familiarity with JAX-RPC and WSDL. If you have never encountered JAXB, that should not be a problem. You can probably pick it up as you read through the material presented here. If you need to brush up on any of these standards, the author suggests Sun's Java Web Services Tutorial (java.sun.com/webservices/docs/1.4/tutorial/doc/index.html) for JAX-RPC and JAXB. For WSDL, we suggest that you read through the specification (www.w3.org/TR/wsdl). It is not very long and provides a lot of illustrative examples. 5.1 JAX-RPC for Messaging Another standard, JAXM, has been developed specifically for XML messaging. However, because JAX-RPC is a required part of the J2EE platform (starting with version 1.4) and the JAXM standard is optional, we believe that a messaging framework based on JAX-RPC is easier to introduce into a corporate Java environment. Any IT department supporting J2EE must support JAX-RPC. Therefore, any code you develop using the techniques illustrated in this chapter should be usable in a corporate Java environment. As an interesting aside, you may be wondering why there is no true messaging standard for Web Services included in the J2EE standard (at least at the time of this writing). I.e., why is JAXM not included in the J2EE standard? The reason is that many of the vendors driving Java standards (e.g., IBM, BEA, Fujitsu) feel that J2EE already supports enough messaging standards. As IBM wrote in casting their vote against final approval of JSR #67 (JAXM): We are significantly concerned that the introduction of JAX-M as yet another API for calling a service (along with RMI-IIOP, JMS, and the proposed JAX-RPC) is unnecessarily complicating the Java programming model for our developers. As a result of this decision, we need to work a little harder to implement Web Services messaging within the J2EE framework. This is not all bad, however, because it gives us an opportunity to gain a detailed understanding of the important JAX-RPC standard. The JAX-RPC 1.0 specification is long and fairly complex. You don't need to understand the entire standard to learn how to use it for messaging. The approach that we suggest is to start working through the examples in this chapter and refer back to the JAX-RPC Javadoc API and specification as necessary. When reviewing JAX-RPC, you should focus on these features, which are used by the WSMF:
In the following sections, we look at each of these features and discuss how they are used to implement the WSMF. Sections 1.2 and 1.3 illustrate how JAX-RPC support for the SOAP 1.1 binding is leveraged to facilitate communication with Web Services using SOAP over HTTP. We first look at simple one-way messaging and then at request-response. Section Error! Reference source not found. introduces the server side framework for implementing messaging services. Section Error! Reference source not found. shows how WSDL is used to describe messaging services. We also look at how JAX-RPC uses WSDL descriptions to create client side representations of services. Section Error! Reference source not found. discusses how and why we use custom XML/Java bindings (e.g., JAXB) with JAX-RPC. Lastly, Section Error! Reference source not found. ties this all together and demonstrates a sample application that uses the WSMF. 5.2 One-Way Messaging Using JAX-RPC (Client Side) Figure 5-1 illustrates how One-Way Messaging is implemented using JAX-RPC. This figure traces the journey of a message object from sender to receiver and is accurate for both the blocking and non-blocking scenarios. In either case, something may come back to the sender at a lower protocol level, e.g., a simple HTTP ACK, an empty SOAP message, or a SOAP fault. That depends on the implementation details, which we get in to later. What defines one-way messaging is that no response is returned at the message level. Here the term message refers to the contents of the SOAP body and not the SOAP document itself. In Section Error! Reference source not found. we will see that an equivalent definition says that a message service is one-way if and only if it's WSDL port type specifies a one-way operation. As illustrated, one-way messaging involves these steps.
Example 5-1 Interface for One-Way Messaging
3 package com.javector.ws;
4
5 public interface OneWayClient {
6
7 // ----------------------------------Public Methods
8
9 /**
10 * Sends a message object to a service that does not return
11 * anything. This method can be used to call asynchronous
12 * message services.
13 *
14 * @param msgObject The message to be sent
15 * @param portName the qualified name of the WSDL port that
16 * contains the operation for receiving the message
17 * @param operationName qualified name of the WSDL operation
18 * on the specified port that accepts the message
19 */
20 public void sendMessage
21 (Object msgObject, String
portName, String operationName)
22 throws Exception;
23
24 }
com/javector/ws/OneWayClient.java
In this section, we have introduced the simple OneWayClient interface which will be used throughout the rest of this book for web services messaging. Although JAX-RPC is complex, because we are focused on messaging, we can simplify our client-side API down to a couple of interfaces. The next section discusses the interface for Request-Response messaging. 5.3 Request-response Messaging Using JAX-RPC (Client Side) As shown in Figure 5-2 steps 1-5 of the Request-Response messaging are the same as in the One-Way scenario. Steps 6-9 trace the response message which follows a slightly different path depending on whether it is a fault or a response. Where a fault message is deserialized according to the JAX-RPC implementation's embedded type mapping, a response message gets deserialized by a special handler. This handler is discussed in detail in Section Error! Reference source not found. The response steps are as follows:
3 package com.javector.ws;
4
5 public interface RequestResponseClient {
6
7 // ----------------------------------Public Methods
8
9 /**
10 * Sends a message object synchronously and returns the
11 * response object that it receives from the remote
12 * service.
13 *
14 * @param requestObject The request to be sent
15 * @param portName the qualified name of the WSDL port that
16 * contains the operation for receiving the message
17 * @param operationName qualified name of the WSDL operation
18 * on the specified port that accepts the message
19 * @return the response received from the remote service
20 */
21 public Object sendRequest
22 (Object requestObj, String portName, String operationName)
23 throws Exception;
24
25 }
com/javector/ws/RequestResponseClient.java
Example 5-2 Interface for Request-Response Messaging About the Article Reader Feedback: Page 1 of 1
SOA World Latest Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||