/* 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. |
/* 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. |