IBM i Activation Groups – Part 4

In this fourth and last article about Activation Groups you’ll have the opportunity to test what happens in this situation:

  • Use an *NEW activation group

Let me tell you how this tests is done. There is one main program called ACTGRP111, a secondary program called ACTGRP2 and a support program called RMVEXTMSG.

ACTGRP111 uses *NEW as value of the ACTGRP control specification keyword so a new activation group will be created each time the program is called. 

ACTGRP2, which is called by ACTGRP111 ends only with RETURN operation code so the file opened in it won’t be closed when the program ends.

RMVEXTMSG. As the program uses DSPLY operation code to show messages to the external queue, this CL program is used to clean the queue before the messages are written.

Once you run ACTGRP111 you’ll have information about what files are opened before the program calls ACTGRP2, when ACTGRP2 is running and after ACTGRP2 has ended. This information is showed because of execution of command DSPJOB OPTION(*OPNF) inside every program. You should execute the command one more time (this time from the command line) once the main program has finished so you’ll be able to see what files remains opened.

TEST 3 – *NEW activation group

Let’s begin with ACTGRP111. This program uses *NEW as value of keyword ACTGPR of the control specification so a new activation group will be created each time the program is called. Then it calls ACTGRP2 that has actgrp(*caller) so it will run in the same activation group as the calling program whose name will be assigned by the system.

You can see the code below.

ACTGRP111:

				
					**free
ctl-opt actgrp('*NEW');

dcl-f file1 extdesc('ASALCEDO1/FILE1');

dcl-pr QCMDEXC extpgm;
  *n char(10000) options(*varsize) const;
  *n packed(15:5) const;
end-pr;

dcl-pr actGrp2 extpgm('ASALCEDO1/ACTGRP2');
end-pr;

dcl-pr rmvMsg extpgm('ASALCEDO1/RMVEXTMSG');
end-pr;

dcl-s command varchar(10000);
dcl-s stopDsply char(1);

rmvMsg();
dsply 'BEFORE ActGrp2';
dsply 'Press Enter' ' ' stopDsply;

command = 'DSPJOB OPTION(*OPNF)';
qcmdexc(command:%len(command));

actGrp2();

rmvMsg();
dsply 'AFTER ActGrp2';
dsply 'Press Enter' ' ' stopDsply;

qcmdexc(command:%len(command));

*inlr = *on;    
				
			

Explanation:

Line 1: Code will be fully free.

Line 2: Compile time parameter actgrp is set to *NEW so the system will create a new activation group and this program will be run in it.

Line 4file1 is declared. (Use any file of one of your libraries if you are running this program in your environment).

Lines 6-9: Prototype for QCMDEXC to execute commands inside RPG code.

Lines 11-12: Prototype for ACTGRP2.

Lines 14-15: Prototype for RMVEXTMSG, the CL program to clear external queue.

Line 17: Variable to hold the command to be executed.

Line 18: Variable to stop the program when a message is displayed in the external queue using DSPLY operation code.

Line 20: Call to rmvMsg so external queue is cleared.

Line 21: Send a message to the external queue just to inform that ACTGRP2 is not running so the output of the command will show information only for ACTGRP111. This is not necessary to activation groups to work, it’s here just for demonstration purposes.

Line 22: Send a message to the external queue and wait for an answer in the variable stopDsply so the program will wait until Enter key is pressed and the previous message can be viewed.

Lines 24-25: Command DSPJOB will be executed but only to show opened files and the activation group in which they are opened.

Line 27: ACTGRP2 is called.

Lines 29-31: External queue is cleared again and a new message is displayed indicating that program ACTGRP2 has ended.

Line 33: DSPJOB is executed again to show which files remains opened after ACTGRP2 has run.

Line 35: ACTGRP111 ends with Last Record indicator on so files are closed.

ACTGRP2:

				
					**free
ctl-opt actgrp(*caller);

dcl-f file2 extdesc('ASALCEDO1/FILE2');

