Home TOC |
![]() ![]() ![]() |
JAXM
The Java
API for XML Messaging ("JAXM") provides a standard way to send XML documents over the Internet from the Java platform. It is based on the SOAP 1.1 and SOAP with Attachments specifications and can be extended to work with higher level messaging protocols such as ebXML Transport, Routing, and Packaging that are built on top of SOAP.
Typically, a business uses a messaging provider service, which does the behind-the-scenes work required to transport and route messages. When a messaging provider is used, all JAXM messages go through it, so when a business sends a message, the message first goes to the sender's messaging provider, then to the recipient's messaging provider, and finally to the intended recipient. It is also possible to route a message to go to intermediate recipients before it goes to the ultimate destination.
Because messages go through it, a messaging provider can take care of housekeeping details like assigning message identifiers, storing messages, and keeping track of whether a message has been delivered before. A messaging provider can also try resending a message that did not reach its destination on the first attempt at delivery. The beauty of a messaging provider is that the client using JAXM technology ("JAXM client") is totally unaware of what the provider is doing in the background. The JAXM client simply makes Java method calls, and the messaging provider in conjunction with the messaging infrastructure makes everything happen behind the scenes.
Though in the typical scenario a business uses a messaging provider, it is also possible to do JAXM messaging without using a messaging provider. In this case, the JAXM client (called a standalone client) is limited to sending point-to-point messages directly to a Web service that is implemented for request-response messaging. Request-response messaging is synchronous, meaning that a request is sent and its response is received in the same operation. A request-response message is sent over a
SOAPConnection
object via the methodSOAPConnection.call
, which sends the message and blocks until it receives a response. A standalone client can operate only in a client role, that is, it can only send requests and receive their responses. In contrast, a JAXM client that uses a messaging provider may act in either the client or server (service) role. In the client role, it can send requests; in the server role, it can receive requests, process them, and send responses.Though it is not required, JAXM messaging usually takes place within a container, generally a servlet or a J2EE
container. A Web service that uses a messaging provider and is deployed in a container has the capability of doing one-way messaging, meaning that it can receive a request as a one-way message and can return a response some time later as another one-way message.
Because of the features that a messaging provider can supply, JAXM can sometimes be a better choice for SOAP messaging than JAX-RPC. The following list includes features that JAXM can provide and that RPC, including JAX-RPC, does not generally provide:
- One-way (asynchronous) messaging
- Routing of a message to more than one party
- Reliable messaging with features such as guaranteed delivery
A JAXM message is made up of two parts, a required SOAP part and an optional attachment part. The SOAP part, which consists of a
SOAPEnvelope
object containing aSOAPHeader
object and aSOAPBody
object. TheSOAPBody
object can hold XML fragments as the content of the message being sent. If you want to send content that is not in XML format or that is an entire XML document, your message will need to contain an attachment part in addition to the SOAP part. There is no limitation on the content in the attachment part, so it can include images or any other kind of content, including XML fragments and documents.Getting a Connection
The first thing a JAXM client needs to do is get a connection, either a
SOAPConnection
object or aProviderConnection
object.Getting a Point-to-Point Connection
A standalone client is limited to using a
SOAPConnection
object, which is a point-to-point connection that goes directly from the sender to the recipient. All JAXM connections are created by a connection factory. In the case of aSOAPConnection
object, the factory is aSOAPConnectionFactory
object. A client obtains the default implementation forSOAPConnectionFactory
by calling the following line of code.SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();The client can use factory to create a
SOAPConnection
object.SOAPConnection con = factory.createConnection();Getting a Connection to the Messaging Provider
In order to use a messaging provider, an application must obtain a
ProviderConnection
object, which is a connection to the messaging provider rather than to a specified recipient. There are two ways to get aProviderConnection
object, the first being similar to the way a standalone client gets aSOAPConnection
object. This way involves obtaining an instance of the default implementation forProviderConnectionFactory
, which is then used to create the connection.ProviderConnectionFactory pcFactory = ProviderConnectionFactory.newInstance(); ProviderConnection pcCon = pcFactory.createConnection();The variable
pcCon
represents a connection to the default implementation of a JAXM messaging provider.The second way to create a
ProviderConnection
object is to retrieve aProviderConnectionFactory
object that is implemented to create connections to a specific messaging provider. The following code demonstrates getting such aProviderConnectionFactory
object and using it to create a connection. The first two lines use the JNDI API to retrieve the appropriateProviderConnectionFactory
object from the naming service where it has been registered with the name "CoffeeBreakProvider". When this logical name is passed as an argument, the methodlookup
returns theProviderConnectionFactory
object to which the logical name was bound. The value returned is a JavaObject
, which must be narrowed to aProviderConnectionFactory
object so that it can be used to create a connection. The third line uses a JAXM method to actually get the connection.Context ctx = getInitialContext(); ProviderConnectionFactory pcFactory = (ProviderConnectionFactory)ctx.lookup("CoffeeBreakProvider"); ProviderConnection con = pcFactory.createConnection();The
ProviderConnection
instance con represents a connection to The Coffee Break's messaging provider.Creating a Message
As is true with connections, messages are created by a factory. And similar to the case with connection factories,
MessageFactory
objects can be obtained in two ways. The first way is to get an instance of the default implementation for theMessageFactory
class. This instance can then be used to create a basicSOAPMessage
object.MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage m = messageFactory.createMessage();All of the
SOAPMessage
objects thatmessageFactory
creates, including m in the previous line of code, will be basic SOAP messages. This means that they will have no pre-defined headers.Part of the flexibility of the JAXM API is that it allows a specific usage of a SOAP header. For example, protocols such as ebXML can be built on top of SOAP messaging. This usage of SOAP by a given standards group or industry is called a profile. In the second way to create a
MessageFactory
object, you use theProviderConnection
methodcreateMessageFactory
and give it a profile. TheSOAPMessage
objects produced by the resultingMessageFactory
object will support the specified profile. For example, in the following code fragment, in whichschemaURI
is the URI of the schema for the desired profile, m2 will support the messaging profile that is supplied tocreateMessageFactory
.MessageFactory messageFactory2 = con.createMessageFactory(<schemaURI>); SOAPMessage m2 = messageFactory2.createMessage();Each of the new
SOAPMessage
objects m and m2 automatically contains the required elementsSOAPPart
,SOAPEnvelope
, andSOAPBody
, plus the optional elementSOAPHeader
(which is included for convenience). TheSOAPHeader
andSOAPBody
objects are initially empty, and the following sections will illustrate some of the typical ways to add content.Populating a Message
Content can be added to the
SOAPPart
object, to one or moreAttachmentPart
objects, or to both parts of a message.Populating the SOAP Part of a Message
As stated earlier, all messages have a
SOAPPart
object, which has aSOAPEnvelope
object containing aSOAPHeader
object and aSOAPBody
object. One way to add content to the SOAP part of a message is to create aSOAPHeaderElement
object or aSOAPBodyElement
object and add an XML document that you build with the methodSOAPElement.addTextNode
. The first three lines of the following code fragment access theSOAPBody
object body, which is used to create a newSOAPBodyElement
object and add it to body. The argument passed to thecreateName
method is aName
object identifying theSOAPBodyElement
being added. The last line adds the XML string passed to the methodaddTextNode
.SOAPPart sp = m.getSOAPPart(); SOAPEnvelope envelope = sp.getSOAPEnvelope(); SOAPBody body = envelope.getSOAPBody(); SOAPBodyElement bodyElement = body.addBodyElement( envelope.createName("text", "hotitems", "http://hotitems.com/products/gizmo"); bodyElement.addTextNode("some-xml-text");Another way is to add content to the
SOAPPart
object by passing it ajavax.xml.transform.Source
object, which may be aSAXSource
,DOMSource
, orStreamSource
object. TheSource
object contains content for the SOAP part of the message and also the information needed for it to act as source input. AStreamSource
object will contain the content as an XML document; theSAXSource
orDOMSource
object will contain content and instructions for transforming it into an XML document.The following code fragments illustrates adding content as a
DOMSource
object. The first step is to get theSOAPPart
object from theSOAPMessage
object. Next the code uses methods from the JavaAPI for XML Processing ("JAXP") to build the XML document to be added. It uses a
DocumentBuilderFactory
object to get aDocumentBuilder
object. Then it parses the given file to produce the document that will be used to initialize a newDOMSource
object. Finally, the code passes theDOMSource
object domSource to the methodSOAPPart.setContent
.SOAPPart soapPart = message.getSOAPPart(); DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("file:///foo.bar/soap.xml"); DOMSource domSource = new DOMSource(doc); soapPart.setContent(domSource);Populating the Attachment Part of a Message
A
Message
object may have no attachment parts, but if it is to contain anything that is not in XML format, that content must be contained in an attachment part. There may be any number of attachment parts, and they may contain anything from plain text to image files. In the following code fragment, the content is an image in a JPEG file, whose URL is used to initialize thejavax.activation.DataHandler
objectdh
. TheMessage
object m creates theAttachmentPart
objectattachPart
, which is initialized with the data handler containing the URL for the image. Finally, the message addsattachPart
to itself.URL url = new URL("http://foo.bar/img.jpg"); DataHandler dh = new DataHandler(url); AttachmentPart attachPart = m.createAttachmentPart(dh); m.addAttachmentPart(attachPart);A
SOAPMessage
object can also give content to anAttachmentPart
object by passing anObject
and its content type to the methodcreateAttachmentPart
.AttachmentPart attachPart = m.createAttachmentPart("content-string", "text/plain"); m.addAttachmentPart(attachPart);A third alternative is to create an empty
AttachmentPart
object and then to pass theAttachmentPart.setContent
method anObject
and its content type. In this code fragment, theObject
is aByteArrayInputStream
initialized with a jpeg image.AttachmentPart ap = m.createAttachmentPart(); byte[] jpegData = ...; ap.setContent(new ByteArrayInputStream(jpegData), "image/jpeg"); m.addAttachmentPart(ap);Sending a Message
Once you have populated a
SOAPMessage
object, you are ready to send it. A standalone client uses theSOAPConnection
methodcall
to send a message. This method sends the message and then blocks until it gets back a response. The arguments to the methodcall
are the message being sent and anEndpoint
object that contains the URL of the receiver.SOAPMessage response = soapConnection.call(message, urlEndpoint);An application that is using a messaging provider uses the
ProviderConnection
methodsend
to send a message. This method sends the message asynchronously, meaning that it sends the message and returns immediately. The response, if any, will be sent as a separate operation at a later time. Note that this method takes only one parameter, the message being sent. The messaging provider will use header information to determine the destination.providerConnection.send(message);
Home TOC |
![]() ![]() ![]() |