bea.com | products | dev2dev | support | askBEA

Download Docs | Site Map | Glossary
Search

Programming Messaging Applications

Receiving XOCP Business Messages

The following sections describe how to receive XOCP business messages in an XOCP application:

- How XOCP Business Messages Are Received

- Receiving an XOCP Business Message

How XOCP Business Messages Are Received

XOCP applications must implement the onMessage method in the MessageListener interface to receive and process business messages. The signature for the onMessage method is as follows.

Listing 4-1 Signature for onMessage Method

>     public void onMessage(XOCPMessage msg)

Whenever a business message arrives, the XOCP application invokes the onMessage method, passing the business message as an input parameter. The XOCP application retrieves the XOCPMessage object containing the business message and then calls methods on that instance to process the message.

If an XOCP application receives multiple business documents in a conversation, the onMessage implementation first determines the type of document received (such as a bid request or bid reward), and then processes the document accordingly.

In addition, the onMessage implementation might contain code that constructs and sends a business message. For example, a conversation participant XOCP application might implement onMessage to receive a request, process the request, and then create and send a reply document.

Receiving an XOCP Business Message

Listing 4-2 shows how the onMessage method is implemented in the MdmTp2Servlet of the Messaging API sample application. This onMessage implementation processes the initial business document (a request) sent from the MsmTp1Servlet. It then creates and sends a reply document back to the conversation initiator.

The following listing is the onMessage implementation in the MdmTp2Servlet of the Messaging API sample application.

Listing 4-2 onMessage Implementation in MdmTp2Servlet

>     public void onMessage(XOCPMessage rmsg) {
>           try{

>             QualityOfService qos = rmsg.getQoS();

>             PayloadPart[] payload = rmsg.getPayloadParts();
>             Document rq = null;

>             // we are using a single part document
>             if (payload != null && payload.length > 0){
>               BusinessDocument bd = (BusinessDocument)payload[0];
>               rq = bd.getDocument();
>             }
>             if (rq == null){
>               throw new Exception("Did not get a request document");
>             }
>             Conversation conv = rmsg.getConversation();

>             Element root = rq.getDocumentElement();
>             String name = root.getNodeName();
>             Text revStr = (Text)root.getFirstChild();

>             // create the return document
>             DOMImplementationImpl domi = new DOMImplementationImpl();
>             DocumentType dType = domi.createDocumentType("reply", null, "reply.dtd");
>             rq = new DocumentImpl(dType);
>             root = rq.createElement("reply");
>             String sendStr = new String(revStr.getData());
>             sendStr="Partner2 -- " + sendStr;
>             root.appendChild(rq.createTextNode(sendStr.toLowerCase()));
>             rq.appendChild(root);

>             XOCPMessage smsg = new XOCPMessage("");
>             smsg.addPayloadPart(new BusinessDocument(rq));

>             smsg.setQoS(qos);

>             //TradingPartnerFilter filter = new TradingPartnerFilter("Partner1");
>             smsg.setExpression("//trading-partner[@name=\'Partner1\']");
>             smsg.setCAId(rmsg.getCAId());
>             smsg.setConversation(conv);

>             smsg.sendAndWait(0);

>           }catch(Exception e){
>             e.printStackTrace();
>           }
>         }

The onMessage code performs the following tasks:

1. Retrieves the Quality of Service setting for the business message by calling the getQoS method on the XOCPMessage instance.

   The application uses the same Quality of Service settings used in the original message to send the reply message.

2. Retrieves the payload parts of the business message by calling the getPayloadParts method on the XOCPMessage instance.

3. Retrieves the first (and only) business document in the PayloadPart[] array.

4. Extracts the associated XML document by calling the getDocument method on the BusinessDocument instance.

5. Retrieves and examines parts of the XML document by using methods on the Document instance. The latter is an instance of the org.w3c.dom.Document class in the org.w3c.dom package published by the World Wide Web Consortium (www.w3.org).

   An XOCP application can also use a third-party implementation of that package, such as the org.apache.xerces.dom package provided by The Apache XML Project (www.apache.org). This package is used by the Messaging API sample application to create and process business documents.

6. Retrieves the data string ("ABCDEFGHI") embedded in the business document and converts it to all lowercase letters.

7. Constructs a reply document and specifies the same Quality of Service setting specified for the request document.

8. Sets the collaboration agreement ID and conversation, and sends the document to the trading partner called Partner 1.