Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Publishing, receiving, and processing a StreamMessage
 

Publishing, receiving, and processing a StreamMessage

The procedures example8.p and example9.p publish, receive, and process a StreamMessage.
To run example8.p and example9.p:
1. Connect to the Sports database.
2. Run example9p to receive the StreamMessage containing the customer names and numbers, as shown:
example9.p
/* Receives a Stream message. */
DEFINE VARIABLE hConsumer      AS HANDLE NO-UNDO.
DEFINE VARIABLE hPubSubSession AS HANDLE NO-UNDO.
/* Creates a session object. */
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN beginSession IN hPubSubSession.
/* Subscribe to the newCustomers topic. The newCustHandler internal
   procedure handles the list of new customers.
*/
RUN createMessageConsumer IN hPubSubSession
  (THIS-PROCEDURE,    /* this procedure will handle it */
   "newCustHandler",  /* name of internal procedure */
   OUTPUT hConsumer).
RUN subscribe IN hPubSubSession
  ("NewCustomers",    /* name of topic */
   ?,                 /* subscription is not durable*/
   ?,                 /* no message selector. */
   FALSE,             /* want my own messages too */
   hConsumer).        /* handles the messages */
/* Start receiving messages */
RUN startReceiveMessages IN hPubSubSession.
/* Wait to receive the messages. Any other I/O-blocked statements can be
   used for receiving messages. */
WAIT-FOR u1 OF THIS-PROCEDURE.
PROCEDURE newCustHandler:
  DEFINE INPUT PARAMETER hMessage     AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER hMsgConsumer AS HANDLE NO-UNDO.
  DEFINE OUTPUT PARAMETER hReply      AS HANDLE NO-UNDO.
  /* Display the stream of customer names and customer numbers. The
     moveToNext function moves the cursor to the next item in the stream
     and returns the data type of that item. We assume the reply is not
     required. */
  IF NOT DYNAMIC-FUNCTION('getMessageType':U IN hMessage) =
    "StreamMessage" THEN RETURN.
  /* Note that the 'moveToNext' functions returns the item's data type. */
  DO WHILE NOT DYNAMIC-FUNCTION('endOfStream':U IN hMessage) WITH DOWN:
    DISPLAY DYNAMIC-FUNCTION('moveToNext':U IN hMessage)
            DYNAMIC-FUNCTION('readChar':U IN hMessage)
            DYNAMIC-FUNCTION('moveToNext':U IN hMessage)
            DYNAMIC-FUNCTION('readInt':U IN hMessage).
    DOWN.
  END.
  RUN deleteMessage IN hMessage.
  APPLY "U1" TO THIS-PROCEDURE.
END PROCEDURE.
3. Run example8.p to publish a StreamMessage containing customer names and numbers, as shown:
Table 40. example8.p
/* Publishing a Stream message. */
DEFINE VARIABLE hMessage       AS HANDLE NO-UNDO.
DEFINE VARIABLE hPubSubSession AS HANDLE NO-UNDO.
/* Creates a session object. */
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN beginSession IN hPubSubSession.
/* Create a stream message */
RUN createStreamMessage IN hPubSubSession (OUTPUT hMessage).
/* Load the message with a list of customer names and custnums. */
FOR EACH customer NO-LOCK:
   RUN writeString IN hMessage (customer.name).
   RUN writeInt IN hMessage (customer.custnum).
END.
/* Publish the message on the NewCustomers topic. */
RUN publish IN hPubSubSession ("NewCustomers", hMessage, ?, ?, ?).