Register Activation Group Exit Procedure
Here's a message that I posted to the iSeries Network RPG forum back in
May. It was in reply to someone who wanted to run procedures when a service
program was activated or deactivated (much like a constructor/destructor
in OO languages.)
As far as I know, there's no way to run a procedure during the activation
phase.
Here's how I approach the problem: I have a subprocedure that gets called
first by every exported subprocedure. I use a global field to determine
if it's been called before, and if so, it does not run again.
I also tend to register a cleanup procedure with CEE4RAGE. Though,
ending the activation group should theoretically clean everything
up for me, I like to do it explicitly. I like to be in control! :)
The following code should help illustrate what I'm talking about:
H NOMAIN
D Initialized s 1N inz(*off)
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Initialization routine
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P InzSrvPgm B
D InzSrvPgm PI
D CEE4RAGE PR
D procedure * procptr const
D feedback 12A options(*omit)
/free
if (Initialized);
return;
endif;
// .. open files here ...
// .. other initializations ...
CEE4RAGE(%paddr(EndSrvPgm): *OMIT);
Initialized = *ON;
return;
/end-free
P e
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* EndSrvPgm(): This is called automatically when the activation
* group ends. (which is when the srvpgm is removed
* from memory.)
*
* Note that this must be exported so that CEE4RAGE can call it.
*
* InzSrvPgm() registers this procedure with the CEE4RAGE() API
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P EndSrvPgm B export
D EndSrvPgm PI
D AgMark 10U 0 options(*nopass)
D Reason 10U 0 options(*nopass)
D Result 10U 0 options(*nopass)
D UserRC 10U 0 options(*nopass)
/free
// ... terminate any helper processes
// ... shut down any persistent APIs
// ... close files.
// ... deallocate memory.
Initialized = *OFF;
return;
/end-free
P E
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Every exported procedure (besides EndSrvPgm) starts by calling
* the InzSrvPgm() subprocedure
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P SomeProc B export
D SomeProc PI 10I 0
D SomeParm 123A const
/free
InzSrvPgm();
// ... perform function of subproc here ...
return data;
/end-free
P E
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* Every exported procedure (besides EndSrvPgm) starts by calling
* the InzSrvPgm() subprocedure
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P AnotherProc B export
D AnotherProc PI 10I 0
D AnotherParm 321A const
/free
InzSrvPgm();
// ... perform function of subproc here ...
return data;
/end-free
P E
Thanks to Scott Klement
阅读(752) | 评论(0) | 转发(0) |