Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : PTP messaging examples : Achieving scalable server architecture with PTP queuing
 

Achieving scalable server architecture with PTP queuing

The procedures example20p and example21.p use PTP queuing to achieve scalable server architecture. Several instances of example20.p send requests to a single JMS queue and receive replies from servers that run example21.p. You can add more instances to handle an increasing volume of requests.
To run example20.p and example21.p:
1. Create the requestQueue queue using the SonicMQ Explorer.
2. Run example20.p to send requests to the requestQueue queue, as shown:
example20.p
/* Sends a request to a queue and receives a reply from the server. */
DEFINE VARIABLE hConsumer   AS HANDLE    NO-UNDO.
DEFINE VARIABLE hPTPSession AS HANDLE    NO-UNDO.
DEFINE VARIABLE hRequest    AS HANDLE    NO-UNDO.
DEFINE VARIABLE request     AS CHARACTER 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 hRequest).
/* Creates a consumer for the reply */
RUN createMessageConsumer IN hPTPSession
  (THIS-PROCEDURE,    /* this procedure will handle it */
   "replyHandler",    /* name of internal procedure */
   OUTPUT hConsumer).
/* Start the reply receiving */
RUN startReceiveMessages IN hPTPSession.
/* Loop forever. */
REPEAT:
  UPDATE request WITH FRAME f1 CENTERED.
  RUN setText IN hRequest (request).
  /* Sends a request to the requestQueue and handles the reply in the
     replyHandler internal procedure. */
  RUN requestReply IN hPTPSession
    ("requestQueue",
     hRequest,
     ?, /* No reply selector. */
     hConsumer, ?, ?, ?).
  /* Wait for the reply. */
  WAIT-FOR u1 OF THIS-PROCEDURE.
END.
PROCEDURE replyHandler:
  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 from the server. */
  DISPLAY "reply text: "
    DYNAMIC-FUNCTION('getText':U IN hReply) FORMAT "X(30)".
  RUN deleteMessage IN hReply.
  APPLY "U1" TO THIS-PROCEDURE.
END PROCEDURE.
3. Run example21.p to receive requests from the requestQueue queue, execute them, and reply to the requester, as shown:
example21.p
/* This example implements a server who gets requests from a JMS queue,
   executes the request, and replies to the requester. Run several
   instances of this server and several instances of a client (example20)
   to observe the scalability of this configuration. */
DEFINE INPUT PARAMETER serverName AS CHARACTER NO-UNDO.
DEFINE VARIABLE hConsumer    AS HANDLE NO-UNDO.
DEFINE VARIABLE hPTPSession  AS HANDLE NO-UNDO.
DEFINE VARIABLE replyMessage 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.
/* Uses one message for all the replies. */
RUN createTextMessage IN hPTPSession (OUTPUT replyMessage).
/* receives requests from the requestQueue */
RUN createMessageConsumer IN hPTPSession
  (THIS-PROCEDURE,    /* this procedure will handle it */
   "requestHandler",  /* name of internal procedure */
   OUTPUT hConsumer).
RUN receiveFromQueue IN hPTPSession
  ("requestQueue", /* request queue */
   ?, /* no message selector */
   hConsumer). /* handles the messages */
/* Start receiving requests */
RUN startReceiveMessages IN hPTPSession.
/* Process requests forever. */
RUN waitForMessages IN hPTPSession ("inWait", THIS-PROCEDURE, ?).
PROCEDURE requestHandler:
  DEFINE INPUT PARAMETER hRequest     AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER hMsgConsumer AS HANDLE NO-UNDO.
  DEFINE OUTPUT PARAMETER hReply      AS HANDLE NO-UNDO.
  DEFINE VARIABLE replyText AS CHARACTER NO-UNDO.
  /* Creates a reply message. The reply is sent automatically when
     control returns to the ABL-To-JMS implementation. */
  replyText = serverName + " executed " +
    DYNAMIC-FUNCTION ('getText':U IN hRequest).
  RUN deleteMessage IN hRequest.
  hReply = replyMessage.
  RUN setText IN hReply (replyText).
END PROCEDURE.
FUNCTION inWait RETURNS LOGICAL:
  RETURN true.
END.