|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Get all mesasages(java) |
« View previous topic :: View next topic » |
Author |
Message
|
joppe |
Posted: Mon Oct 24, 2011 11:45 pm Post subject: Get all mesasages(java) |
|
|
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 |
|
 |
mqjeff |
Posted: Tue Oct 25, 2011 1:42 am Post subject: |
|
|
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 |
|
 |
fjb_saper |
Posted: Tue Oct 25, 2011 6:35 am Post subject: |
|
|
 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 |
|
 |
mqjeff |
Posted: Tue Oct 25, 2011 6:52 am Post subject: |
|
|
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 |
|
 |
|
|
 |
|
Page 1 of 1 |
|
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
|
|
|
|