Continuing my articles about PHP and RPG communicating together, this time I will show how these two languages can talk with each other using data queues.
If you know all about dataqueues, you can skip this section!
Dataqueues are an object type of *DTAQ that can be used for fast communications between programs. Using these objects is a lot faster than using a database table, or data area for example.
It is possible to just read the dataqueue message and leave it there, or read the entry and remove it. For the majority of applications, once the message has been read, it is removed.
Additionally, it is now possible to journal dataqueues for auditing/recovery purposes.
An Example of Using DataQueues
In one of our applications, we need to send HTML emails to support staff reporting any end of day processing problems.
As it is very simple to send a fancy email message using PHP, we decided that the overnight processing would send a message to a data queue where a PHP program would be waiting and then transmit the email to staff.
Firstly, we needed to create the data queue, as you know, this could easily be achieved by using the CRTDTAQ (Create DataQueue) command from a 5250 command line, but in this example, we show how to use the IBM i PHP toolkit to create the queues.
Here we can see that we create a dataqueue object using the DataQueue class.
We then call the CreateDataQ() function with all the queue attributes of queue name and library.
If the dataqueue is keyed, the key length, any authority, the maximum number of entries, and the initial number of entries are all passed as parameters to this function.
In this next example, we will be using an RPG program to send data to a data queue.
This program uses the traditional system API QSNDDTAQ to send a message.
So, that’s put some information into our dataqueue, now the PHP needs to retrieve this message, process it and send out the email.
The next part of our example is to show you how our complete PHP script processes the data queue and sends out the relevant email.
Featured within this script, is the “stop req” processing, which is explained in more detail in the next part of this article.
The following pseudo code explains this script
1. Make the connection to the IBM i
2. Switch on debug, just in case
3. Get the toolkit to use the dataqueue facilities
4. In a never ending loop, poll the dataqueue waiting for any message that has *EMAIL as a keyed entry. We will not receive any other keyed entries.
5. If the message we received contains the text ‘Stop Req’, we drop out the loop and end our processing.
6. Call the Send_Email function, passing the message as a parameter.
7. Split the message, using the comma symbol as a delimiter
8. Build the variables for the email
9. Send the email, using Zend Mail
10. Loop around to step 3 to await the next message.
If we do not want our PHP to continually poll the data queue, we send a ‘Stop Req’ message to the queue. Once the PHP has processed the entries in the queue, it receives the stop request message and completes normally.
For this example, I will show how this can be achieved using CL programming.
Although we typically see a high level language using the system APIs, it is just as easy to use the same API in a CL program.
Hopefully, this article in our PHP and RPG series, shows how the two languages can work together seamlessly.
If you take a look at the coding in the IBM i PHP toolkit, you will soon see that the dataqueue functions provided are just wrappers to the IBM dataqueue APIs that we used in our RPG examples, but made available to us as callable functions.
As I say many times, if you have any problems with the toolkit, switch on the debugging feature and check the toolkit log, which by default is /usr/local/zendsvr6/var/log/tkit_debug.log, and don’t forget the PHP logs in the same file directory, they are your friends, use them.
Contact us if you need any assistance with your PHP projects.
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems.