View all messages from WebSphere MQ at a time using Java

How can we view all messages in a WebSphere MQ queue in a single API call using java?

Here is the code I'm using. Here I use this for loop code until q is reached.

  MQGetMessageOptions gmo=new MQGetMessageOptions(); gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_NEXT ; //System.out.println("Status: "+i); MQMessage out=new MQMessage(); out.format =MQC.MQFMT_XMIT_Q_HEADER;//MQC.MQFMT_REF_MSG_HEADER; mqCon.getQue().get(out,gmo); System.out.print(i); 

How can I get all messages without using a for loop? It takes a long time to view 10,000 messages.

+4
source share
4 answers

How can I get all messages without using for a loop?

Use a while . Sorry, I could not resist a slightly mock answer to this question. WMQ does not have an API call similar to the SQL select statement. Messages and databases share some features, but require fundamentally different requirements.

It takes a long time to view 10,000 messages.

Take a look at Performance SupportPacs. They are published on the SupportPacs homepage and have names starting with MP. Find the one that suits your platform and MQ version, and it will list various scripts for posting and receiving messages, as well as recommendations for tuning performance.

I would also ask why a regular application should view 10,000 messages. QMgr will select the messages for you based on the MsgID, correlation ID or property, and this is much faster than viewing all the messages so that the application can find the messages you are interested in. Sometimes people need to look at all the messages in the queue in order to archive the queue or debug the problem, but this is an exception, not a rule. If the Production application regularly scans all messages in the queue, the queues may not be used correctly as a database.

+2
source

How can I get all messages without using for a loop?

 MQGetMessageOptions getOptions = new MQGetMessageOptions(); getOptions.options = MQC.MQGMO_BROWSE_NEXT + MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING; MQMessage message = new MQMessage(); byte[] b = null; while(true) { try { queue.get(message, getOptions); b = new byte[message.getMessageLength()]; message.readFully(b); System.out.println(new String(b)); message.clearMessage(); } catch (IOException e) { System.out.println("IOException: " + e.getMessage()); break; } catch (MQException e) { if (e.completionCode == 2 && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) System.out.println("All messages read."); else System.out.println("MQException: Completion Code = " + e.completionCode + " : Reason Code = " + e.reasonCode); break; } } 

I have posted some Java / MQ examples here: http://www.capitalware.biz/mq_code_java.html

+1
source

If you want to just browse messeges (not extract them), you can use javax.jms.QueueBrowser. Very fast...

 import javax.jms.* ... public ArrayList<Message> browse() { ... QueueBrowser queueBrowser = queueSession.createBrowser((javax.jms.Queue) lookupQueue()); Enumeration enums = queueBrowser.getEnumeration(); while (enums.hasMoreElements()) { Object objMsg = enums.nextElement(); if (objMsg instanceof TextMessage) { TextMessage message = (TextMessage) objMsg; Log4j.trace("Text message: " + i + ". MSG:" + message.getText() + " MSG id:" + message.getJMSMessageID() + " MSG dest:" + message.getJMSDestination()); } else if (objMsg instanceof ObjectMessage) { ObjectMessage message = (ObjectMessage) objMsg; Log4j.trace("Object Message: " + i + ". MSG" + " MSG id:" + message.getJMSMessageID() + " MSG dest:" + message.getJMSDestination()); } } } ... 
+1
source

Just noticed the message format (out.Format) that you are using. MQFMT_XMIT_Q_HEADER is used for messages sent to the transmission queue. Messages in a transmission queue are usually not read by applications. MQ uses a send queue to send messages from one queue manager to another queue manager on the MQ network. I hope you do not view messages in the transmission queue.

For applications, the message format usually depends on the receiving application. For example, if the receiving application is based on CICS, then the format will be MQFMT_CICS, for IMS it will be MQFMT_IMS. If a text / string data type is expected, you can use MQFMT_STRING. For administering MQ using PCF messages, the format may be MQFMT_PCF.

0
source

Source: https://habr.com/ru/post/1445344/


All Articles