Many applications depend on passing a number of different messages from one system to another. When you think of an application of this nature, the first thing that comes to mind is Web Services. The most common way to use Web Services is to have specific messages defined in XML passed to a server that processes the XML message. Some people consider using RMI or CORBA for an application dependent on messaging.
When you're thinking of adopting a particular technology, you must consider many things. Just a couple of those things should be the time it will take to educate yourself and others within your department as well as the cost of the technology and the overhead of running a particular technology within your environment. Depending on the development skills within your workshop, Web Services, RMI, and CORBA may be a lot to handle up front. However, if you have a development area that depends on the iSeries and the developers within your area are familiar with Java, you may want to consider IBM Toolbox for Java and the data queue implementation for passing messages.
IBM Toolbox for Java is a set of Java classes that allow you to write Java applications that interact with data on your iSeries. IBM Toolbox for Java uses the Host Servers to communicate with different iSeries objects.
Allow Multiple Clients to Pass Messages to Different Data Queues
I recently returned from a cruise. The cruise ship had an elevator to take passengers to different floors on the ship. The elevator attendant asked the passengers which floors they wanted to go to and then pressed the numbers for those floors on the elevator's keypad. This is a good analogy of the architecture that I am going to talk about.
The iSeries server has one main data queue that listens for clients that want to open communications and close communications. In order for the client to communicate with the server, an open session request message must be sent to the server. When the main data queue receives that request message from the client, it creates another data queue that the client uses for communications. On the server, a separate thread is created that listens to requests on the data queue that was created for that particular client. The server sends a response message to the client, informing the client which data queue it is supposed to communicate with. The client then sends every message to that data queue. Once the client is done communicating with the server, it sends a close session message, which deletes the data queue that the client was using and destroys the thread that was used to listen on that data queue.
You can think of the main data queue as the attendant who is waiting for the passengers to enter the elevator. The passengers are clients. Once the passengers successfully enter the elevator, they communicate on different data queues that are assigned by the attendant. When the passengers are ready to come off the elevator, they send a close session to the attendant, who deletes the data queue that the client was using along with the thread that was created for them.
The Open Session and Close Session Message and Response
The first thing that must be defined is the messages that will be passed to and from the server. In the past, developers created their own message formats and documented them in great detail. However, XML has evolved as the standard way of creating and passing messages between different systems. XML is also easy to read, which makes it convenient when debugging a problem.
The first message we will create is the Open-Session-Request message. This message will be sent from the client to the server's main data queue. Note that the main data queue will listen only for clients that want to open a session and close a session. Once the main data queue receives the Open-Session-Request, it creates another data queue for that particular client to communicate with. The server then dynamically creates a session ID that the client uses to talk to the data queue that was assigned. An Open-Session-Response will be created that will have the name of the data queue for that client to communicate with, along with a session ID.
The Open-Session-Request XML that will be used to send to the server will look like this:
The Open-Session-Response XML that the server will send to the client will look like this:
Here, the data-queue-communication-object is the name of the data queue that the client will use to communicate with, and session-id-value is dynamically created for that particular client.
Once the client receives the Open-Session-Response, it knows which data queue to communicate with and can send further messages to the server for processing. When the client is done, it sends a Close-Session-Request to the main data queue, which notifies the server that the client is done communicating. The Close-Session-Request sends the data queue that was created for communications within the Close-Session-Request. This way, the main data queue knows which data queue to remove and which thread to destroy. Once this is done, the server sends the Close-Session-Response to the client, which allows the client to know that communications have ended successfully.
The Close-Session-Request looks like this:
Here, data-queue-communication-object is the name of the data queue that the client used for communications with the server, and session-id-value is what the client was assigned to communicate with the server.
The Close-Session-Response that's used to notify the client that communications ended successfully looks like this:
More messages could be defined, but we will look only at the open session and close session messages.
Creating Additional Data Queues per Client
There are many advantages to using data queues when receiving messages from a client:
- Many clients can send messages to one single data queue.
- Messages on a data queue are free-format, meaning that a long string value can be sent.
- The data queue can be used for either synchronous or asynchronous processing.
- Messages within a data queue can be ordered as last-in first-out (LIFO), first-in first-out (FIFO), or keyed. When the data queue is marked as keyed, a message can be taken off the queue by specifying a single key.
To ensure that the correct clients are sending messages to the server, we will use the KeyedDataQueue. All clients that are distributed will have a keyed value that is used to communicate with the main data queue on the server. Clients that try to send messages to the data queue without the key will not be successful. By using a KeyedDataQueue, we have control over which clients are sending messages to our server.
The client must send an Open-Session-Request message to start communications with the server. Once the server receives an Open-Session-Request, it creates a keyed data queue to be used for communications by the client. The server also dynamically creates a session ID and sends this information back to the client in the Open-Session-Response. When a message is sent to a keyed data queue, a key must be sent along with it. We will use the session ID as the key for communications. Anytime a client wants to send another message after a session has been opened, it must use the session ID as the key. This is done to ensure that only that one single client can send and receive messages from that data queue. An example of this is shown in Figure 1.
Figure 1: The Open-Session-Request is sent to the server's main data queue, which starts a new thread for that client and creates a keyed data queue for only that client to communicate with. The server then informs the client of which keyed data queue to communicate with by sending an Open-Session-Response. (Click images to enlarge.)
When the client is done communicating, it sends a Close-Session-Request to the main data queue, which then deletes the data queue that was created for that client, and the separate thread that was used to monitor the client's queue is destroyed. This is shown in Figure 2.
Figure 2: The client sends a Close-Session-Request to the server's main data queue. Once it's received, the client's keyed data queue is removed and the thread the server was using to allow the client to communicate with is destroyed. A Close-Session-Response is sent to the client to indicate that communications have ended.
The More, the Merrier
As you can see, the more clients there are, the more data queues there are. Each client communicates with its own data queue that is monitored by its own server thread. If one particular client has a problem, it should not affect the other clients. This architecture also makes it easy to debug client problems because the data queue and thread of each client are independent.
Benjamin Garbers is a Software Engineer within the Rochester Support Center. He is currently developing Internet applications for IBM's iSeries Technical Web site.