Java Code to Put a Message on MQ Queue

When we use WebSphere MQ on a Commerce project, sometimes we have a need to put a message on the queue for testing inbound or outbound message functionality. In most cases we can get away by putting a message on the queue “manually” i.e. open the relevant queue in MQ Explorer and put a message. But there is a limitation on the size of the message which cannot exceed 1000 characters. This topic discusses some alternatives and a simple java program that can be used to put messages on the queue.

Basic MQ installation has some sample PUT programs that can be used to put a message on the queue. One such sample program is AMQSPUTC. But the out-of-box program has a limitation of 100 characters on the message size. This sample program is compiled from AMQSPUT0.c which is available in the samples folder in MQ installation home. If you have the patience to alter and recompile this program, open the AMQSPUT0.c and look for the line buffer[100] and change it appropriately. I did not have the patience to recompile a C program. I utilized the sample java program from MQ installation and extended it to write my own code.  The advantage of writing my own code in java: I can convert this into a command that I can schedule using a Commerce scheduler or simply reuse this code wherever I can. My program takes the parameters of a queue name, a queue manager and an input file.

To compile this program you will need <MQ_HOME>\Java\lib\com.ibm.mq.jar file in your classpath. Compile and put this class in an appropriate jar file - I called it MQUtils.jar. Use the following sample to run the program:

java.exe -classpath <MQ_HOME>\Java\lib\com.ibm.mq.jar;./MQUtils.jar com.wcsadmin.utilities.mq.PutMessage QM_Name Queue_Nam Path_To_Filename

The contents of the file itself need not be on one line.

Here is the source of my program: PutMessage.java

Leave a Comment