How to handle messages in Aperio?
General overwiew.
Generally in API comunication the the service program sends response data only. After the request with incoming parameters are received, server calculates answer and put it into the response. In case there is no corresponding records or update process fails etc.. the apropriate HTTP code is returned back. But what in case the server wants to send message to the client? The Message can be packed to hardcoded field in response data. Or send in HTTP headers etc.. How to transfer variable message text from RPG program to the client. If there is only one RPG program in call chain then seems to be not an issue to receive mesages sent by it then put all of them into response… But in case there are several programs in call chain, all of them should have possibility to send their messages independly then gather all of them in the response. How to do it? There is no general answer. There are couple of ways. In this article we describe three of them which we currently use in our solutions.
Standard DC1 method
If in call chain any error or warning occure the program can send message using IBSJob_raiseWarning() or IBSJob_raiseException() procedures. See example in RPG programs in DC1. These procedures store message text together with data (field names, variables) inside memory of job. Then the program whoch actually prepares response (MGRR….) can read all messages from memory and put them into response. To put message into response You can use procedure respAddMessage(). Then clear memory of job. Actually If You choose this technique you don’t need to do almost enything except use correct skeleton of program (See example or skeleton programs in QSAMPLE source file in IAF100AP library). The DC1 dependent programs contains already prepared procedures to handle messages sent via IBSJob_raiseWarning() or IBSJob_raiseException() procedures. The procedures are located in common source:
Depend on flag variables You can decide if messages will be put into response or not. See lcl_startProgram() procedure.
Using temporary file
The other method to send messages metween programs and back to MGRR program is by using temporary file. MGRR program can create temporary file in QTEMP library. Then all next programs in call stack can write messages into this temporary file. At the end the MGRR program can gather all the messages and by respAddMessage() put all messages into response. This tehnique is used in Mobile Warehouse APIs. See MGRR1001 as an example.
This technique works but developer must handle messages flow. Remember to remove old messages from temporary file. The same job can be used for other API calls.
Native IBMi method.
There is one more method which can be used to pass messages trough the chain of calls. Native IBMi message handling. You can use sentProgramMessage system API to issue messages into call ctack. Then the MGRR program can gather all of them and put into response.
In Aperio we have prepared set of procedures to help programmer to handle messages in this way. The most important thing is to pass call stack entry name trough all callers to the last calling programs. So, all programs knows to which call stack they should send message. It can be Z1PGMN variable which contains current program name. Copybook for messages’ procedures:
Link to the message file used in the program can be set by setDftMsgFile().
Example:
The messages can be sent by sendPgmMsg().
Then all messages from call stack can be added to the response by one line of code in MGRR program with command respAddPgmMessages().