Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Publishing and subscribing with a TextMessage
 

Publishing and subscribing with a TextMessage

The procedures example1.p and example2.p demonstrate basic Pub/Sub messaging. The procedure example1.p publishes a TextMessage to a topic, and the procedure example2.p subscribes to a topic and receives a TextMessage.
To run example1.p and example2.p:
1. Run example2.p so the subscriber is running before you publish, as shown:
example2.p
/* Subscribes and receives a Text message. */
DEFINE VARIABLE hPubSubSession AS HANDLE NO-UNDO.
DEFINE VARIABLE hConsumer      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 GolfTopic topic. Messages are handled by the
   "golfHandler" internal procedure. */
RUN createMessageConsumer IN hPubSubSession
  (THIS-PROCEDURE,  /* this procedure will handle it */
   "golfHandler",   /* name of internal procedure */
   OUTPUT hConsumer).
RUN subscribe IN hPubSubSession
  ("GolfTopic",     /* name of topic */
   ?,               /* subscription is not durable */
   ?,               /* no message selector */
   FALSE,           /* want my own messages too */
   hConsumer).      /* handles the incoming 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 golfHandler:
  DEFINE INPUT PARAMETER hMessage     AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER msghConsumer AS HANDLE NO-UNDO.
  DEFINE OUTPUT PARAMETER hReply      AS HANDLE NO-UNDO.
  /* Display the message - we assume that reply is not required. */
  DISPLAY "Message text: "
    DYNAMIC-FUNCTION('getText':U IN hMessage) FORMAT "x(70)".
  RUN deleteMessage IN hMessage.
  APPLY "U1" TO THIS-PROCEDURE.
END PROCEDURE.
2. Run example1.p to publish the TextMessage to a topic, as shown:
example1.p
/* Publishes a text 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 text message */
RUN createTextMessage IN hPubSubSession (OUTPUT hMessage).
RUN setText IN hMessage ("Golf shoes on sale today").
/* Publish the message on the "GolfTopic" topic */
RUN publish IN hPubSubSession ("GolfTopic", hMessage, ?, ?, ?).
RUN deleteMessage IN hMessage.
RUN deleteSession IN hPubSubSession.