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
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
DEF QL(STOCK_REQUEST)
DEF QL(STOCK_RESPONSE)Running the application
.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 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_RESPONSEWaiting 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
MQ Explorer to connect to your
MQ queue manager on z/OS and
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 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.