Java code for MQ Read/Write

While working on MQ Integration on any commerce project, you typically do not have the luxury of testing out MQ send/reply functionality from your development environment. I have MQ Server installed on my development environment and I try and test my send/reply functionality as close to the real environment as possible. I create the queues and queue managers that mimic the server environment. In such cases, when I have to test a functionality that requires sending a message onto the queue and reading a response, I have a few options. One of them is to work with RHF utility I discussed earlier. The other option is to have a simple java problem that behaves like a consumer of my mq message and responds to it. This post discusses the Java Code that I put together to do this.There is a third option which is to write a Message Driven Bean and attach it to the queue using a corresponding listener. This option never worked for me from commerce end, as there were issues with overlapping MQ transactions.

Here is the source of this java program: ReadWriteMessage.java

To compile this program, you will need {MQ_INSTALL_ROOT}\Java\lib\com.ibm.mq.jar in your classpath. Compile and put this class in an appropriate jar file - I called it MQUtils.jar. You can use the following as an example to run this program:

java.exe -classpath {MQ_INSTALL_ROOT}\Java\lib\com.ibm.mq.jar;./MQUtils.jar com.wcsadmin.utilities.mq.ReadWriteMessage {QM_Name}{Receive_Queue_Name} {Response_Queue_Name} {Path_To_The_Response_File}

Here is how I used this program:
1. The message is placed on the queue by my code.
2. As soon as the message is placed on the queue, I have to run this program to respond to the message. I can run this program from within my WSAD/RAD environment.
3. This program consumes the message from the specified queue and responds with the contents of a file that you specify. The contents of the response file are critical to test your program efficiently.

Once you specify the arguments to this program in the RAD Run Environment, you will be able to run this command easily. If you are using RAD to run this program, all you have to do is add the com.ibm.mq.jar to the classpath of the project that hosts this program and then specify the run time arguments.

Did this help you?

3 Comments

  1. Abhiyendra said,

    September 4, 2007 at 9:26 am

    Hello,
    I need some help, I am in scenario where I have to look for attachments(image attachments) in the message and in case attachment found I have to save the attachment in a specific folder.

    Thanks
    Abhi

  2. Avis said,

    March 20, 2009 at 4:21 pm

    When working with MQ, how to read email messages from the Queue?

  3. srilatha said,

    June 26, 2009 at 9:49 am

    I am also working with MQ , how to read Message from MQueue.