Service programs can be written in a variety of languages, including RPG, COBOL, C, and C++. They can also be used to provide an interface to external resources, such as databases or web services.
We have three components to setup for our service program, the service program itself, a binding directory and the binder language that tells the system what procedures the service program is sharing with the world.
Let's look at defining a simple *SRVPGM in Visual Studio Code for IBM i
Service Program - simplesrv.srvpgm
I'm starting this one by creating a folder called "services" and adding that folder name to the root/Rules.mk file. Then copying in an .ibmi.json and a Rules.mk file - we will edit the Rules.mk file to reference our new service program when we write it.
Now we can write a very simple SQL RPGLE service program to do something like return the ibm i system name.
HINT - we could do this is a single bound RPG program rather than a MODULE and ILEPGM but for clarity I'm going to do the longer format module and ILEPGM. If we want to make a bound program we would have simply changed the name of our source file from .sqlrpgle to .pgm.sqlrpgle
We create our module source code for this service program with an .sqlrpgle extension. I have called mine SIMPLEMOD-Simple_Service_Program.sqlrpgle
SIMPLEMOD-Simple_Service_Program.sqlrpgle
ctl-opt nomain;
dcl-proc getsystemname export;
dcl-pi getsystemname;
systemname char(8);
end-pi;
exec sql
VALUES CURRENT SERVER INTO :systemname ;
return;
end-proc;
Now we need to tell BOB that this will be compiled as a module. Make sure this line is in the Rules.mk file:
We can compile this, and it will create a module called SIMPLEMOD:
Create the module
Once it's deployed and compiled you should see a module in your project library:
Now let's create the service program, using this module.
To do this we need to add a new file with the extension .bnd This is the Binder Source language that defines what the service program is going to do. This definition will tell BOB to perform the crtsrvpgm command.
Update the Rules.mk file
Now we need to add the .srvpgm definition to the Rules.mk file to tell BOB to create a service program from the binder language and the module (or multiple models):
SIMPLESRV.SRVPGM: SIMPLESRV.BND SIMPLEMOD.MODULE
SIMPLESRV.bnd
The binder language definition tells the system which procedures will be exported from the service program. In other words, which procedures will be exported or made available for use for programs that want to use this service program.
EXPORT SYMBOL('GETSYSTEMNAME')
ENDPGMEXP
HINT - remember to also add your services folder to the root Rules.mk file so the compiler knows to look in there and process its Rules.mk file.
And that's it!
Or is it?
Of course, it's not - we want to create a binding directory so that RPG programs can use that binding directory to find our new service program.
But let's dive into this during the next lesson...