Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Publishing and receiving a group of messages in a transaction
 

Publishing and receiving a group of messages in a transaction

The procedures example22.p and example23.p publish and receive a group of messages in a single transaction. The procedure example22.p creates a session that is transacted for sending, and The procedure example23.p creates a session that is transacted for receiving.
To publish and receive a group of messages in a transaction:
1. Run example23.p so the subscriber is running before you publish, as shown:
example23.p
/* Subscribes and receives three messages in a single transaction. */
DEFINE VARIABLE hConsumer      AS HANDLE  NO-UNDO.
DEFINE VARIABLE hPubSubSession AS HANDLE  NO-UNDO.
DEFINE VARIABLE iMsgNum        AS INTEGER NO-UNDO.
/* Creates a transaction for receiving session. */
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN setTransactedReceive IN hPubSubSession.
RUN beginSession IN hPubSubSession.
/* Subscribe to the TestTopic topic. Messages are handled by the
   "msgHandler" internal procedure. */
RUN createMessageConsumer IN hPubSubSession
  (THIS-PROCEDURE,   /* this procedure will handle it */
   "msgHandler",     /* name of internal procedure */
   OUTPUT hConsumer).
RUN subscribe IN hPubSubSession
  ("TestTopic",      /* 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 three messages. */
WAIT-FOR u1 OF THIS-PROCEDURE.
PROCEDURE msgHandler:
  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(70)".
  RUN deleteMessage IN hMessage.
  iMsgNum = iMsgNum + 1.
  /* Commit the reception of the three messages. */
  IF iMsgNum = 3 THEN DO:
    RUN commitReceive IN hPubSubSession.
    MESSAGE "committed!".
    APPLY "U1" TO THIS-PROCEDURE.
  END.
END PROCEDURE.
2. Run example22.p to subscribe and receive messages from example22.p in a single transaction, as shown:
example22.p
/* Publishes A group of Text messages in a single transaction. */
DEFINE VARIABLE hMessage       AS HANDLE NO-UNDO.
DEFINE VARIABLE hPubSubSession AS HANDLE NO-UNDO.
/* Creates a transcated for sending session. */
RUN jms/pubsubsession.p PERSISTENT SET hPubSubSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPubSubSession ("localhost:2506").
RUN setTransactedSend IN hPubSubSession.
RUN beginSession IN hPubSubSession.
/* Create a text message */
RUN createTextMessage IN hPubSubSession (OUTPUT hMessage).
/* Publish three messages */
RUN setText IN hMessage ("message1").
RUN publish IN hPubSubSession ("TestTopic", hMessage, ?, ?, ?).
RUN setText IN hMessage ("message2").
RUN publish IN hPubSubSession ("TestTopic", hMessage, ?, ?, ?).
RUN setText IN hMessage ("message3").
RUN publish IN hPubSubSession ("TestTopic", hMessage, ?, ?, ?).
/* Commit the publication of the messages. */
RUN commitSend IN hPubSubSession.
RUN deleteMessage IN hMessage.
RUN deleteSession IN hPubSubSession.