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


AddThis Social Bookmark Button

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

Sending a Message

We then send a message and close the connection as shown below:

private static final String endpointURL ="http://somewebservice:7001";
URLEndPoiunt ep = new URLEndPoint(endpointURL);
SOAPMessage reply = connection.send (message,ep);

Alternatively, if we were sending many messages to the same end point, we might set the default message end point as follows:

private static final String endpointURL ="http://somewebservice:7001";
message.setTo(new URLEndPoint(endpointURL);

We could then update the message content as required, keep sending the same message, and later close the connection as required.

A complete message provider example is shown below. Note that this example is for illustration purposes and may not conform to the SOAP specification.

Listing 2: SOAPProducer.java

import javax.xml.messaging.*;
import javax.xml.soap.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.parsers.*;
import javax.servlet.http.*;
import java.io.IOException;
import org.w3c.dom.Document;

public class SOAPServlet extends HttpServlet {
  private static final String providerURI =

  private ProviderConnectionFactory connectionFactory;

  public void init(ServletConfig servletConfig) throws ServletException {
     Context ctx = new InitialContext();
     connectionFactory = (ProviderConnectionFactory)ctx.lookup( providerURI);

  public void doGet(HttpServletRequest request,HttpServletResponse response )
    throws ServletException, IOException
    try {
      // A request comes in to send a message. So we create a connection from the factory.
      // factories are used when we have an intermediate which caches and send messages on our behalf
      // much like JMS.
      SOAPConnection connection = connectionFactory.createConnection();

      // Create a message factory from the Connection to produce messages.
      MessageFactory mf = connection.createMessageFactory();
      // Create messgages from the message factory.
      Message message = mf.createMessage( );

      // Get the SOAPPart from the message.
      // Note the infrastructure takes care of creating the SOAPPart.
      // The user needs to get the SOAPPart and get the SOAPEnvelope and populate it appropriately.
      SOAPPart soapPart = message.getSOAPPart( );
      // Get the SOAPEnvelope from the header container obtained above.
      SOAPEnvelope soapEnvelope = soapPart.getSOAPEnvelope( );

      // Create a SOAPHeader from the SOAPEnvelope
      SOAPHeader soapHeader = soapEnvelope.createSOAPHeader() ;

      // Create a SOAPHeaderElement that will be appended to the SOAPHeader.
      SOAPHeaderElement she = soapHeader.createSOAPHeaderElement( );
      she.setName("from", "http://foo.bar/", "m" );
      she.addContent("foo@bar.com" ;

      // Add a DOMSource object as the actual message content

      DOMSource domsrc = null;
      try {
        DocumentBuilderFactory dbf =
        DocumentBuilderFactory.newInstance( );
        DocumentBuilder db = dbf.newDocumentBuilder( ;
        Document doc = db.parse("file:///foo.bar/soap.xml" );
        domsrc = new DOMSource(doc);
      } catch(Exception e ) {
        System.err.println("Error in creating DOMSource" +
        e.getMessage( );


      soapEnvelope.setSOAPBody(soapBody );
      URLEndpoint ep = new URLEndpoint("http://foo.bar/Service" );
      connection.send(message, endPoint );
      response.setStatus(HttpServletResponse.SC_OK );

    } catch(Exception jxe {
  } // end doGet


The JAX Pack APIs add an exciting element to Web services development in a J2EE environment. In this article, we saw how to process XML messages using JAXM -- an admittedly low-level API, but all in all, a very powerful set of tools for writing robust Web services. In part three of this series we will look at the remaining JAXPack APIs -- JAXR and JAX-RPC.

Al Saganich is BEA Systems' senior developer and engineer for enterprise Java technologies, focused on Java integration and application with XML and Web services.

Return to ONJava.com.