Try OpenEdge Now
skip to main content
Messaging and ESB
Messaging Examples : Pub/Sub messaging examples : Installing an error handler to handle an asynchronous error
 

Installing an error handler to handle an asynchronous error

The procedure example16.p installs an error handler to detect a JMS server communication loss.
To install an error handler to handle an asynchronous error:
1. Run example16.p, as shown:
example16.p
/* Installs an error handler to deal with a JMS server communication loss. */
DEFINE VARIABLE errorConsumer  AS HANDLE  NO-UNDO.
DEFINE VARIABLE hPubSubSession AS HANDLE  NO-UNDO.
DEFINE VARIABLE jmsIsOk        AS LOGICAL NO-UNDO INITIAL TRUE.
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, "errorHandler", OUTPUT errorConsumer).
RUN setErrorHandler IN hPubSubSession (errorConsumer).
RUN startReceiveMessages IN hPubSubSession.
/* Wait forever for messages until the connection with the JMS server is
   lost with error code "-5" (shutdown the SonicMQ Broker to simulate
   that). */
RUN waitForMessages IN hPubSubSession ("inWait", THIS-PROCEDURE, ?).
IF NOT jmsIsOk THEN DO:
  MESSAGE "Disocnnectiong from JMS Server... "VIEW-AS ALERT-BOX.
  RUN deleteSession IN hPubSubSession.
END.
FUNCTION inWait RETURNS LOGICAL:
  RETURN jmsIsOk.
END.
PROCEDURE errorHandler:
  DEFINE INPUT PARAMETER hMessage         AS HANDLE NO-UNDO.
  DEFINE INPUT PARAMETER hMessageConsumer AS HANDLE NO-UNDO.
  DEFINE OUTPUT PARAMETER hReply          AS HANDLE NO-UNDO.
  DEFINE VARIABLE errorCode AS CHARACTER NO-UNDO.
  DEFINE VARIABLE errorText AS CHARACTER NO-UNDO.
  ASSIGN
    errorCode = DYNAMIC-FUNCTION
      ('getCharProperty':U IN hMessage, "errorCode")
    errorText = DYNAMIC-FUNCTION('getText':U IN hMessage).
  RUN deleteMessage IN hMessage.
  MESSAGE errorText errorCode VIEW-AS ALERT-BOX.
  IF errorCode = "-5" THEN
    jmsIsOk = FALSE.
END.
2. Shut down the SonicMQ Broker to simulate the communication loss.