Kbase P100905: 4GL code example for error handling when sending JMS messages to a queue
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  08/10/2009 |
|
Status: Verified
GOAL:
4GL code example for error handling when sending JMS messages to a queue
GOAL:
How to detect if a SonicMQ broker is still alive before sending a message to a queue
FACT(s) (Environment):
Progress 9.x
OpenEdge 10.x
All Supported Operating Systems
FIX:
/* Error handling example code for sending a text message to a queue:
Closes the session for the case the SonicMQ broker is not alive anymore.
Does an error handling for the case cannot send the message*/
DEFINE VARIABLE ptpsession AS HANDLE.
DEFINE VARIABLE messageH AS HANDLE.
DEFINE VARIABLE errorConsumer AS HANDLE.
DEFINE VARIABLE jmsIsOk AS LOGICAL INIT yes.
/* Creates a session object. */
RUN jms/ptpsession.p PERSISTENT SET ptpsession ("-H localhost -S 5162").
RUN setBrokerURL IN ptpsession ("localhost:2506").
RUN SETUSER IN ptpsession ("Administrator").
RUN setPassword IN ptpsession ("Administrator").
/* Ping the broker every 7 s */
RUN setPingInterval IN ptpsession (7) .
RUN setNoErrorDisplay IN ptpsession (TRUE).
RUN beginSession IN ptpsession.
/* Creates error consumer for PingInterval */
RUN createMessageConsumer IN ptpsession(THIS-PROCEDURE, "errorHandler",
OUTPUT errorConsumer).
RUN setErrorHandler IN ptpsession(errorConsumer).
/* Create a text message */
RUN createTextMessage IN ptpsession (OUTPUT messageH).
RUN setText IN messageH ("Golf shoes on sale today.").
MESSAGE "Waiting 20s to see if the SonicMQ broker is still alive".
/* Wait 20s for the ping to return or error */
RUN waitForMessages IN ptpsession ("inWait", THIS-PROCEDURE, 20).
IF NOT jmsIsOk THEN DO:
MESSAGE "Disconnectiong from JMS Server... "VIEW-AS ALERT-BOX.
RUN deleteMessage IN messageH.
RUN deleteSession IN ptpsession.
END.
IF jmsIsOk THEN DO:
/* Sends the message to the "GolfQueue" queue */
RUN sendToQueue IN ptpsession ("SampleQ1", messageH, ?, ?, ?) NO-ERROR.
IF ERROR-STATUS:ERROR THEN MESSAGE "ERROR sending TO queue SampleQ1 " RETURN-VALUE VIEW-AS ALERT-BOX.
ELSE MESSAGE "Sent message to SampleQ1" VIEW-AS ALERT-BOX.
RUN deleteMessage IN messageH.
RUN deleteSession IN ptpsession.
END.
MESSAGE "Deleted message and queue session " VIEW-AS ALERT-BOX.
FUNCTION inWait RETURNS LOGICAL.
RETURN jmsIsOk.
END.
PROCEDURE errorHandler:
DEFINE INPUT PARAMETER messageH AS HANDLE NO-UNDO.
DEFINE INPUT PARAMETER messageConsumerH AS HANDLE NO-UNDO.
DEFINE OUTPUT PARAMETER replyH AS HANDLE NO-UNDO.
DEFINE VAR errorCode AS CHAR NO-UNDO.
DEFINE VAR errorText AS CHAR NO-UNDO.
ASSIGN
errorCode = DYNAMIC-FUNCTION('getCharProperty':U IN messageH, "errorCode") .
ASSIGN errorText = DYNAMIC-FUNCTION('getText':U IN messageH).
RUN deleteMessage IN messageH.
IF errorCode = "-5" THEN
jmsIsOk = no.
MESSAGE "jmsIsOk =" jmsIsOk errorText errorCode VIEW-AS ALERT-BOX.
END.