Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Publishing with a reply handle, subscribing, and receiving an automatic reply
 

Publishing with a reply handle, subscribing, and receiving an automatic reply

The procedures example5.p, example6.p, and example7.p illustrate publishing with a reply handle and receiving an automatic reply. The procedure example5.p subscribes with an automatic reply mechanism. It can only reply to messages that have the JMSReplyTo header field. The procedure example6.p subscribes with explicit reply by calling the publishproceduree directly. It can only reply to messages that have the JMSReplyTo header field. The procedure example7.p publishes using the requestReplyprocedure for receiving reply messages from subscribers. It populates the JMSReplyTo header field automatically.
To run example5.p, example6.p, and example7.p:
1. Run example5.p so the subscriber is running before you publish, as shown:
example5.p
/* Using the automatic reply mechanism. Note that the received message
   must have a JMSReplyTo header field for this to work. Example7 can be
   used to receive the reply. */
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.
/* 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 messages */
/* Start receiving messages */
RUN startReceiveMessages IN hPubSubSession.
/* Wait forever to receive messages since "u1" is never applied. */
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.
  /* Creates a reply message. The reply is published automatically when
     control returns to the ABL–JMS implementation.*/
  DISPLAY DYNAMIC-FUNCTION('getText':U IN hMessage) FORMAT "x(60)".
  IF DYNAMIC-FUNCTION('hasReplyTo':U IN hMessage) THEN DO:
    RUN createTextMessage IN hPubSubSession (OUTPUT hReply).
    RUN setText IN hReply ("Will bid. Send data in sportsXML format.").
  END.
  RUN deleteMessage IN hMessage.
END PROCEDURE.
2. Run example6.p to subscribe with explicit reply by calling the publish procedure directly, as shown:
example6.p
/* Replying explicitly. Note that the received message must have a
   JMSReplyTo header field for this to work. Example7 can be used to
   receive the reply. */
DEFINE VARIABLE msgConsumer    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.
RUN createMessageConsumer IN hPubSubSession
  (THIS-PROCEDURE,   /* this procedure will handle it */
   "messageHandler", /* name of internal procedure */
   OUTPUT msgConsumer).
RUN subscribe IN hPubSubSession
  ("GolfTopic",
   ?,               /* no durable subscription */
   ?,               /* no message selector */
   FALSE,            /* want to get my own publications */
   msgConsumer).
RUN startReceiveMessages IN hPubSubSession.
/* Wait forever to receive messages since "u1" is never applied. */
WAIT-FOR u1 OF THIS-PROCEDURE.
RUN deleteSession IN hPubSubSession.
PROCEDURE messageHandler:
  DEFINE INPUT PARAMETER hMessage         AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER hMessageConsumer AS HANDLE NO-UNDO.
  /* hAutoReply is not used in this example */
  DEFINE OUTPUT PARAMETER hAutoReply      AS HANDLE NO-UNDO.
  DEFINE VARIABLE hReply AS HANDLE NO-UNDO.
  DISPLAY DYNAMIC-FUNCTION('getText':U IN hMessage) FORMAT "x(60)".
  IF NOT DYNAMIC-FUNCTION('hasReplyTo':U IN hMessage) THEN RETURN.
  /* Publishes a reply explicitly - using the publish call. */
  RUN createTextMessage IN hPubSubSession (OUTPUT hReply).
  RUN setText IN hReply("Will bid. Send data in sportsXML format.").
  RUN publish IN hPubSubSession (DYNAMIC-FUNCTION
   ('getJMSReplyTo':U IN hMessage), hReply, ?, ?, ?).
  RUN deleteMessage IN hMessage.
  /* After we have sent the message, delete it. */
  RUN deleteMessage IN hReply.
END PROCEDURE.
3. Run example7.p to publish using the requestReply procedure for receiving reply messages from subscribers. It populates the JMSReplyTo header field automatically, as shown:
example7.p
/* Publishes a message and receives a reply. */
DEFINE VARIABLE hConsumer      AS HANDLE NO-UNDO.
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.
/* Start receiving messages */
RUN startReceiveMessages IN hPubSubSession.
/* Create a text message */
RUN createTextMessage IN hPubSubSession (OUTPUT hMessage).
RUN setText IN hMessage ("Golf shoes on sale today.").
/* Creates a consumer for the reply */
RUN createMessageConsumer IN hPubSubSession
  (THIS-PROCEDURE, /* this procedure will handle it */
   "golfHandler",  /* name of internal procedure */
   OUTPUT hConsumer).
/* Publish the message onto the Golf topic. Handle the reply in the
   golfHandler internal procedure. */
RUN requestReply IN hPubSubSession
  ("GolfTopic",
   hMessage,
   ?,         /* No reply selector. */
   hConsumer, ?, ?, ?).
RUN deleteMessage IN hMessage.
/* Wait forever to receive messages since "u1" is never applied. */
WAIT-FOR u1 OF THIS-PROCEDURE.
PROCEDURE golfHandler:
  DEFINE INPUT PARAMETER hReply       AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER hMsgConsumer AS HANDLE NO-UNDO.
  DEFINE OUTPUT PARAMETER responseH   AS HANDLE NO-UNDO.
  /* Display the reply - we are not sending a response. */
  DISPLAY "reply text: "
    DYNAMIC-FUNCTION('getText':U IN hReply) FORMAT "X(30)".
  RUN deleteMessage IN hReply.
END PROCEDURE.