This is the third post regarding JMS (MQRFH2) messages in non-JMS application. The previous 2 are:
- Handling A JMS (MQRFH2) Message in non-JMS Java Application
- JMS (MQRFH2) Message But The User Sees Garbage
If you want to create JMS messages then the simplest solution is to use the IBM MQ classes for JMS. In case you didn’t know, you can run MQ/JMS applications in stand-alone mode rather than using a J2EE server (i.e. WAS, WebLogic, JBoss, etc..). Here are 2 links from the MQ Knowledge Center to get you started:
There are cases where people don’t want to create a JMS application to put JMS messages to an MQ queue. IBM doesn’t have any really good Java coding samples that demonstrate the creation of an JMS message using the MQRFH2 class. In this blog posting, I’ll show you a fully functioning non-JMS Java example that creates a JMS messages using the MQRFH2 class.
You can download the source code from here.
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Hashtable; import com.ibm.mq.MQException; import com.ibm.mq.MQMessage; import com.ibm.mq.MQPutMessageOptions; import com.ibm.mq.MQQueue; import com.ibm.mq.MQQueueManager; import com.ibm.mq.constants.CMQC; import com.ibm.mq.headers.MQRFH2; /** * Program Name * MQTest71 * * Description * This java class will connect to a remote queue manager with the * MQ setting stored in a HashTable and put a JMS message * (aka MQRFH2 message) on a queue. * * Sample Command Line Parameters * -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -q TEST.Q1 -u UserID -x Password * * @author Roger Lacroix */ public class MQTest71 { private static final SimpleDateFormat LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"); private Hashtable<String,String> params; private Hashtable<String,Object> mqht; private String qMgrName; private String outputQName; /** * The constructor */ public MQTest71() { super(); params = new Hashtable<String,String>(); mqht = new Hashtable<String,Object>(); } /** * Make sure the required parameters are present. * @return true/false */ private boolean allParamsPresent() { boolean b = params.containsKey("-h") && params.containsKey("-p") && params.containsKey("-c") && params.containsKey("-m") && params.containsKey("-q") && params.containsKey("-u") && params.containsKey("-x"); if (b) { try { Integer.parseInt((String) params.get("-p")); } catch (NumberFormatException e) { b = false; } } return b; } /** * Extract the command-line parameters and initialize the MQ HashTable. * @param args * @throws IllegalArgumentException */ private void init(String[] args) throws IllegalArgumentException { int port = 1414; if (args.length > 0 && (args.length % 2) == 0) { for (int i = 0; i < args.length; i += 2) { params.put(args[i], args[i + 1]); } } else { throw new IllegalArgumentException(); } if (allParamsPresent()) { qMgrName = (String) params.get("-m"); outputQName = (String) params.get("-q"); try { port = Integer.parseInt((String) params.get("-p")); } catch (NumberFormatException e) { port = 1414; } mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c")); mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h")); mqht.put(CMQC.PORT_PROPERTY, new Integer(port)); mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u")); mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x")); // I don't want to see MQ exceptions at the console. MQException.log = null; } else { throw new IllegalArgumentException(); } } /** * Connect, open queue, write a message, close queue and disconnect. * */ private void testSend() { MQQueueManager qMgr = null; MQQueue queue = null; String msgData = "This is a test message from MQTest71"; int openOptions = CMQC.MQOO_OUTPUT + CMQC.MQOO_FAIL_IF_QUIESCING; MQPutMessageOptions pmo = new MQPutMessageOptions(); try { qMgr = new MQQueueManager(qMgrName, mqht); MQTest71.logger("successfully connected to "+ qMgrName); queue = qMgr.accessQueue(outputQName, openOptions); MQTest71.logger("successfully opened "+ outputQName); MQMessage sendmsg = new MQMessage(); // Set the RFH2 Values MQRFH2 rfh2 = new MQRFH2(); rfh2.setEncoding(CMQC.MQENC_NATIVE); rfh2.setCodedCharSetId(CMQC.MQCCSI_INHERIT); rfh2.setFormat(CMQC.MQFMT_STRING); rfh2.setFlags(0); rfh2.setNameValueCCSID(1208); /** * First, set the 'Message Service Domain' value. * Valid values for mcd.Msd are: * jms_none, jms_text, jms_bytes, jms_map, jms_stream & jms_object */ rfh2.setFieldValue("mcd", "Msd", "jms_text"); /** * Set the destination JMS queue name. */ rfh2.setFieldValue("jms", "Dst", "queue:///"+outputQName); /** * Set user values. */ rfh2.setFieldValue("usr", "SomeNum", 123); rfh2.setFieldValue("usr", "SomeText", "TEST"); // Set the MQRFH2 structure to the message rfh2.write(sendmsg); // Write message data sendmsg.writeString(msgData); // Set MQMD values sendmsg.messageId = CMQC.MQMI_NONE; sendmsg.correlationId = CMQC.MQCI_NONE; sendmsg.messageType = CMQC.MQMT_DATAGRAM; // IMPORTANT: Set the format to MQRFH2 aka JMS Message. sendmsg.format = CMQC.MQFMT_RF_HEADER_2; // put the message on the queue queue.put(sendmsg, pmo); MQTest71.logger("Message Data>>>" + msgData); } catch (MQException e) { MQTest71.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode); } catch (IOException e) { MQTest71.logger("IOException:" +e.getLocalizedMessage()); } finally { try { if (queue != null) { queue.close(); MQTest71.logger("closed: "+ outputQName); } } catch (MQException e) { MQTest71.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode); } try { if (qMgr != null) { qMgr.disconnect(); MQTest71.logger("disconnected from "+ qMgrName); } } catch (MQException e) { MQTest71.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode); } } } /** * A simple logger method * @param data */ public static void logger(String data) { String className = Thread.currentThread().getStackTrace()[2].getClassName(); // Remove the package info. if ( (className != null) && (className.lastIndexOf('.') != -1) ) className = className.substring(className.lastIndexOf('.')+1); System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data); } /** * main line * @param args */ public static void main(String[] args) { MQTest71 write = new MQTest71(); try { write.init(args); write.testSend(); } catch (IllegalArgumentException e) { MQTest71.logger("Usage: java MQTest71 -m QueueManagerName -h host -p port -c channel -q QueueName -u UserID -x Password"); System.exit(1); } System.exit(0); } }
As I said above, you can download the source code from here.
There are some things to note:
- The order of items written to the MQMessage is very important. The MQRFH2 must be FIRST followed by the application’s message data.
- A JMS message needs BOTH an ‘mcd’ and ‘jms’ folders.
- The application MUST set the MQMD Format field of the message to CMQC.MQFMT_RF_HEADER_2 otherwise it is left to its default value (blank), hence, it will NOT be a JMS message.
- If the application needs to set their own properties, then you store them in the ‘usr’ folder.
If you follow my instructions and/or use my sample Java program above then you will be able to create JMS messages in a non-JMS Java application.
Regards,
Roger Lacroix
Capitalware Inc.
2 Responses to Creating A JMS (MQRFH2) Message in non-JMS Java Application