|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
JAVA - Read multi messages from a Queue with single access. |
« View previous topic :: View next topic » |
Author |
Message
|
kalpakci |
Posted: Wed May 22, 2013 5:50 am Post subject: JAVA - Read multi messages from a Queue with single access. |
|
|
 Newbie
Joined: 19 Mar 2012 Posts: 3
|
I'm new to Websphere MQ - Java operations. I use Java to read messages from a queue then process them.
First I connect to queue, read a single message, then disconnect. It opens and closes about 20 connections per second.
1 - How can i read more than one message per access.
2 - I use openOptions = 28. It is working but is it correct?
My running code:
Code: |
MQMessage retrievedMessage = null;
private MQQueueManager qMgr = null;
public void run() throws InterruptedException {
Config.Get_Values();
MQQueue myQueue = null;
int openOptions = 28;
int queuedepth = 0;
com.ibm.mq.MQEnvironment.hostname = Config.MQHostname;
com.ibm.mq.MQEnvironment.port = Config.MQPort;
com.ibm.mq.MQEnvironment.channel = Config.MQchannel;
com.ibm.mq.MQEnvironment.CCSID = Config.MQCCSID;
this.qMgr = new MQQueueManager(Config.MQManagerName);
MQQueue queue_depth_access = this.qMgr.accessQueue(Config.MQQueueName, MQC.MQOO_SET | MQC.MQOO_INQUIRE);
queuedepth = queue_depth_access.getCurrentDepth();
if (queuedepth < 1) {
System.out.println("queue is empy");
queue_depth_access.close();
qMgr.disconnect();
return;
}
myQueue = this.qMgr.accessQueue(Config.MQQueueName, openOptions, null, null, null);
MQMessage myMessage = new MQMessage();
myMessage.correlationId = MQC.MQCI_NONE;
myMessage.messageId = MQC.MQMI_NONE;
myQueue.get(myMessage);
msg = myMessage.readString(myMessage.getMessageLength());
myQueue.close();
qMgr.disconnect();
if (msg.contains("PMovement")) {
//process
} else {
//nothing
}
}
|
|
|
Back to top |
|
 |
Vitor |
Posted: Wed May 22, 2013 6:06 am Post subject: Re: JAVA - Read multi messages from a Queue with single acce |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
kalpakci wrote: |
First I connect to queue, read a single message, then disconnect. It opens and closes about 20 connections per second. |
This is the wrong design. You should connect, do whatever queue access you need to do and then disconnect when you're done.
kalpakci wrote: |
1 - How can i read more than one message per access. |
By changing the design.
kalpakci wrote: |
2 - I use openOptions = 28. It is working but is it correct? |
No. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
mqjeff |
Posted: Wed May 22, 2013 6:37 am Post subject: Re: JAVA - Read multi messages from a Queue with single acce |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Vitor wrote: |
kalpakci wrote: |
First I connect to queue, read a single message, then disconnect. It opens and closes about 20 connections per second. |
This is the wrong design. |
You should not ask for the depth of the queue.
You should read until the get tells you there are no messages left. |
|
Back to top |
|
 |
Vitor |
Posted: Wed May 22, 2013 6:48 am Post subject: Re: JAVA - Read multi messages from a Queue with single acce |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
mqjeff wrote: |
Vitor wrote: |
kalpakci wrote: |
First I connect to queue, read a single message, then disconnect. It opens and closes about 20 connections per second. |
This is the wrong design. |
You should not ask for the depth of the queue.
You should read until the get tells you there are no messages left. |
Didn't even notice that in the code
That's wrong as well. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
hughson |
Posted: Wed May 22, 2013 8:33 am Post subject: Re: JAVA - Read multi messages from a Queue with single acce |
|
|
 Padawan
Joined: 09 May 2013 Posts: 1959 Location: Bay of Plenty, New Zealand
|
kalpakci wrote: |
I use openOptions = 28. It is working but is it correct? |
In order to help others to read your code in the future, you should avoid using numbers like this and instead use the constants MQ provides for you to use. You do this in other places in your code I see, so you are not unfamiliar with the idea.
openOptions of value 28 would be the same as adding together all these constants:-
- MQC.MQOO_INPUT_EXCLUSIVE (getting from a queue in a destructive manner with exclusive queue access)
- MQC.MQOO_BROWSE (getting from a queue in a non-destructive manner)
- MQC.MQOO_OUTPUT (putting to a queue)
So you should ask yourself, do you need all those options?
As already noted, you shouldn't need to inquire the depth of the queue, especially since all you are checking for is whether there is at least one message. Attempting to get the message will tell you the same information and you can look out for the reason code MQRC_NO_MSG_AVAILABLE which will tell you the same thing.
You should ensure you only do
Code: |
..new MQQueueManager...
..qMgr.accessQueue... |
once at the beginning, and
Code: |
myQueue.close();
qMgr.disconnect(); |
once at the end.
Everything else ought to go in a loop until you are given, MQRC_NO_MSG_AVAILABLE. You should also ensure you are using the ability to wait a little while for messages too.
Cheers
Morag _________________ Morag Hughson @MoragHughson
IBM MQ Technical Education Specialist
Get your IBM MQ training here!
MQGem Software |
|
Back to top |
|
 |
fjb_saper |
Posted: Wed May 22, 2013 11:41 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
And you should ask yourself whether input exclusive is required or whether you could use input shared?  _________________ MQ & Broker admin |
|
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
|
|
|
|