Author |
Message
|
andy840920 |
Posted: Wed Aug 26, 2009 5:57 am Post subject: mq with java,multithread application,can't putMsg |
|
|
Apprentice
Joined: 29 Apr 2007 Posts: 44
|
my application like this:
Code: |
while (true) {
String msg = getQueue.getMsg();
BusinessClass obj = new BusinessClass(msg); //extends Thread
obj.start(); //start a thread to handle and the main thread continue getMsg
}
|
in obj.start() method need to call putQueue.putMsg() api to send a msg to other queue. but i found the method can't successful alway, and it block all the time.
i found something about it at http://publib.boulder.ibm.com/infocenter, that should Synchronized on the queue manager, or create one queue manager in every thread. somebody can help me? |
|
Back to top |
|
 |
zpat |
Posted: Wed Aug 26, 2009 6:40 am Post subject: |
|
|
 Jedi Council
Joined: 19 May 2001 Posts: 5866 Location: UK
|
Unless you use handle sharing, each thread needs it's own queue manager and queue handle.
Since MQ is asynchronous anyway - there is no reason to do this, just issue the put in the same thread. |
|
Back to top |
|
 |
andy840920 |
Posted: Wed Aug 26, 2009 6:48 am Post subject: |
|
|
Apprentice
Joined: 29 Apr 2007 Posts: 44
|
zpat wrote: |
Unless you use handle sharing, each thread needs it's own queue manager and queue handle.
Since MQ is asynchronous anyway - there is no reason to do this, just issue the put in the same thread. |
how to use handle sharing? my program like this:
Code: |
public static QueueManager qmgr = new QueueManager ();
getQueue = qmgr.createGetQueue();
putQueue = qmgr.createPutQueue();
|
only one qmgr handler in my process. handle sharing how to write? |
|
Back to top |
|
 |
andy840920 |
Posted: Wed Aug 26, 2009 6:52 am Post subject: |
|
|
Apprentice
Joined: 29 Apr 2007 Posts: 44
|
every thread need one queue handle?
so i must close queue after put or get msg, and open in the new thread to put or get? i think that is not efficient. so all thread use two queue handle to get or put msg in my program. |
|
Back to top |
|
 |
zpat |
Posted: Wed Aug 26, 2009 6:54 am Post subject: |
|
|
 Jedi Council
Joined: 19 May 2001 Posts: 5866 Location: UK
|
No you don't have to close the queue, multiple threads can access the same queue at the same time.
Your design is simply wrong. MQ is async anyway - you don't need to create additional threads for this purpose.
Read the manual about handle sharing but you don't need to do this. |
|
Back to top |
|
 |
mqjeff |
Posted: Wed Aug 26, 2009 7:05 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
You can't issue a PUT on a CONNECTION that is in a pending GET. You can not in general issue more than one MQ call on a single connection at the same time.
You can use the same QueueManager object for all of your puts. You need one QueueManager object for each separate GET you wish to issue *simultaneously*.
The loop posted will start the thread, and then immediately go back to the GET. If the separate thread tries to issue the PUT before the GET succeeds, the connection will block until the GET completes. If the GET is sitting in long wait, then PUT will block until the GET returns. Given this code, the various threads will be blocking in an apparently random manner, based on an internal dispatch order. |
|
Back to top |
|
 |
andy840920 |
Posted: Wed Aug 26, 2009 7:11 am Post subject: |
|
|
Apprentice
Joined: 29 Apr 2007 Posts: 44
|
Quote: |
No you don't have to close the queue, multiple threads can access the same queue at the same time.
Your design is simply wrong. MQ is async anyway - you don't need to create additional threads for this purpose.
Read the manual about handle sharing but you don't need to do this. |
Code: |
while (true) {
String msg = getQueue.getMsg();
BusinessClass obj = new BusinessClass(msg); //extends Thread
obj.start(); //start a thread to handle and the main thread continue getMsg
} |
why in obj.start() can't put a msg into another queue. how to design my program, i need is only when the main thread receive a msg and start a thread to handle, i won't block the main thread to recveive msg. |
|
Back to top |
|
 |
andy840920 |
Posted: Wed Aug 26, 2009 7:22 am Post subject: |
|
|
Apprentice
Joined: 29 Apr 2007 Posts: 44
|
so if my program like this:
Code: |
Qmgr putQmgr = new Qmgr();
Qmgr getQmgr = new Qmgr();
public static PutQueue putQueue = putQmgr.createPutQueue();
public static GetQueue getQueue = getQmgr.createGetQueue();
while (true) {
String msg = getQueue.getMsg();
BusinessClass obj = new BusinessClass(msg); //extends Thread
obj.start(); //start a thread to handle and the main thread continue getMsg
}
class BusinessClass extends Thread {
void run() {
putQueue.putMsg();
}
} |
it's ok? use two QMGR. |
|
Back to top |
|
 |
andy840920 |
Posted: Wed Aug 26, 2009 8:32 am Post subject: |
|
|
Apprentice
Joined: 29 Apr 2007 Posts: 44
|
zpat wrote: |
No you don't have to close the queue, multiple threads can access the same queue at the same time.
Your design is simply wrong. MQ is async anyway - you don't need to create additional threads for this purpose.
Read the manual about handle sharing but you don't need to do this. |
async anyway? but my GET method is waiting until get a msg. and the PUT method can't return if it's not send successful. |
|
Back to top |
|
 |
mqjeff |
Posted: Wed Aug 26, 2009 8:39 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
andy840920 wrote: |
zpat wrote: |
No you don't have to close the queue, multiple threads can access the same queue at the same time.
Your design is simply wrong. MQ is async anyway - you don't need to create additional threads for this purpose.
Read the manual about handle sharing but you don't need to do this. |
async anyway? but my GET method is waiting until get a msg. and the PUT method can't return if it's not send successful. |
I suspect that zpat's meaning was that you do not need to process each message in parallel, as you are not in any way blocking the sender of the messages you are processing.
Now, whether or not you are running in a request/reply scenario that has a very tight SLA is a separate question, and may indeed require that you processing multiple requests simultaneously.
Although if you are in that kind of a situation, you may want to highly consider porting your code to JMS and running inside an Application Server instead of trying to reinvent all of the scalability and connection pooling and management that comes with Message Driven Beans. |
|
Back to top |
|
 |
|