Someone over at Stack Overflow posted a question about logging MQ error messages to a file. After the user updated the posting, I recognized the code as mine (MQWrite.java) that I wrote back in 2000 (Yup, 16 years ago!!).
So, I decided to bring MQWrite.java into this decade and created MQWrite2.java. Here is MQWrite2.java that sets the connection information in a HashTable and uses Log4J as the logger. (You can download it from here.)
import java.io.IOException; import java.util.Hashtable; import org.apache.log4j.Logger; 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; /** * Program Name * MQWrite2 * * Description * This java class will connect to a queue manager and * and write a message to a queue. * * Sample Command Line Parameters * -h 127.0.0.1 -p 1414 -c TEST.CHL -m MQA1 -q TEST.Q1 * * @author Roger Lacroix, Capitalware Inc. */ public class MQWrite2 { private Logger logger = Logger.getLogger(MQWrite2.class.getName()); private Hashtable<String, String> params = null; private Hashtable<String, Object> mqht = null; private String qMgrName; private String outputQName; /** * The constructor */ public MQWrite2() { super(); logger.info("MQWrite2 is now starting."); } /** * 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"); 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 variables. * @param args * @throws IllegalArgumentException */ private void init(String[] args) throws IllegalArgumentException { params = new Hashtable<String, String>(); 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"); mqht = new Hashtable<String, Object>(); mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c")); mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h")); try { mqht.put(CMQC.PORT_PROPERTY, new Integer(params.get("-p"))); } catch (NumberFormatException e) { mqht.put(CMQC.PORT_PROPERTY, new Integer(1414)); } // 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 testReceive() { int openOptions = CMQC.MQOO_OUTPUT + CMQC.MQOO_FAIL_IF_QUIESCING; MQPutMessageOptions pmo = new MQPutMessageOptions(); MQQueueManager _qMgr = null; MQQueue queue = null; MQMessage sendMsg = null; String line = "This is a test message."; try { _qMgr = new MQQueueManager(qMgrName, mqht); queue = _qMgr.accessQueue(outputQName, openOptions); sendMsg = new MQMessage(); sendMsg.messageId = CMQC.MQMI_NONE; sendMsg.correlationId = CMQC.MQCI_NONE; sendMsg.writeString(line); // put the message on the queue queue.put(sendMsg, pmo); logger.info("MQWrite2 Message inserted: " + line); } catch (MQException mqex) { logger.error("MQWrite2 MQException CC=" +mqex.completionCode + " : RC=" + mqex.reasonCode); } catch (IOException e) { logger.error("MQWrite2 IOException " + e.getLocalizedMessage()); } finally { try { if (queue != null) queue.close(); } catch (MQException mqex2) { logger.error("MQWrite2 MQException CC=" +mqex2.completionCode + " : RC=" + mqex2.reasonCode); } finally { try { if (_qMgr != null) _qMgr.disconnect(); } catch (MQException mqex3) { logger.error("MQWrite2 MQException CC=" +mqex3.completionCode + " : RC=" + mqex3.reasonCode); } } } logger.info("MQWrite2 is now ending."); } /** * main line * @param args */ public static void main(String[] args) { MQWrite2 write = new MQWrite2(); try { write.init(args); write.testReceive(); } catch (IllegalArgumentException e) { System.out.println("Usage: java MQWrite2 -h host -p port -c channel -m QueueManagerName -q QueueName"); System.exit(1); } System.exit(0); } }
And here is the ‘log4j.properties’ file that goes with the code (it is included in the download):
# # stdout # log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%m%n # # MQWrite # log4j.category.MQWrite2=DEBUG, mqwrite, stdout #log4j.category.MQWrite2=INFO, mqwrite, stdout # "mqwrite" appender writes to a file log4j.appender.mqwrite=org.apache.log4j.RollingFileAppender log4j.appender.mqwrite.File=log/MQWrite.log log4j.appender.mqwrite.MaxFileSize=1000KB log4j.appender.mqwrite.MaxBackupIndex=9 log4j.appender.mqwrite.layout=org.apache.log4j.PatternLayout log4j.appender.mqwrite.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p - %m%n #log4j.appender.mqwrite.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %p (%F:%M:%L) - %m%n
Make sure you create a ‘log’ directory under the directory where you run the code from or else it will throw an error like:
log4j:ERROR setFile(null,true) call failed. java.io.FileNotFoundException: log\MQWrite.log (The system cannot find the path specified)
Regards,
Roger Lacroix
Capitalware Inc.