Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Publishing, subscribing, and receiving the customer table in a StreamMessage
 

Publishing, subscribing, and receiving the customer table in a StreamMessage

The procedures example14.p and example1.5p use RAW transfer to publish, subscribe, and receive the customer table in a StreamMessage. The procedure example14.p publishes the customer table in a StreamMessage; each customer record is a bytes item. The procedure example15.p subscribes and receives the customer table in a StreamMessage; each customer record is a bytes item.
To run Examples 14 and 15:
1. Start a server for the Sports database. Each client must connect to the database in multi-user mode.
2. Run example15.p so the subscriber is running before you publish, as shown:
example15.p
/* Receives the customer table in a Stream message. */
DEFINE VARIABLE hPubSubSession AS HANDLE NO-UNDO.
DEFINE VARIABLE msgConsumer1   AS HANDLE NO-UNDO.
DEFINE TEMP-TABLE ttCustomer NO-UNDO LIKE customer.
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
  ("topic1",
   ?,         /* not a durable subscription */
   ?,         /* no message selector */
   FALSE,     /* no local events */
   msgConsumer1).
RUN startReceiveMessages IN hPubSubSession.
WAIT-FOR u1 OF THIS-PROCEDURE.
FOR EACH ttCustomer:
  DISPLAY ttCustomer WITH 2 COLUMN.
END.
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 rawCust AS RAW NO-UNDO.
  DO WHILE NOT DYNAMIC-FUNCTION('endOfStream' IN hMessage):
    DYNAMIC-FUNCTION('moveToNext':U IN hMessage).
    rawCust = DYNAMIC-FUNCTION('readBytesToRaw':U IN hMessage).
    RAW-TRANSFER rawCust TO ttCustomer.
    RELEASE ttCustomer.
  END.
  RUN deleteMessage IN hMessage.
  APPLY "U1" TO THIS-PROCEDURE.
END PROCEDURE.
3. Run example14.p, as shown:
example14.p
/* Publishes the customer table in a Stream message. */
DEFINE VARIABLE hMesg          AS HANDLE NO-UNDO.
DEFINE VARIABLE hPubSubSession AS HANDLE NO-UNDO.
DEFINE VARIABLE rawCust        AS RAW    NO-UNDO.
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN beginSession IN hPubSubSession.
RUN createStreamMessage IN hPubSubSession (OUTPUT hMesg).
FOR EACH customer NO-LOCK:
  RAW-TRANSFER customer TO rawCust.
  RUN writeBytesFromRaw IN hMesg(rawCust).
END.
RUN publish IN hPubSubSession ("topic1", hMesg, ?, ?, ?).
RUN deleteMessage IN hMesg.
RUN deleteSession IN hPubSubSession.