One of the most basic functions of inter-process communication is the use of the queue. This first queuing article will introduce the simplest of the IBM i queuing mechanisms, the data queue.
In programming, queuing is a technique used to transfer data asynchronously between two software components. It differs from a parameter in that the sender can place a message on a queue, go on processing, and then optionally check later to see if the message was processed.
The asynchronous nature of queuing is very different from the synchronous style of passing data via one or more parameters directly to another procedure or program and waiting until that data is processed. Asynchronous processing provides a wide spectrum of capabilities, ranging from simple flow control to sophisticated concepts like store and forward, subscribe and publish, and guaranteed delivery. And since there are so many different uses for queuing, the IBM i has several different queue implementations. This article introduces you to the data queue.
Data Queues vs. Message Queues
I consider data queues the simplest queuing mechanism on the IBM i, although I suppose an argument could be made that message queues are "simpler" than data queues because they have more built-in support. The problem is that message queues are designed for a very specific function: communicating operational information from one application component to another and ultimately to human beings. Because of this focus, the message queue infrastructure has several features specific to textual data and locales, such as (human) language support and substitution variables. Data queues, on the other hand, tend to be locale-independent and more centered on transporting binary or at least machine-targeted data (I use "machine-targeted" when I refer to encoding such as XML, which is nominally human-readable but really intended to pass information between programming tiers).
That doesn't make message queues any less important; in fact, message queues are fundamental to programming on the IBM i, especially in an ILE environment. I'll try to address message queues and their uses in another installment. And if we have time, I'll also go in the other direction and talk about user queues, an even more basic data transport mechanism. But for now, let me focus on the data queue.
Creating and Deleting
Creating and deleting data queues is relatively straightforward: the CRTDTAQ and DLTDTAQ commands provide those capabilities. And while you might expect (rightly) that the DLTDTAQ command doesn't need many parameters, it might not be so intuitive that the base CRTDTAQ command is also light on parameters. DLTDTAQ first:
Data queue . . . . . . . . . . . Name, generic*
Library . . . . . . . . . . . *LIBL Name, *LIBL, *CURLIB...
The command has exactly one parameter, the qualified name of the data queue to delete. This is the standard for nearly all Delete commands on the IBM i. The CRTDTAQ command, at least in its simplest form, isn't much more complex:
Data queue . . . . . . . . . . . Name
Library . . . . . . . . . . . *CURLIB Name, *CURLIB
Type . . . . . . . . . . . . . . *STD *STD, *DDM
Maximum entry length . . . . . . 1-64512
Force to auxiliary storage . . . *NO *NO, *YES
Sequence . . . . . . . . . . . . *FIFO *FIFO, *LIFO, *KEYED
The qualified data queue name is the same as on the Delete command. The type parameter can be either *STD or *DDM. I won't talk about DDM queues today except to point out that remote data queues are a really nifty way to add multi-tier support quickly.
But let me talk about the other couple of parameters. MAXLEN (Maximum entry length) is typically the only parameter I mess with; it identifies the maximum length of the largest message you plan to put on the queue. I nearly always have a single fixed length for my queue messages so my maximum length is usually the length of my messages, but that's not a requirement; data queues allow you to put a message of any length up to the maximum length onto the queue. Force to auxiliary storage is a sort of persistence mechanism; it makes sure that messages that get written stay written in the case of a system crash. Finally, you have the sequence. I usually use *FIFO, I never use *LIFO, and I occasionally use *KEYED. Each has its uses, but for today's discussion we'll stick with First-In, First-Out, or *FIFO. FIFO queues order their entries in arrival sequence: the first message to be pushed into a queue is the first to be popped off the stack by a read operation.
Note that after you populate this part of the command, you get the second half, which contains some more technical entries such as sender ID and queue size parameters. The parameters can be important, but I won't be covering them in this article; maybe I can get to them in a more advanced data queue article.
So Now It's Time to Read and Write!
Having created the queue using CRTDTAQ, it's now time to send some data and receive it. First let me set you up with some prototypes.
D SendData PR ExtPgm('QSNDDTAQ')
D Dtaqnam 10a const
D Dtaqlib 10a const
D Dtaqlen 5p 0 const
D Data const like(myMessage)
D ReceiveData PR ExtPgm('QRCVDTAQ')
D Dtaqnam 10a const
D Dtaqlib 10a const
D Dtaqlen 5p 0
D Data like(myMessage)
D WaitTime 5p 0 const
SendData and ReceiveData are the prototypes for the two primary APIs, QSNDDTAQ and QRCVDTAQ. Both of these APIs can have far more complex calls with many more parameters. But the parameters I've prototyped here are all that you need to use the basic functionality of a FIFO data queue. Technically, these prototypes would also work fine with a LIFO queue, but I don't often have a use for a LIFO queue (also known as a "stack"). Just remember that these are for non-keyed queues. You may have noticed that that I use "like(myMessage)" wherever I have to actually define the data. That's because I usually use the same size buffer for all the messages in a given application so that it's easy to define a field or data structure named myMessage and then use that in the prototype of the API call.
Having said all that, it's time to use the prototypes:
SendData( 'APPQ': 'APPLIB': %size(myMessage): myMessage);
This puts a message onto the data queue named APPQ in library APPLIB (which you would have already created with the CRTDTAQ command). Another program would then read that message, using this call:
ReceiveData( 'APPQ': 'APPLIB': lenReceived: myMessage: 60);
That's really all there is to it. Obviously, we could spend hours going over the various application infrastructure settings. In this case, I set the timeout for 60 seconds, but other applications might have shorter wait times or even no wait time at all. Or you could go to the other extreme and have a very long wait time. That's an application issue. Just remember to check the lenReceived variable; if it's zero, then you didn't get a message from the queue.
So, the purpose here wasn't to make you a data queue expert. It was to show you that just a couple of commands and a few lines of code are all you need to take advantage of data queues. In future articles, we'll take a look at keyed queues and how they are used (hint: think multi-line transactions) and other queue types as well, including user queues and message queues. Until then, keep on queuing!