Integrating SOAP and Message Queue

This section explains how you can send, receive, and process a JMS message
that contains a SOAP payload.

Message Queue provides a utility to help you send and receive SOAP messages
using the JMS API. With the support it provides, you can convert a SOAP message
into a JMS message and take advantage of the reliable messaging service offered
by Message Queue. You can then convert the message back into a SOAP message
on the receiving side and use SAAJ to process it.

To send, receive, and process a JMS message that contains a SOAP payload,
you must do the following:

Import the library com.sun.messaging.xml.MessageTransformer . This is the utility whose methods you will use to convert SOAP
messages to JMS messages and vice versa.

Before you transport a SOAP message, you must call the MessageTransformer.SOAPMessageIntoJMSMessage method. This method transforms the SOAP message into a JMS message.
You then send the resulting JMS message as you would a normal JMS message.
For programming simplicity, it would be best to select a destination that
is dedicated to receiving SOAP messages. That is, you should create a particular
queue or topic as a destination for your SOAP message and then send only SOAP
messages to this destination.

The Session argument specifies the session to be
used in producing the Message.

On the receiving side, you get the JMS message containing
the SOAP payload as you would a normal JMS message. You then call the MessageTransformer.SOAPMessageFromJMSMessage utility to extract the SOAP message, and then use SAAJ to disassemble
the SOAP message and do any further processing. For example, to obtain the SOAPMessage make a call like the following:

The MessageFactory argument specifies a message factory
that the utility should use to construct the SOAPMessage from
the given JMS Message.

The following sections offer several use cases and code examples to
illustrate this process.

Example 1: Deferring SOAP Processing

In the first example, illustrated in Figure 5–9, an incoming SOAP message is received by a servlet. After
receiving the SOAP message, the servlet MyServlet uses
the MessageTransformer utility to transform the message
into a JMS message, and (reliably) forwards it to an application that receives
it, turns it back into a SOAP message, and processes the contents of the SOAP
message.

Instantiate a Message Queue Destination administered object corresponding
to a physical destination in the Message Queue message service. In this example,
the administered object is mySOAPQueue and the physical
destination to which it refers is myPSOAPQ.

Queue mySOAPQueue = new com.sun.messaging.Queue("myPSOAPQ");

Use the MessageTransformer utility, as shown,
to transform the SOAP message into a JMS message. For example, given a SOAP
message named MySOAPMsg,

Example 2: Publishing SOAP Messages

In the next example, illustrated in Figure 5–10, an incoming SOAP message is received by a servlet. The
servlet packages the SOAP message as a JMS message and (reliably) forwards
it to a topic. Each application that subscribes to this topic, receives the
JMS message, turns it back into a SOAP message, and processes its contents.

Figure 5–10 Publishing a SOAP Message

The code that accomplishes this is exactly the same as in the previous
example, except that instead of sending the JMS message to a queue, you send
it to a topic. For an example of publishing a SOAP message using Message Queue,
see Example 5–5.

Code Samples

This section includes and describes two code samples: one that sends
a JMS message with a SOAP payload, and another that receives the JMS/SOAP
message and processes the SOAP message.

Example 5–5 illustrates the use
of the JMS API, the SAAJ API, and the JAF API to send a SOAP message with
attachments as the payload to a JMS message. The code shown for the SendSOAPMessageWithJMS includes the following methods:

A constructor that calls the init method
to initialize all the JMS objects required to publish a message

A send method that creates the SOAP message
and an attachment, converts the SOAP message into a JMS message, and publishes
the JMS message

Example 5–6 illustrates the use
of the JMS API, SAAJ, and the DOM API to receive a SOAP message with attachments
as the payload to a JMS message. The code shown for the ReceiveSOAPMessageWithJMS includes the following methods:

A constructor that calls the init method
to initialize all the JMS objects needed to receive a message.

An onMessage method that delivers the message
and which is called by the listener. The onMessage method
also calls the message transformer utility to convert the JMS message into
a SOAP message and then uses SAAJ to process the SOAP body and uses SAAJ and
the DOM API to process the message attachments.