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

Publishing with message properties and subscribing selectively

The procedure example3.p publishes a TextMessage from Super Golf Center to Sub Par Golf using the setStringPropertyprocedure. The procedure example4.p subscribes to a topic and only receives messages addressed to Sub Par Golf (by passing a selector to the subscribeprocedure call).
To run example3.p and example4.p:
1. Run example4p first so the subscriber is running before you publish, as shown:
example4.p
/* Receives a text message with "TO" property equal to "Sub Par Golf" */
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.
/* Subscribes 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 */
   "TO = 'Sub Par Golf'", /* only messages from Sub Par Golf */
   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 hMsgConsumer 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(30)"
    "Message from: " DYNAMIC-FUNCTION
      ('getCharProperty':U IN hMessage, "FROM").
  RUN deleteMessage IN hMessage.
  APPLY "U1" TO THIS-PROCEDURE.
END PROCEDURE.
2. Run example3.p, as shown:
example3.p
/* Publishes a Text message with properties. */
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.").
/* Set the "FROM:" and the "TO:" properties */
RUN setStringProperty IN hMessage ("FROM", "Super Golf Center").
RUN setStringProperty IN hMessage ("TO", "Sub Par Golf").
/* Publish the message on to the golf topic */
RUN publish IN hPubSubSession ("GolfTopic", hMessage, ?, ?, ?).