![]() |
![]() |
|
|
Receiving XOCP Business Messages
The following sections describe how to receive XOCP business messages in an XOCP application:
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:
The application uses the same Quality of Service settings used in the original message to send the reply message.
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.
![]() |
![]() |
|
Copyright © 2001 BEA Systems, Inc. All rights reserved.
|