Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Publishing, subscribing, and receiving an XML document in a BytesMessage
 

Publishing, subscribing, and receiving an XML document in a BytesMessage

The procedures example12.p and example13.p use a MEMPTR variable to publish and receive an XML document in a BytesMessage to prevent code-page conversions. The code pages of the document and the OpenEdge client do not have to match.
To run example12.p and example13.p:
1. Run example13.p to subscribe and receive a BytesMessage containing an XML document, as shown:
example13.p
/* Receives an XML document in a Bytes message. */
DEFINE VARIABLE hPubSubSession AS HANDLE  NO-UNDO.
DEFINE VARIABLE msgConsumer1   AS HANDLE  NO-UNDO.
DEFINE VARIABLE stillWaiting   AS LOGICAL NO-UNDO INITIAL TRUE.
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN beginSession IN hPubSubSession.
RUN createMessageConsumer IN hPubSubSession
  (THIS-PROCEDURE, "messageHandler", OUTPUT msgConsumer1).
RUN subscribe IN hPubSubSession
  ("xmlTopic",
   ?,          /* not a durable subscription */
   ?,          /* no message selector. */
   FALSE,      /* no local events */
   msgConsumer1) NO-ERROR.
RUN startReceiveMessages IN hPubSubSession.
RUN waitForMessages IN hPubSubSession ("inWait", THIS-PROCEDURE, ?).
RUN deleteSession IN hPubSubSession.
PROCEDURE messageHandler:
  DEFINE INPUT PARAMETER hMessage         AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER hMessageConsumer AS HANDLE NO-UNDO.
  DEFINE OUTPUT PARAMETER hReply          AS HANDLE NO-UNDO.
  DEFINE VARIABLE mDoc  AS MEMPTR NO-UNDO.
  DEFINE VARIABLE hDoc  AS HANDLE NO-UNDO.
  DEFINE VARIABLE hRoot AS HANDLE NO-UNDO.
  mDoc = DYNAMIC-FUNCTION('getMemptr':U IN hMessage).
  CREATE X-DOCUMENT hDoc.
  CREATE X-NODEREF hRoot.
  hDoc:LOAD("memptr", mDoc, FALSE).
  hDoc:GET-DOCUMENT-ELEMENT(hRoot).
  RUN GetChildren(hRoot, 1).
  RUN deleteMessage IN hMessage.
  stillWaiting = FALSE.
END PROCEDURE.
PROCEDURE GetChildren:
  DEFINE INPUT PARAMETER hParent AS HANDLE  NO-UNDO.
  DEFINE INPUT PARAMETER iLevel  AS INTEGER NO-UNDO.
  DEFINE VARIABLE hNodeRef AS HANDLE  NO-UNDO.
  DEFINE VARIABLE ix       AS INTEGER NO-UNDO.
  CREATE X-NODEREF hNodeRef.
  REPEAT ix = 1 TO hParent:NUM-CHILDREN.
    hParent:GET-CHILD(hNodeRef, ix).
    IF hNodeRef:NAME = "#text" THEN
      MESSAGE "Node text: " hNodeRef:NODE-VALUE.
    ELSE
      MESSAGE "Node name: " hNodeRef:NAME.
    RUN GetChildren(hNodeRef, (iLevel + 1)).
  END.
  DELETE OBJECT hNodeRef.
END PROCEDURE.
FUNCTION inWait RETURNS LOGICAL:
  RETURN stillWaiting.
END.
2. Run example12.p to publish the BytesMessage containing an XML document, as shown:
example12.p
/* Publishes an XML document in a Bytes message. */
DEFINE VARIABLE hDoc           AS HANDLE NO-UNDO.
DEFINE VARIABLE hPubSubSession AS HANDLE NO-UNDO.
DEFINE VARIABLE hMesg          AS HANDLE NO-UNDO.
DEFINE VARIABLE mValue         AS MEMPTR NO-UNDO.
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN beginSession IN hPubSubSession.
RUN createBytesMessage IN hPubSubSession (OUTPUT hMesg).
CREATE X-DOCUMENT hDoc.
hDoc:LOAD("file", "personal.xml", FALSE).
hDoc:SAVE("memptr", mValue).
RUN setMemptr IN hMesg(mValue, ?, ?).
RUN publish IN hPubSubSession ("xmlTopic", hMesg, ?, ?, ?).
SET-SIZE(mValue) = 0.
RUN deleteMessage IN hMesg.
RUN deleteSession IN hPubSubSession.
/* The personal.xml document:
<?xml version="1.0" encoding='UTF-8' ?>
<personnel>
  <person id="Irving.Nigrini">
    <name><family>Nigrini</family> <given>Irving</given></name>
    <email>inigrini@subpargolf.com</email>
    <link manager="Thomas.Roy"/>
  </person>
  <person id="Jules.Nigrini">
    <name><family>Nigrini</family> <given>Jules</given></name>
    <email>jnigrini@subpargolf.com</email>
    <link manager="Thomas.Roy"/>
  </person>
</personnel>*/
* XML code page encoding
* Code page example 1
* Code page example 2