Prepare the sample IBM MQ stock query application

This scenario describes a simple stock query application, which is written in Java™, that uses the Java Message Service (JMS) API to interact with IBM® MQ. This application is used by the z/OS® Connect scenarios to demonstrate two-way services with the IBM MQ service provider.

zosConnect-2.0 Applies to zosConnect-2.0.

The stock query application takes request messages from an IBM MQ queue. The messages contain the ID of the item that the caller wants information on. The application then generates a response message that contains information on the requested item and sends it to a response queue. The requesting application takes the response message from the response queue.

Compiling the application

To compile the application, copy the following sample code for the stock query application to a file called TwoWayBackend.java on a UNIX System Services environment on a z/OS LPAR. Where, IBM MQ for z/OS is installed, including the IBM MQ for z/OS UNIX Systems Services Components feature .
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.TextMessage;
import com.ibm.mq.jms.MQConnectionFactory;

/**
 * Simple back end application which generates responses to stock queries.
 */
public class TwoWayBackend {
    public static void main(String[] args) {
        String queueManagerName = args[0];
        String requestQueueName = args[1];
        String responseQueueName = args[2];
		
        try {
            //Connect to queue manager.
            MQConnectionFactory cf = new MQConnectionFactory();
            cf.setQueueManager(queueManagerName);
			
            JMSContext ctx = cf.createContext();
			
            //Connect to request queue.
            JMSConsumer consumer = ctx.createConsumer(ctx.createQueue(requestQueueName));
			
            //Create JMS producer.
            JMSProducer producer = ctx.createProducer();
			
            //Create response queue.
            Queue responseQueue = ctx.createQueue(responseQueueName);
			
            //Loop round getting messages, and generating a response.			
            while(true) {
                System.out.println("Waiting for message. Press Ctrl+C to exit.");
				
                TextMessage requestMessage = null;
        
                //This loop provides a succinct way of breaking out of the loop by pressing Ctrl+C.
                while(requestMessage == null) {
                    requestMessage = (TextMessage) consumer.receiveNoWait();
                    if (requestMessage == null) {
                        Thread.sleep(1000);
                    }
                }
				
                //Request message payload is a 6 digit integer representing an item id.
                String itemID = requestMessage.getText().trim();

                System.out.println("Message received: " + itemID + ".");
				
                //Response message is a 6 digit integer item id, 
                //followed by a 20 character description,
                //a 6 digit integer item count and a 
                //a 6 digit integer item cost.
				
                //Return a hard coded response body, with the itemID inserted.
                String responseMessagePayload = String.format("%sA description.      0005000000045", itemID); 
				
                Message responseMessage = ctx.createTextMessage(responseMessagePayload);
				
                //Response messages have the correlation id set to the message id of the request message.
                responseMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
				
                //Send response.
                producer.send(responseQueue, responseMessage);		
         }
      }
      catch(Exception e) {
         System.out.println("Caught exception ");
         e.printStackTrace();
      }
    }
}

Using a Java 7.1 or later Java SE Development Kit, enter the following command to compile the application:

javac -cp /usr/lpp/mqm/V9R1M0/java/lib/com.ibm.mq.allclient.jar TwoWayBackend.java

If necessary, replace /usr/lpp/mqm/V9R1M0/ with the path to the installation of the IBM MQ for z/OS UNIX Systems Services Components feature.

Defining the queues for the application

Work with your IBM MQ administration to define a pair of queues for use by the stock query application. Both a request and a response queue are needed. For example, to define a request queue that is called STOCK_REQUEST and a response queue that is called STOCK_RESPONSE enter the following MQSC commands:
DEF QL(STOCK_REQUEST)
DEF QL(STOCK_RESPONSE)

Running the application

To run the stock query application, first ensure that the UNIX System Services STEPLIB includes the IBM MQ SCSQAUTH and SCSQANLE libraries. Specify these libraries in the startup JCL or using the .profile file. From UNIX and Linux® System Services, you can include these libraries by adding the following line to your .profile. Replace thlqual with the high-level data set qualifier that you chose when installing IBM MQ:

export STEPLIB=thlqual.SCSQAUTH:thlqual.SCSQANLE:$STEPLIB
For more information, see A launch icon to indicate a link opens a new tab or window. STEPLIB configuration for IBM MQ classes for JMS on z/OS in the IBM MQ documentation.
Enter the following command from UNIX System Services, replacing MQ21 with the name of the queue manager where the STOCK_REQUEST and STOCK_RESPONSE queues were defined. If necessary, replace both instances of /usr/lpp/mqm/V9R1M0/ with the installation path of the IBM MQ for z/OS UNIX System Services Components feature.
java -cp /usr/lpp/mqm/V9R1M0/java/lib/com.ibm.mq.allclient.jar:.  -Djava.library.path=/usr/lpp/mqm/V9R1M0/java/lib/ TwoWayBackend  MQ21 STOCK_REQUEST STOCK_RESPONSE
The following output is displayed.
Waiting for message. Press Ctrl+C to exit.
If a problem occurs, exception information is displayed that you can use to analyze the problem.

To stop the application, enter Ctrl+C in the command window where the application was run.

Testing the application

Use A launch icon to indicate a link opens a new tab or window. MQ Explorer to connect to your A launch icon to indicate a link opens a new tab or window. MQ queue manager on z/OS and A launch icon to indicate a link opens a new tab or window. send a message to the STOCK_REQUEST queue. Use a message payload value that consists of a six-digit integer, left-padded with zeros. For example, 001234.

The following output is displayed by the stock query application:

Waiting for message. Press Ctrl+C to exit. Message received: 001234 Waiting for message. Press Ctrl+C to exit.

If a problem occurs, exception information is displayed that you can use to analyze the problem.

You can now use IBM MQ Explorer to A launch icon to indicate a link opens a new tab or window.browse the response message on the STOCK_RESPONSE queue.

What to do next

Follow the steps in Create a server to connect to IBM MQ to create a connection to the IBM MQ queue manager.