There are a lot of MQ interfaces in the project, especially after entering the combined test, there are a lot of MQ input and withdrawal requirements. The implementation of MQ resource manager with Webshpere is too slow, and there are too many manual operations. Two tools have been written manually. One is VBA's interface file with fixed format to generate XML file for MQ interface test. One is a tool written in java that invests and fetches batch XML for MQ to generate XML files. Here we introduce the second, the first VBA tool can leave a message if necessary.
Thought: Because it's only a simple file reading and writing, not involving XML parsing, using apache's commons.io to read and write files; and using javax.jms and ibm.mq.allclient to read and write MQ. So in addition to the jdk1.8 library, you need three jar packages. com.ibm.mq.allclient.jar, commons-io-2.6.jar, javax.jms-api-2.0.1.jar.
Code implementation: MQCommon.java
public static ResourceBundle createResourceBundle() { // JMS setting file (mq.properties) ResourceBundle bundle = ResourceBundle.getBundle(JMS_PROPERTIES); return bundle; } public static MQQueueConnectionFactory createMQQueueConnectionFactory(ResourceBundle bundle) throws JMSException { // Configure QueueConnectionFactory. MQQueueConnectionFactory factory = new MQQueueConnectionFactory(); factory.setTransportType(com.ibm.mq.jms.JMSC.MQJMS_TP_CLIENT_MQ_TCPIP); factory.setHostName(bundle.getString(JMS_HOST)); factory.setPort(Integer.valueOf(bundle.getString(JMS_PORT))); factory.setChannel(bundle.getString(JMS_CHANNEL)); return factory; } public static QueueConnection createQueueConnection(MQQueueConnectionFactory factory) throws JMSException { String password = System.getProperty(MQ_Password_KEY, ""); String userId = System.getProperty(MQ_UserID_KEY, MQ_UserID); System.out.println("user[" + userId + "],password[" + password + "] for connection"); QueueConnection conn = factory.createQueueConnection(userId, password); return conn; } public static QueueSession createQueueSession(QueueConnection conn) throws JMSException { QueueSession session = conn.createQueueSession(true, QueueSession.AUTO_ACKNOWLEDGE); return session; } public static MQQueue createMQQueue( String queueName) throws JMSException { MQQueue mqQueue = new MQQueue(queueName); return mqQueue; }
MQPut.java
/** * @param args * 1st:QueueName(ex.inq001,outq001,errq001..) * 2nd: MessageFile's Path(Relative path) */ public static void main(String[] args) { int paramLength = args.length; String queueName = null; String fileName = null; String[] paramCheckResult = null; paramCheckResult = paramCheck(paramLength, args); queueName = paramCheckResult[0]; fileName = paramCheckResult[1]; MQPut.put( queueName, fileName); } public static void put(String queueName, String fileName) { File file = new File(fileName); ResourceBundle bundle = null; MQQueueConnectionFactory factory = null; MQQueue mqQueue = null; //clear object QueueSender sender = null; QueueSession session = null; QueueConnection conn = null; try { bundle = MQCommon.createResourceBundle(); factory = MQCommon.createMQQueueConnectionFactory(bundle); conn = MQCommon.createQueueConnection(factory); session = MQCommon.createQueueSession(conn); mqQueue = MQCommon.createMQQueue(queueName); MQCommon.info(factory, mqQueue); sender = createQueueSender(session, mqQueue); if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { // Read & Send Message. sendMessage(files[i], session, sender); } } else { // Read & Send Message. sendMessage(file, session, sender); } // mq commit. session.commit(); System.out.println("Put completed."); } catch (JMSException | RuntimeException e) { // mq rollback } finally { // close sender session conn } } private static String[] paramCheck(int paramLength, String[] args) { String[] paramCheckResult = new String[2]; switch (paramLength) { case 0: System.out.println("Please Data Input."); paramCheckResult[0] = ReadInputUtils.readInput("target queueName(ex.inq001,outq001,errq001..): "); paramCheckResult[1] = ReadInputUtils.readInput("target filepath(Relative path): "); break; case 1: System.out.println("Please Data Input."); paramCheckResult[0] = args[0]; paramCheckResult[2] = ReadInputUtils.readInput("target filepath(Relative path): "); break; default: paramCheckResult[0] = args[0]; paramCheckResult[1] = args[1]; break; } return paramCheckResult; } private static QueueSender createQueueSender(QueueSession session, MQQueue mqQueue) throws JMSException { QueueSender sender = session.createSender(mqQueue); return sender; } private static void sendMessage(File file, QueueSession session, QueueSender sender) { InputStream inFile = null; InputStreamReader innerReader = null; try { inFile = new FileInputStream(file); //String message = IOUtils.toString(inFile,"utf-8"); String message = IOUtils.toString(inFile,"utf-8"); // Send Message information. System.out.println("Put file :" + file.getName() ); System.out.println("Message :"); System.out.println(message); // Send Message. TextMessage tm; tm = session.createTextMessage(message); sender.send(tm); } catch (JMSException | IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(innerReader); IOUtils.closeQuietly(inFile); } }
MQView.java
/** @param args * 1st:QueueName(ex.inq001,outq001,errq001..) * 2nd: MessageDirectory Path(Relativepath) * 3rd:Extension(ex.txt,xml..), * 4th:DeleteOption(defalt false without input) */ public static void main(String[] args) { int paramLength = args.length; String queueName = null; String dirPath = null; String extension = null; boolean deleteOption = false; Object[] paramCheckResult = new Object[4]; paramCheckResult = paramCheck(paramLength, args); queueName = (String) paramCheckResult[0]; dirPath = (String) paramCheckResult[1]; extension = (String) paramCheckResult[2]; deleteOption = (boolean) paramCheckResult[3]; MQView.view(queueName, dirPath, extension, deleteOption); } public static String view(String queueName, String dirPath, String extension, boolean deleteOption) { ResourceBundle bundle = null; MQQueueConnectionFactory factory = null; MQQueue mqQueue = null; String mkDirPath = null; int i = 0; // clear object QueueReceiver receiver = null; QueueSession session = null; QueueConnection conn = null; try { bundle = MQCommon.createResourceBundle(); factory = MQCommon.createMQQueueConnectionFactory(bundle); conn = MQCommon.createQueueConnection(factory); session = MQCommon.createQueueSession(conn); mqQueue = MQCommon.createMQQueue(queueName); MQCommon.info(factory, mqQueue); receiver = createQueueReceiver(session, mqQueue); conn.start(); // loop for message in queue mkDirPath = createDirectory(dirPath); while (true) { TextMessage textMessage = (TextMessage) receiver.receiveNoWait(); if (textMessage == null) { System.out.println("All message output completed"); break; } writeFile(i, queueName, extension, mkDirPath, textMessage); i++; } System.out.println( "Wrote " + i + "files in :" + mkDirPath); // delete object if (deleteOption) { // mq commit. } else { // mq rollback } } catch (JMSException | IOException | RuntimeException e) { // mq rollback } finally { // close receiver session conn } return mkDirPath; } private static void writeFile(int i, String queueName, String extension, String dirName, TextMessage textMessage) throws JMSException, IOException { String fileName = queueName + "_" + (i + 1) + "." + extension; String filePath = dirName + "/" + fileName; File mkFile = new File(filePath); mkFile.createNewFile(); BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(filePath,true),"UTF-8")); String message = textMessage.getText(); try { fileWriter.write(message); System.out.println(fileName + " wrote."); } catch (IOException e1) { throw new IOException(); } finally { fileWriter.close(); } }
Encountered pits:
1. To read and write utf8 encoding with commons.io, special specification is needed, otherwise it will be read according to the default encoding.
//Read the utf8 file inFile = new FileInputStream(file); String message = IOUtils.toString(inFile,"utf-8"); //Write utf8 files BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(filePath,true),"utf-8")); fileWriter.write(message);
2. If MQ is taken out, commit should also be clearly written, otherwise it will still exist after reading.