dcl-pr QCMDEXC extpgm;
  *n char(10000) options(*varsize) const;
  *n packed(15:5) const;
end-pr;

dcl-pr rmvMsg extpgm('ASALCEDO1/RMVEXTMSG');
end-pr;

dcl-s command varchar(10000);
dcl-s stopDsply char(1);

rmvMsg();
dsply 'Inside ACTGRP2';
dsply 'Press Enter' ' ' stopDsply;

command = 'DSPJOB OPTION(*OPNF)';
qcmdexc(command:%len(command));

return;  
				
			

Explanation:

Line 1: Code will be fully free.

Line 2: Compile parameter ACTGRP(*CALLER) is set so this program will run in the same activation group as the calling program.

Line 4: file2 is declared. (Use any file of one of your libraries if you are running this program in your environment.)

Lines 6-9: Prototype for QCMDEXC to execute commands inside RPG code.

Lines 11-12: Prototype for RMVEXTMSG, the CL program to clear external queue.

Line 14: Variable to hold the command to be executed.

Line 15: Variable to stop the program when a message is displayed in the external queue using DSPLY operation code.

Line 17: Call to rmvMsg so external queue is cleared.

Line 18: Send a message to the external queue just to inform that ACTGRP2 is running so the output of the command will show information for ACTGRP2 and its calling program, ACTGRP111. This is not necessary to activation groups to work, it’s here just for demonstration purposes.

Line 19: Send a message to the external queue and wait for an answer in the variable stopDsply so the program will wait until Enter key is pressed and the previous message can be viewed.

Lines 21-22: Command DSPJOB will be executed but only to show opened files and the activation group in which they are opened.

Line 24: ACTGRP2 ends only with RETURN operation code so files are NOT closed.

RMVEXTMSG:

				
					            PGM
             RMVMSG     PGMQ(*EXT) CLEAR(*ALL) RMVEXCP(*NO)
             ENDPGM 
				
			

Explanation:

Line 1: Start CL program.

Line 2: Remove Message command. This will remove all messages from EXTERNAL queue but unhandled exception messages.

Line 3: End CL program.

The test begins with the execution of DSPJOB OPTION(*OPNF) to show there are no opened files before ACTGRP111 is run.

Then ACTGRP111 is called. The program sends a message to the EXTERNAL queue informing that the information to be displayed on the next screen is BEFORE running the ACTGRP2 program.

Pressing Enter the program will continue and it will show the files opened by ACTGRP111.

We can see in this screen that FILE1 is now opened and that the activation group in which is opened is number 20 (remember that the value typed in the actgrp keyword of the ctl-opt specification was *NEW).

Pressing Enter the program will continue.

Now ACTGRP2 is called. The program sends a message to the EXTERNAL queue informing that the information to be displayed on the next screen is about ACTGRP2 program which is now running.

Pressing Enter the program will continue.

Now a new file has been added to the list. Program ACTGRP2 is now running and file FILE2 is now opened. If you look to the activation group in which it is opened it is number 20 and this is because of it was compiled with parameter ACTGRP(*CALLER) that means the program will be run in the same activation group as the calling program does,  that is in the same activation group as ACTGRP111.

Pressing Enter the program will continue.

ACTGRP2 has ended. The program sends a message to the EXTERNAL queue informing that the information to be displayed on the next screen is about ACTGRP111 because ACTGRP2 has ended.

Pressing Enter the program will continue.

And as expected file FILE2 is still opened as program ACTGRP2 has ended with just a RETURN operation code so no automatic close of files is done. The question is: Will it be closed once program ACTGRP111 has ended?

Pressing Enter the program will end.

It’s time to see if file FILE2 is still opened. So type DSPJOB OPTION(*OPNF) in any command line and press Enter.

And magic is done! File FILE2 is closed. And that’s the correct behavior when you are working with ACTGRP(*NEW) when you compile a program. After this program ends, and the activation group has no other program running inside it, the activation group is automatically deleted and all the files that were opened are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *