ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ API Support » Get all mesasages(java)

Post new topic  Reply to topic
 Get all mesasages(java) « View previous topic :: View next topic » 
Author Message
joppe
PostPosted: Mon Oct 24, 2011 11:45 pm    Post subject: Get all mesasages(java) Reply with quote

Newbie

Joined: 10 Oct 2011
Posts: 4
Location: Oslo - Norway

Hi.

I am writing some small test applications in java just for the learning experience and one of the things I would like to do is to get all the messages on a queue in one loop. The following works great, but something tells me that there must be a better way to do this. I personally don't like using exceptions as program logic.
One more thing - Do I need to create a new MQMessage object each time I am getting a message from the Queue (I tried clearMessage(), but no luck)?

Any thoughts?


Code:

   public ArrayList<String> getAllMessagesFromQueue(){
      
      ArrayList<String> messagelist = new ArrayList<String>();
      
      MQMessage messageBuffer = new MQMessage();
      
      MQGetMessageOptions gmo = new MQGetMessageOptions();
      
      try {
         
         while(true){
            
            myQueue.get(messageBuffer, gmo);
            messagelist.add(messageBuffer.readUTF());

            messageBuffer = new MQMessage();
         }
         
         
      } catch (MQException e) {
         System.out.println("Websphere MQ ERROR occured : Completion code " + e.completionCode
               + " Reason Code " + e.reasonCode);
         if(e.reasonCode == 2033){
            //"EOF" - no more messages on queue!
            return messagelist;
         }
         
         //e.printStackTrace();
      } catch (IOException e){
         System.out.print("IOException:");
         e.printStackTrace();
      }
      
      return null;
   }
Back to top
View user's profile Send private message
mqjeff
PostPosted: Tue Oct 25, 2011 1:42 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

You should be checking the Reason Code after each Get. If it's a 2033, or a few other things, you should stop the loop.
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Tue Oct 25, 2011 6:35 am    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

Know that readUTF an writeUTF come in a pair. For all other stuff use simple write and read.
If you do not create a new message every time around the loop, the minimum you need to do is reinitialize msgId and correlId to MQC.MQMI_NONE and MQC.MQCI_NONE before each get.

Have fun
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
mqjeff
PostPosted: Tue Oct 25, 2011 6:52 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

fjb_saper wrote:
Know that readUTF an writeUTF come in a pair. For all other stuff use simple write and read.


And don't, in general, use readUTF OR writeUTF.

They impose a very small limit on the size of the text.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ API Support » Get all mesasages(java)
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.