Sun Java System Message Queue 4.3 Developer's Guide for Java Clients

ProcedureTo Write a SOAP Client for Point-to-Point Messaging

  1. Get an instance of a SOAPConnectionFactory:


    SOAPConnectionFactory myFct = SOAPConnectionFactory.newInstance();
  2. Get a SOAP connection from the SOAPConnectionFactory object:


    SOAPConnection myCon = myFct.createConnection();

    The myCon object that is returned will be used to send the message.

  3. Get a MessageFactory object to create a message:


    MessageFactory myMsgFct = MessageFactory.newInstance();
  4. Use the message factory to create a message:


    SOAPMessage message = myMsgFct.createMessage();

    The message that is created has all the parts that are shown in Figure 5–7.

    Figure 5–7 SOAP Message Parts

    Diagram showing SOAP message with pre-initialized objects:
part, envelope, header, and body.

    At this point, the message has no content. To add content to the message, you need to create a SOAP body element, define a name and content for it, and then add it to the SOAP body.

    Remember that to access any part of the message, you need to traverse the tree, calling a get method on the parent element to obtain the child. For example, to reach the SOAP body, you start by getting the SOAP part and SOAP envelope:


    SOAPPart mySPart = message.getSOAPPart();
    SOAPEnvelope myEnvp = mySPart.getEnvelope();
  5. Now, you can get the body element from the myEnvp object:


    SOAPBody body = myEnvp.getBody();

    The children that you will add to the body element define the content of the message. (You can add content to the SOAP header in the same way.)

  6. When you add an element to a SOAP body (or header), you must first create a name for it by calling the envelope.createName method. This method returns a Name object, which you must then pass as a parameter to the method that creates the body element (or the header element).


    Name bodyName = envelope.createName("GetLastTradePrice", "m",
                                           "http://eztrade.com")
    SOAPBodyElement gltp = body.addBodyElement(bodyName);
  7. Now create another body element to add to the gltp element:


    Name myContent = envelope.createName("symbol");
    SOAPElement mySymbol = gltp.addChildElement(myContent);
  8. And now you can define data for the body element mySymbol:


    mySymbol.addTextNode("SUNW");

    The resulting SOAP message object is equivalent to this XML scheme:


    <SOAP-ENV: Envelope
        xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/">
            <SOAP-ENV:Body>
                <m:GetLastTradePrice xmlns:m="http://eztrade.com">
                    <symbol>SUNW</symbol>
                </m:GetLastTradePrice>
        </SOAP-ENV:Body>
    </SOAP-ENV: Envelope>
  9. Every time you send a message or write to it, the message is automatically saved. However if you change a message you have received or one that you have already sent, this would be the point when you would need to update the message by saving all your changes. For example:


    message.saveChanges();
  10. Before you send the message, you must create a URLEndpoint object with the URL of the endpoint to which the message is to be sent. (If you use a profile that adds addressing information to the message header, you do not need to do this.)


    URLEndpoint endPt = new URLEndpoint("http://eztrade.com//quotes");
  11. Now, you can send the message:


    SOAPMessage reply = myCon.call(message, endPt);

    The reply message (reply) is received on the same connection.

  12. Finally, you need to close the SOAPConnection object when it is no longer needed:


    myCon.close();