Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : PTP messaging examples : Sending a message to a queue and receiving a message from a queue
 

Sending a message to a queue and receiving a message from a queue

The procedures example18.p and example19.p send and receive a message from a queue.
To send a message to a queue and receive a message from a queue:
1. Create the GolfQueue queue using the SonicMQ Explorer. (See SonicMQ Programming Guide for information about creating queues.)
2. Run example18.p to send a TextMessage to the GolfQueue, as shown:
example18.p
/* Sends A Text message to a queue. */
DEFINE VARIABLE hMessage    AS HANDLE NO-UNDO.
DEFINE VARIABLE hPTPSession AS HANDLE NO-UNDO.
/* Creates a session object. */
RUN jms/ptpsession.p PERSISTENT SET hPTPSession
  ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPTPSession ("localhost:2506").
RUN beginSession IN hPTPSession.
/* Create a text message */
RUN createTextMessage IN hPTPSession (OUTPUT hMessage).
RUN setText IN hMessage ("Golf shoes on sale today.").
/* Sends the message to the "GolfQueue" queue */
RUN sendToQueue IN hPTPSession ("GolfQueue", hMessage, ?, ?, ?).
RUN deleteMessage IN hMessage.
RUN deleteSession IN hPTPSession.
3. Run example19.p to receive a message from the GolfQueue, as shown:
example19.p
/* Receives a Text message from a queue. */
DEFINE VARIABLE hConsumer   AS HANDLE NO-UNDO.
DEFINE VARIABLE hPTPSession AS HANDLE NO-UNDO.
/* Creates a session object. */
RUN jms/ptpsession.p PERSISTENT SET hPTPSession
 ("-H localhost -S 5162 ").
RUN setBrokerURL IN hPTPSession ("localhost:2506").
RUN beginSession IN hPTPSession.
/* GolfQueue Messages are handled by the "golfHandler" procedure. */
RUN createMessageConsumer IN hPTPSession
  (THIS-PROCEDURE,    /* this procedure will handle it */
   "golfHandler",     /* name of internal procedure */
   OUTPUT hConsumer).
RUN receiveFromQueue IN hPTPSession
  ("GolfQueue",       /* name of queue */
   ?,                 /* no message selector */
   hConsumer).        /* handles incoming messages*/
/* Start receiving messages */
RUN startReceiveMessages IN hPTPSession.
/* 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(70)".
  RUN deleteMessage IN hMessage.
  APPLY "U1" TO THIS-PROCEDURE.
END PROCEDURE.