Tue, May
3 New Articles

Tech Tip: Make Your RPG CGI Programs Perform Better

  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

In my last TechTip, I provided a U.S. ZIP code search program that uses AJAX to display potential results as you type. I mentioned that this technique would cause a lot of extra database access and that one way to avoid a lot of open/close of the database could be to use a data queue to communicate between the RPG CGI program and the database. Now, I have created a programming model that shows how to do just that. (Sorry! Those cool HTML/JavaScript features that I promised to write about will have to wait until the next tip.)

Before I start, let me explain why this is an issue at all: Every time an RPG CGI program is called, it's called through the Web server's CGI job. The Web server watches port 80, waiting for something to happen. When a CGI request arrives, it executes the request, calls a CGI program, opens the files, reads the records, closes the files, and passes data back to the browser. It then eagerly watches port 80 again, ready to answer more requests. If we can minimize the open/close of the database files—or even better, keep the database files open—we can gain a performance advantage. And that's what this TechTip is about. Figure 1 shows how the programming model works.


Figure 1: For maximum efficiency, keep your database files open. (Click images to enlarge.)

A: The browser makes a request and waits for the Web server to answer.
B: The Web server "catches" the request and executes the RPG CGI program.
C: The RPG CGI program places an entry in the data queue INBOX at D and then waits for the answer to appear in the REPLY data queue at F.
E: A never-ending CGI server program waits for something to appear in the data queue INBOX, and when it does, the CGI server program reads the data queue entry, processes it, and writes the answer back to the REPLY data queue.
C: The RPG CGI program reads the REPLY data queue, creates an HTML reply, and writes it back to the waiting browser.

Wow! This is a lot of waiting. Does this really work? Yes. And it works very fast.

But how does the RPG CGI program know which entry to read from the REPLY data queue? The keyed data queues are the nuts and bolts of all this. If you have not worked with keyed data queues before, here is what the help text on the CrtDtaQ command says: "Data queue entries are received by key. A key is a prefix added to an entry by its sender." So the challenge is to generate a unique key that can be passed around with the data entry and to make sure that it is picked up by the correct requester.

For that purpose, I use a Universally Unique Identifier (UUID), which is a 16-byte string that is guaranteed to be unique. My good friend Carsten Flensburg helped me write the GETUUID program used in this example, and just for fun I created a file and wrote 10,000,000 records into it using GETUUID to generate the keys. None came out the same, so let's assume that it works the way we want.

But what if I want to use the INBOX to serve various CGI server programs? The INBOX data queue is also keyed, and I just use the name of the program and library that are to read the data queue entry. This is a very simple way to solve this problem, and it ensure me that a lot of different programs can monitor the INBOX data queue without "stealing" something that does not belong to them.

I have created a small library called CGISERVER, which contains the data queues, the data files, and the RPG programs.

Here is an overview of the functions of the programs:

GETUUID     RPGLE       Generate Universally Unique Identifier (UUID)
REQ001      RPGLE       Request data from server program
SRV000      RPGLE       Serve data to CGI program
STP001      RPGLE       Stop server program
BUILDDTAQ   CLLE        Build data queues
USSTATES    *FILE       PF-DTA      US states   
USZIPCODES  *FILE       PF-DTA      US Zip Codes
INBOX       *DTAQ                   INBOX data 
REPLY       *DTAQ                   REPLY data 
QCLSRC      *FILE       PF-SRC      CL source
QRPGSRC     *FILE       PF-SRC      RPG source

To install the CGISERVER library and test it, do the following:

  1. Download the zipped save file cgiserver.zip.
  2. Create a save file called CGISERVER somewhere on your i5.
  3. Unzip and FTP the save file to your i5 Web server to the save file you just created.
  4. Restore the CGISERVER library with the following command:
  5. Add CGISERVER to your library list.
  6. Submit never-ending program SRV000 to a job queue that accepts more than one jobq entry (for example, QUSRNOMAX) with the following command:
    You might have to twist the command with a proper user profile. Also, remember to check whether QUSRNOMAX is active; alternatively, you can use QSYSNOMAX.Note: If you want to stop the SRV000 program, just call STP001, which will send a "*STOPPGM" entry to the server program.
  7. To test, you can call program REQ001 by entering CALL REQ001. This will fetch 50 entries from database USZIPCODES and print a primitive list (nothing fancy but good enough to see that things are working).

When you look at the programs, you will see that I use QUALIFIED data structures to move the data around. This could be done using arrays or strings, but remember that this is just a model to show you how to remove databases from CGI programs and move them into never-ending CGI server programs.

Because I want to make this work in my U.S. ZIP code search program, I created a new RPG CGI program called FORM007, which replaces FORM005 that was used in my last tip.

To install the FORM007 RPG CGI program, do the following:

  1. Download the source.
  2. Unzip and FTP the source to your i5 CGI library and follow the instructions in the header section to compile it.
  3. If you have installed code from the "Give Me That ZIP Code, Please" TechTip, you have to make a change in the JavaScript that handles the call to the Web server. In directory /ajax, locate the file called getzipcodeus.js and find the following line:
    var url="http://yourserver:81/cgi-bin/form005"
    Change it to
    var url="http://yourserver:81/cgi-bin/form007"
  4. If you have not installed the code from "Give Me That ZIP Code, Please," you can just call FORM007 from your browser:


Be sure to have SRV000 running before you test. Otherwise, you will get the message shown in Figure 2.


Figure 2: Avoid this error message.

This completes this TechTip, which I hope can help you make your CGI programs perform a little better in a quite simple way.

But before I fade away, I have three more things to mention:

If necessary, it is very easy to scale the CGI server programs. Just call them again and you add some horsepower. Very easy.

  1. If you have some data that resides on a different i5 box than your Web server is running on, just create a remote data queue and you are done. I did a test in our environment, and it took me no more than 10 minutes to set up a working example.
  2. Data queues are a very cheap and simple way to send data back and forth quickly. If you need a more robust and secure way to send data, consider WebSphere MQ (formerly MQ Series).

Stay tuned! Cool HTML/JavaScript features are coming up in my next TechTip.

Jan Jorgensen is a programmer at Electrolux Laundry Systems Denmark. He works with stuff like RPG, HTML, JavaScript, and Perl. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it..

Jan Jorgensen

Jan Jorgensen is one of the owners of www.reeft.dk, which specializes in mobile and i5 solutions. He works with RPG, HTML, JavaScript, Perl, and PHP. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it.




Support MC Press Online

$0.00 Raised:

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: