WebSphere MQ is IBM's flagship product for providing reliable inter-platform messaging, and accessing MQ from RPG is easy once you understand the techniques.
I have a hard time with the rebranding of IBM's MQSeries to WebSphere MQ; it's always seemed to me to be one of the more random renamings. MQSeries had great brand recognition as it was. But regardless of the name, MQ has always provided a powerful queuing architecture, and I guess a rose by any name holds true here. The purpose of this article is not to bemoan the naming but to introduce the architecture to RPG programmers.
Why MQ?
Given my last three articles on data queues, you might wonder why I'm even writing about WebSphere MQ. The primary reason is that MQ Series is the next step in architecture after data queues. Without going into a point-by-point comparison, MQ provides a number of industrial-strength capabilities that aren't available in data queues. You could add them yourself, but with WebSphere MQ all of the coding is done for you. This doesn't come for free; WebSphere MQ is an added expense and not a cheap one, depending on the platform. But the beauty is that once you learn it on one platform, you can use it on any platform.
RPG vs. Java
Since WebSphere MQ is a multi-platform product, your best access is probably through Java. However, if your primary area of concern is writing programs on the IBM i that interface with WebSphere MQ and you aren't already Java proficient, then this article will give you a little help in how to access the product. I will take the opportunity to once again briefly evangelize for Java proficiency; it opens up so many other worlds for an RPG programmer that it's simply a no-brainer that Java be your next language. But we can discuss that another day.
MQ Basics
So let's get down to basics. In MQ, you have three basic components: the queue manager, the queue, and the message. Here's the relationship:
Figure 1: This is the relationship of the primary objects in a WebSphere MQ setup.
The queue manager provides the coordination and servicing of the queues. This includes delivery, trigger and instrumentation events, creation and deletion, holding and releasing, and all of the other management functions. Also, the queue manager is the primary unit of "location"; a queue manager is associated with a physical machine. Any messages destined for a queue for another queue manager must be routed through a remote queue. A remote queue on one queue manager is tied to a local queue on another queue manager. Figure 1 shows some of these relationships. Note that technically the remote queue manager can even be on the same machine as the local queue manager; in this manner, the applications can be segregated while still running on the same machine; this allows portions of the application to be distributed throughout the network as necessary with no changes to the application code.
Note that this is only one of a variety of configurations available. It would take a whole series of articles to address various issues such as channels, clusters, namelists, publish and subscribe, predefined and dynamic queues, transaction control, dead letter queues, and so on.
Accessing the Queues
The simplest type of communication is the asynchronous put, also known as "fire and forget." For an asynchronous put, there are five basic steps:
- 1.Connect to a queue manager.
- 2.Open a queue.
- 3.Put (send) a message.
- 4.Close the queue.
- 5.Disconnect from the queue manager.
All of this is done via simple calls to the LIBMQM service program in library QMQM. The calls are relatively simple and very consistent. We only need a few variables:
d mq_HCONN s 10i 0
d mq_OPTS s 10i 0
d mq_HOBJ s 10i 0
d mq_OCODE s 10i 0
d mq_CCODE s 10i 0
d mq_REASON s 10i 0
d mq_QMNAME s 48
d mq_QNAME s 48
Here's the call to connect to a queue manager:
MQCONN( mq_QMNAME: mq_HCONN: mq_OCODE: mq_REASON);
The variable mq_QMNAME contains the name of the queue manager. WebSphere MQ allows up to 48 characters for the name of the queue manager and, as we'll see in a moment, another 48 characters for the name of the queue. You can see that mq_HCONN is a simple integer used to identify the connection. This value is called a "handle," hence the H in HCONN. The value is meaningless to the calling application and is only provided to pass back into other APIs to identify the queue manager. Later in fact you'll use that handle to open a queue in the queue manager:
MQOPEN( mq_HCONN: MQOD: mq_OPTS: mq_HOBJ: mq_OCODE: mq_REASON);
To open a queue, we call MQOPEN, passing in the connection handle, the queue descriptor (MQOD), and some options (mq_OPTS), and getting back a handle to the queue object (mq_HOBJ). You'll notice that the last two parameters for both calls are two more integers, the completion code (mq_OCODE) and the reason code (mq_REASON). Typically, both the completion code and the reason code should be zero for the action to be considered to have completed successfully. This is the standard reporting procedure for all of the WebSphere MQ APIs and one you should be familiar with.
The completion code is OK, warning, or failure. For anything other than OK, the reason code will clarify the actual error that occurred. You can go here for a list of the reason codes and explanations of each. The rest of the code consists of very similar calls to WebSphere MQ APIs:
MQPUT( mq_HCONN: mq_HOBJ: MQMD: MQPMO:
BufferLen: BufferPtr: mq_CCODE: mq_REASON);
MQCLOSE( mq_HCONN: mq_HOBJ: mq_OPTS: mq_CCODE: mq_REASON);
MQDISC( mq_HCONN: mq_OCODE: mq_REASON);
The MQPUT API has a few extra parameters: the handles for the connection and the queue, followed by the description and options for the message, the buffer length and pointer, and then finally the completion code and reason code. MQCLOSE has the two handles and some options, followed by the standard method. MQDISC is even simpler: the handle to the queue manager you're closing and the completion code and reason code. So as you can see, one of the most consistent actions in communicating with WebSphere MQ is to check the completion code and reason code. In my code, I have a standard routine that does that and logs any errors.
The get sequence is almost identical; connecting to a queue manager, opening a queue, and closing the queue and queue manager are all required for the get just as for the put. The only difference is that there are quite a few more options on the get, from the idea of a timeout value to the concept of browsing versus actually popping something off the stack. Maybe when we get a chance we can spend little more time on the APIs themselves, including the options. But for now, this is a quick glimpse into working with WebSphere MQ. Thanks for reading!
LATEST COMMENTS
MC Press Online