ibm-messaging / mq-golang

Calling IBM MQ from Go applications
Apache License 2.0
167 stars 60 forks source link

Using Multiple Queues #171

Closed 2020testuser closed 3 years ago

2020testuser commented 3 years ago

Hi, Could anyone please help me? Not sure whether the below is possible in mq-golang .

I want to connect to two different queues (Queue Manager, Channel etc. are all same for the two queues) programmatically using the mq-golang package? Is it possible to do this? If so, could you please let me know which go file has the implementation for this as I was unable to find it.

Thanks in advance!

ibmmqmet commented 3 years ago

You CONNECT to a queue manager, not a queue. Once connected, then you can OPEN as many objects such as queues that you need, by using the connection object.

2020testuser commented 3 years ago

@ibmmqmet - Thanks! Very sorry for the delay in my reponse. Got pulled in for another project and so was totally out of sight on this. I'm going to try the above. Could you please clarify the below? I have 5 queues. In the below options, if option 1 is the best option, will I need to create separate thread (one for each queue) to get the messages from each queue concurrently? If I want to get all messages from all queues concurrently, do I need to use threading mechanism (one thread per queue)?

  1. One QueueMgr Connection -> Multiple Queues
  2. One QueueMgr Connection -> One Queue

Thanks in advance

ibmmqmet commented 3 years ago

On a given CONNECTION, only one verb can be active at a time. So you cannot retrieve simultaneously from two queues that you have opened from the same connection - if you issue the MQGET from two threads, they will be serialised internally.

2020testuser commented 3 years ago

@ibmmqnet - Thanks! Could you please direct me to any link or sample for getting the messages from more than one queue using multiple threads (one thread for each queue)? Wondering how the MQGET works with threading. I will have to do some reading to understand.

2020testuser commented 3 years ago

@ibmmqmet - Could you please confirm that if I issue MQGET from two threads, this means, have two queues opened separately by each thread? Also, could you let me know whether running as separate thread or totally separate program (2 programs one for each queue) is better? Since, the message expiry is 5 seconds, I'm wondering whether there would be any issues in opening each queue in a separate connection in a separate thread. Thanks in advance!

ibmmqmet commented 3 years ago

How you handle threads is entirely up to you with your program. As I have already said, only a single operation can be in progress at one time for a given CONNECTION. So if you have two open OBJECT handles to the same queue for the same CONNECTION, and call MQGET in 2 separate threads, then only one will actually be worked on by the queue manager while the other is (transparently) blocked until the first is done. You can choose to have multiple connections within the same process if you want; then objects associated with the different connections can be dealt with in parallel.

Some people prefer to use the callback mechanism so that messages are pushed into the application as they arrive rather than using the synchronous MQGET verb, perhaps with polling.

There's rarely a single best design - you need to work it out based on your messaging patterns, arrival rates, what processing you do to each message, latency and response time requirements ... All the usual things.

2020testuser commented 3 years ago

@ibmmqmet - Thanks for the explanation. Will check the callback mechanism.