Introduction:
A message queue is a queue of messages sent between applications. it is faster communication between client and server. MQ is the most popular messaging communication over the network. we can put and get the messages from the queue. once we get the message from the queue, it is removed from queue also. below the figure shows how can we put the messages into a queue and at the same time pull the messages from the queue.
Here is my sample java code to put messages into MQ
Explanation:
A message queue is a queue of messages sent between applications. it is faster communication between client and server. MQ is the most popular messaging communication over the network. we can put and get the messages from the queue. once we get the message from the queue, it is removed from queue also. below the figure shows how can we put the messages into a queue and at the same time pull the messages from the queue.
Here is my sample java code to put messages into MQ
package file2mq; import com.ibm.mq.MQC; import com.ibm.mq.MQEnvironment; 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 java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * * @author Janardhan Randhi */ public class File2Mq { public static void main(String[] args) throws IOException { String file = "D:\\Sample.txt"; //your file BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; StringBuilder stringBuilder = new StringBuilder(); String ls = System.getProperty("line.separator"); try { while ((line = reader.readLine()) != null) { stringBuilder.append(line); stringBuilder.append(ls); } String String1 = stringBuilder.toString(); System.out.println(String1); insertToQueue(String1); //return stringBuilder.toString(); } finally { reader.close(); } } public static void insertToQueue(String messageBody) { { ///Message to put on MQ. // String msg = "Connection successful with MQ"; ///Create a default local queue. String qMngrStr = "********";//QManagerName MQQueueManager qManager; MQQueue destQueue; int openOptions = MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INPUT_SHARED + MQC.MQOO_INQUIRE + MQC.MQOO_OUTPUT; //Set MQ connection credentials to MQ Envorinment. MQEnvironment.hostname = "000.00.00.000"; //host MQEnvironment.channel = "SYSTEM.DEF.SVRCONN"; MQEnvironment.port = 1414; String qName = "******"; //QName MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT); try { qManager = new MQQueueManager(qMngrStr); destQueue = qManager.accessQueue(qName, openOptions); // quedepth=(long) destQueue.getCurrentDepth(); MQMessage msg = new MQMessage(); try { msg.writeString(messageBody); } catch (Exception ex) { System.err.println("Exception in insertToQueue-->" + ex.getMessage()); } MQPutMessageOptions pmo = new MQPutMessageOptions(); destQueue.put(msg); System.out.println("ok"); destQueue.close(); qManager.disconnect(); } catch (MQException ex) { System.err.println("Exception in insertToQueue MQException-->" + ex.getMessage()); //ex.printStackTrace(); System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode + " Reason Code " + ex.reasonCode); } } } }
- Here I am creating the BufferReader instance for reading the file and the whole file read using the reader object and then convert into a string.
- Then create the method like insertToQueue pass the string as a parameter. here I am providing me details like QName, QManagername, port, host.
- Create the MQQueueManager instance with QManagername as a parameter.
- Using the access queue method to get the queue connection.
- Then create the MQMessage instance for writing the message into the queue.
- using the put method to put the message into the queue.
- Finally, disconnect the QManager.