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 get the messages from the queue.
Here is my sample java code to get the messages from MQ.
Explanation:
1. First, create the MQEnvironment instance for adding the details of MQ.
2. Next, create the MQQueueManager instance and pass the MQQueueManager name to get the connection.
3. MQGetMessageOptions is used to access the messages from MQ.
4. Use the access queue method to connect the queue.
5. Use the getCurrentDepth method to the current depth of the queue.
6. Use the get method to get the messages from MQ.
7. Finally, close the queue and Qmanager.
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 get the messages from the queue.
package file2mq; import com.ibm.mq.MQC; import com.ibm.mq.MQEnvironment; import com.ibm.mq.MQException; import com.ibm.mq.MQGetMessageOptions; import com.ibm.mq.MQMessage; import com.ibm.mq.MQQueueManager; import java.io.IOException; /** * * @author Janardhan ram Randhi * Description :This class get the messages from queue. */ public class PullMessageFromMq { public static void main(String[] args) throws IOException { long quedepth = 0; String qMngrStr = "***********";//QManagerName MQQueueManager qManager; com.ibm.mq.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 = "**********"; //host MQEnvironment.channel = "SYSTEM.DEF.SVRCONN"; MQEnvironment.port = 1414; String qName = "*****"; //QName MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT); MQGetMessageOptions getOptions = new MQGetMessageOptions(); getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_CONVERT; try { qManager = new MQQueueManager(qMngrStr); destQueue = qManager.accessQueue(qName, openOptions); quedepth = (long) destQueue.getCurrentDepth(); MQMessage msg = new MQMessage(); destQueue.get(msg, getOptions); if (quedepth >= 0) { byte[] b = new byte[msg.getMessageLength()]; msg.readFully(b); System.out.println(new String(b)); msg.clearMessage(); 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); } } }
1. First, create the MQEnvironment instance for adding the details of MQ.
2. Next, create the MQQueueManager instance and pass the MQQueueManager name to get the connection.
3. MQGetMessageOptions is used to access the messages from MQ.
MQGetMessageOptions getOptions = new MQGetMessageOptions(); getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING + MQC.MQGMO_CONVERT;
5. Use the getCurrentDepth method to the current depth of the queue.
6. Use the get method to get the messages from MQ.
7. Finally, close the queue and Qmanager.