Service program on IBM i

28 Views


A service program on IBM i (AS/400, IBM i) is one of the core building blocks of ILE (Integrated Language Environment).

Think of it as IBM i’s version of a shared library (.dll on Windows, .so on Unix/Linux, .jar on Java).


🧠 Definition

A service program is:

  • A collection of procedures (functions/subroutines) and possibly data

  • Compiled once and stored in a system object (*SRVPGM)

  • Designed to be reused by multiple programs or other service programs

  • Lives inside an activation group when called, and can stay resident in memory for fast reuse


✅ Why Use Service Programs?

Benefit Explanation
Code reuse Write procedures once, use in many programs
Easier maintenance Fix logic in one place instead of 50 programs
Smaller programs Main programs don’t need to duplicate code
Better performance Service program can stay loaded in memory across calls
Encapsulation Hide implementation details, expose only needed APIs

🧩 Example Structure

A typical service program flow:

Module (SRVPGMMOD)  -->  Service Program (*SRVPGM)  -->  Called by RPG/CL/COBOL programs

🛠 Steps to Create a Service Program

1. Write RPGLE Module (Procedures)

MATHMOD.RPGLE

ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-proc AddNums export;
   dcl-pi *n int(10);
      a int(10);
      b int(10);
   end-pi;
   return a + b;
end-proc;

dcl-proc SubNums export;
   dcl-pi *n int(10);
      a int(10);
      b int(10);
   end-pi;
   return a - b;
end-proc;

Compile as a module:

CRTRPGMOD MODULE(MYLIB/MATHMOD) SRCFILE(MYLIB/QRPGLESRC)

2. Create a Service Program

CRTSRVPGM SRVPGM(MYLIB/MATHSRV) MODULE(MYLIB/MATHMOD) EXPORT(*ALL)

This packages the procedures into a reusable service program.


3. Call from a Program

MAINPGM.RPGLE

ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-pr AddNums int(10) extproc(*dclcase);
   a int(10);
   b int(10);
end-pr;

dcl-pr SubNums int(10) extproc(*dclcase);
   a int(10);
   b int(10);
end-pr;

dsply ('5 + 3 = ' + %char(AddNums(5: 3)));
dsply ('10 - 7 = ' + %char(SubNums(10: 7)));

Compile program and bind it to the service program:

CRTBNDRPG PGM(MYLIB/MAINPGM) SRCFILE(MYLIB/QRPGLESRC) BNDSRVPGM((MYLIB/MATHSRV))

📌 Notes About Service Programs

  • Activation Group: Usually compiled with ACTGRP(*CALLER) so they share the caller’s AG.

  • Exported Procedures: Only procedures declared with EXPORT are callable externally.

  • Signature: Service programs have a signature (like a version ID). Changing the signature without rebinding can cause binding errors.


🔍 Service Program vs Program

Feature Program (*PGM) Service Program (*SRVPGM)
Entry point Yes (main procedure) No (only procedures)
Standalone runnable Yes No (must be bound or called)
Reusable code Limited High
Used for APIs Sometimes Always

👉 In short:
A program = something you call to run.
A service program = a library of procedures you bind into other programs.


 

Post Comments