package com.wcsadmin.utilities.mq /** * @author Sahadev Komaragiri **/ import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import com.ibm.mq.*; public class ReadWriteMessage { private String qManager; private String sendQueueName; private String replyQueueName; private String responseMessageFile; public static void main(String args[]) { ReadWriteMessage gM = new ReadWriteMessage(); gM.qManager = args[0]; gM.sendQueueName = args[1]; gM.replyQueueName = args[2]; gM.responseMessageFile = args[3]; gM.runNow(); } public void runNow() { MQQueueManager qMgr = null; MQQueue queue = null; try { // Create a connection to the QueueManager System.out.println("Connecting to queue manager: "+qManager); qMgr = new MQQueueManager(qManager); // Set up the options on the queue we wish to open int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; // Now specify the queue that we wish to open and the open options System.out.println("Accessing queue: " + sendQueueName); queue = qMgr.accessQueue(sendQueueName, openOptions, null, null, null); // Get the message off the queue. String msgText = ""; MQMessage rcvMessage = new MQMessage(); queue.get(rcvMessage); byte[] b = new byte[rcvMessage.getMessageLength()]; rcvMessage.readFully(b); msgText = new String(b); System.out.println("Read Request Message: " + msgText); byte[] messageId = rcvMessage.messageId; writeResponse(messageId); } catch (MQException ex) { int MQRC_NO_MSG_AVAILABLE = 2033; if (ex.reasonCode == MQRC_NO_MSG_AVAILABLE) { System.out.println("No message on the specified queue: " + sendQueueName ); } else { System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode + " Reason Code " + ex.reasonCode); } } catch (java.io.IOException ex) { System.out.println("An IOException occured while reading to the message buffer: " + ex); } catch (Exception ex) { System.out.println("Some other exception!: " + ex); } finally { try { System.out.println("Closing the queue"); queue.close(); System.out.println("Disconnecting from the Queue Manager"); qMgr.disconnect(); System.out.println("Done!"); } catch(Exception e) { System.out.println("Error in finally!"); } } } private void writeResponse(byte[] correlationId) throws Exception { // Create a connection to the QueueManager System.out.println("Connecting to queue manager: "+qManager); MQQueueManager qMgr = new MQQueueManager(qManager); // Set up the options on the queue we wish to open int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT; // Now specify the queue that we wish to open and the open options System.out.println("Accessing queue: "+replyQueueName); MQQueue queue = qMgr.accessQueue(replyQueueName, openOptions); // Define a simple WebSphere MQ Message ... File file = new File(responseMessageFile); String qmessage = getContents(file); MQMessage msg = new MQMessage(); msg.correlationId = correlationId; msg.writeString(qmessage); // Specify the default put message options MQPutMessageOptions pmo = new MQPutMessageOptions(); // Put the message to the queue System.out.println("Sending Reply Message: " + qmessage); queue.put(msg, pmo); // Close the queue System.out.println("Closing the reply queue"); queue.close(); System.out.println("Disconnecting the reply queue - queue manager"); qMgr.disconnect(); } private String getContents(File aFile) { StringBuffer contents = new StringBuffer(); //declared here only to make visible to finally clause BufferedReader input = null; try { input = new BufferedReader( new FileReader(aFile) ); String line = null; //not declared within while loop while (( line = input.readLine()) != null) { contents.append(line); contents.append(System.getProperty("line.separator")); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (input!= null) { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return contents.toString(); } }