External stored procedure that reads from a data queue |
In my previous article we created a stored procedure for writing data to the data queue using the external stored procedure. Now, in this article I'm going to read data from the data queue.
RPGLE program to read data from Data queue
D QRCVDTAQ PR extpgm('QRCVDTAQ') D dtquenam 10A const D dtquelib 10A const D datalen 5P 0 const D databuf 32767A const options(*varsize) D waittime 5P 0 const D main pr extpgm('EXTPGM6') D 10a D 10a D 20a D main pi D dtquenam 10a D dtquelib 10a D receive 20a D len s 5P 0 inz D waittime s 5p 0 inz(10) C EVAL RECEIVE = ' ' C CALL 'QRCVDTAQ' C PARM dtquenam C PARM dtquelib C PARM len C PARM receive C PARM waittime C receive DSPLY C len DSPLY C SETON LR C RETURN return
D QRCVDTAQ PR extpgm('QRCVDTAQ') D dtquenam 10A const D dtquelib 10A const D datalen 5P 0 const D databuf 32767A const options(*varsize) D waittime 5P 0 const D main pr extpgm('EXTPGM6') D 10a D 10a D 20a D main pi D dtquenam 10a D dtquelib 10a D receive 20a D len s 5P 0 inz D waittime s 5P 0 inz(10) /Free receive = ' '; QRCVDTAQ(dtquenam:dtquelib:len:receive:waittime); dsply receive; dsply len; *INLR = *ON; return; /End-Free
**FREE dcl-pr QRCVDTAQ extpgm ; *n char(10) const; *n char(10) const; *n packed(5:0) const; *n char(32767) const options(*varsize); *n packed(5:0) const; end-pr; dcl-pi *N; dtquenam char(10); dtquelib char(10); receive char(20); end-pi; dcl-s len packed(5:0) inz; dcl-c waittime const(10); QRCVDTAQ(dtquenam:dtquelib:len:receive:waittime); DSPLY receive; DSPLY len; *INLR = *ON;
Compile RPGLE program to read data from data queue
CRTSQLRPGI OBJ(EASYCLASS1/EXTPGM6) SRCFILE(EASYCLASS1/EXTPROC) SRCMBR(EXTPGM6) COMMIT(*NONE) OBJTYPE(*PGM) REPLACE(*NO) DBGVIEW(*SOURCE)
Explanation of the above code
D QRCVDTAQ PR extpgm('QRCVDTAQ') D dtquenam 10A const D dtquelib 10A const D datalen 5P 0 const D databuf 32767A const options(*varsize) D waittime 5P 0 const
Here, we defined the procedure prototype for the The Receive Data Queue (QRCVDTAQ) API that call external program QRCVDTAQ and is used to read the data from the data queue and this API has several parameters such as name of the data queue dtquenam, then we need to provide the library dtquelib, length of the data queue datalen as output parm and data buffer databuf as output parm and then waittime mentioned the wait time which is in seconds as Input parm and If the wait time value is greater than 0 then the maximum range is 99999 which is approx 28 hours and If the wait time value is less than 0 then it waits forever and continue processing immediately. If no entry exists, the call completes immediately with the length of data parameter set to zero.
D main pr extpgm('EXTPGM6') D 10a D 10a D 20a D main pi D dtquenam 10a D dtquelib 10a D receive 20a D len s 5P 0 inz D waittime s 5p 0 inz(10)
Here, we have defined PR and PI for main procedure/program which is alternative for the entry parameter list where we take input as data queue name, data queue library and last output parameter where we receive the data that reads from the data queue. we defined two variables as len and waittime to be used by the QRCVDTAQ api. waittime is initialized with 10 seconds to wait.
C EVAL RECEIVE = ' ' C CALL 'QRCVDTAQ' C PARM dtquenam C PARM dtquelib C PARM len C PARM receive C PARM waittime C receive DSPLY C len DSPLY C SETON LR C RETURN
First we initialized receive variable with blank. Then we call QRCVDTAQ api by passing data queue name, data queue library, len and receive and waittime. After the call is complete we dsply the receive and len variable where we received data after reading the data queue. Finally set last record indicator to setOn and return from the program.
Create Stored procedure program for reading data from data queue
D sqlproc S 500a inz(*blanks) /Free sqlproc = 'CREATE PROCEDURE EASYCLASS1.READFROMDATAQUEUE( ' + 'IN DQNAM CHAR(10), IN DQLIB CHAR(10), ' + 'OUT RECEIVED CHAR(20)) ' + 'LANGUAGE RPGLE ' + 'SPECIFIC EASYCLASS1.EXTPROCED6 ' + 'EXTERNAL NAME EASYCLASS1.EXTPGM6 ' + 'NOT DETERMINISTIC ' + 'NO SQL ' + 'CALLED ON NULL INPUT ' + 'PARAMETER STYLE GENERAL'; EXEC SQL EXECUTE IMMEDIATE :sqlproc; *INLR = *ON; /End-Free
SQL Script for create stored procedure to read from data queue is as follows:
Compile RPGLE program to create stored procedure that read data from data queue
CRTSQLRPGI OBJ(EASYCLASS1/EXTPROC6) SRCFILE(EASYCLASS1/EXTPROC) SRCMBR(EXTPROC6) COMMIT(*NONE) OBJTYPE(*PGM) REPLACE(*NO) DBGVIEW(*SOURCE)
Call EXTPROC6 to create stored procedure
Once compilation is successfull, the call the program EXTPROC6 from the command line and that will create the externa stored procedure named READFROMDATAQUEUE in library EASYCLASS1 for you.
Write Program that calls external stored procedure that reads data from data queue
Declared 3 variables parm1, parm2 and parm3 as required in stored procedure call and then call the stored procedure using SQL CALL as below and displayed the parm3 that receives data read from data queue.
D parm1 s 10a inz('DATAQUEUE1') D parm2 s 10a inz('EASYCLASS1') D parm3 s 20a inz /Free EXEC SQL CALL EASYCLASS1.READFROMDATAQUEUE(:parm1,:parm2,:parm3); dsply parm3; *INLR = *ON; /End-Free
Compile RPGLE program to Call stored procedure that read data from data queue
CRTSQLRPGI OBJ(EASYCLASS1/CALLPROC6) SRCFILE(EASYCLASS1/CALLPROC6) SRCMBR(EXTPROC6) COMMIT(*NONE) OBJTYPE(*PGM) REPLACE(*NO) DBGVIEW(*SOURCE)
Note: the data queue named DATAQUEUE1 in library EASYCLASS1 were already created and created using CRTDTAQ command. and data can be send to data queue by calling sotred procedure that writes data to data queue.