ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Hangin' with the JAX Pack, Part 2: JAXM
Pages: 1, 2, 3, 4

Producing Messages

Building a message consumer is a much more straightforward process than building a message producer. However, the concepts as a whole are simple. Sending a SOAP 1.1 message with JAXM is a three-step process:



  1. Create a message.
  2. Populate the message with appropriate content.
  3. Send the message.

There are a number of ways to achieve these three steps using either direct connections or a messaging provider. Let's look more closely at how to develop message producers.

Create a Connection

The first step in developing a messaging producer is to create a connection. When developing applications to produce SOAP 1.1 messages, we would normally build a message and send it over some connection. JAXM, however, defines the concept of a messaging provider. Those readers familar with JMS will be quite at home with messaging providers. A messaging provider is a sort of intermediary that collects, caches, and ultimately sends messages on behalf of an application. We can create one connection in one of two ways; using a messaging provider or not.

Creating a direct connection, without a messaging producer, is done using a SOAPConnection object that has a static newInstance() method for creating connection instances. SOAP Connections are created as shown below:

java.xml.soap.SOAPConnection connection = SOAPConnection.newInstance();

Creating a connection using a messaging provider is a somewhat more difficult process. Like JMS connections, using a messaging provider requires you request the connection from the messaging provider:

private static final String providerURI = "http://java.sun.com/xml/jaxm/provider";
Context ctx = new InitialContext();
ProviderConnectionFactory cf = (ProviderConnectionFactory)ctx.lookup( providerURI);
SOAPConnection connection = connectionFactory.createConnection();

The code above assumes that the connection factory was registered in JNDI (again, much like JMS) and fully implements the ProviderConnectionFactory interface.

Obtain a Message From a Message Factory

Once we have a connection, we need a message to send. Again, much like JMS, we use a factory object to create the message.

Using a direct connection, we can use a simple MessageFactory object to generate an instance of a SOAPMessage as shown below:

MessageFactory mf = MessageFactory.newInstance();
SOAPMessage mg = mf.createMessage();

If we were using a messaging provider we would need to obtain a MessageFactory from the connection, as the messages might use common profiles and are bound to the provider behind the scenes. A code snippet for obtaining messages from a messaging provider-based connection is shown below.

MessageFactory mf = connection.createMessageFactory(); // Create messgages from the message factory.
SOAPMessage message = mf.createMessage( );

Populating a Message

Once we have a message, we populate it the same way for both connection types. For example:

//Get the SOAP part of the message. We could have gotten the attachment part as well.
SOAPPart sp = message.getSOAPPart( );

// Get the soap envelope
SOAPEnvelope se = sp.getSOAPEnvelope( );

// populate the soap envelope using a DOMSource object
DOMSource domsrc = null;
try {
  DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance( );
DocumentBuilder db = dbf.newDocumentBuilder( ;
Document doc = db.parse("file:///somefiles.xml" );
  domsrc = new DOMSource(doc);
} catch(Exception e ) {
System.err.println("Error in creating DOMSource" +
  e.getMessage( );
}
// add the DOMSource object to the soap envelop
se.setContent(domsrc);

Obviously, you will need to adhere to the SOAP rules, perhaps adding SOAP Header elements with appropriate content. See the SOAP specification for complete details on how to comply with the SOAP specification.

Pages: 1, 2, 3, 4

Next Pagearrow