0

I'm using the connection.start() to start the connection and consumer.receive() to receive the messages from the queue. But while closing the connection, it's not able to close the connection using connection.close(). Due to this it's exhausting the connection limit and throwing an exception that queue manager is not available.

What's the reason behind this? and how to solve it?

    connectionWMQ = connectionFactory.CreateConnection();
    connectionWMQ.ExceptionListener = new ExceptionListener(OnXMSException);

    // Create session
    ISession sessionWMQ = connectionWMQ.CreateSession(false, AcknowledgeMode.AutoAcknowledge);

    IDestination destination = sessionWMQ.CreateQueue("QueueName");
    IMessageConsumer consumer=sessionWMQ.CreateConsumer(destination);
    try{
         connectionWMQ.Start();
         var message=(IMessage)Consumer.Receive(TIMEOUTTIME);
         //decoding the msg;

         connectionWMQ.Close();
       }
  catch(Exception ex){
       }
kaseidu
  • 173
  • 1
  • 6
  • Looking at the method names you mention, you are using XMS. NET core. What is the version? connection.close() closes all open objects created under that connection - namely consumer, producer and session. It may be that connection.close is not getting called, for some reason, in your application! Can you update the question with the code? – Shashi May 27 '22 at 08:13
  • @Shashi I'm using IBM XMS version 9.2.0.4. I've tested and it's calling the connection.close() each time connection is open for receiving the message. – Mrinal Pramanik May 27 '22 at 08:45
  • How frequently are the connections opened and closed? Are you doing it for every message consumed? – Shashi May 27 '22 at 08:48
  • @Shashi Yes. I'm using windows service. – Mrinal Pramanik May 27 '22 at 08:56
  • One of the best practise is to open connection to queue manager only once and hold it as long as required and close it during application shutdown. See section "Build with performance in mind" of this page https://mqseries.wordpress.com/2014/12/16/the-top-15-websphere-mq-best-practices/. Another point: In your code if the Consumer.Receive(TIMEOUTTIME) or any other of your code throws an exception, then connection.close will not be called. Could you move the connection.close after catch block and try? – Shashi May 27 '22 at 09:05
  • @Shashi Thanks for the suggestion. The actual and final code consists the connection.close() at the end of the try catch block. Is there is any way to to open the connection frequently because as per our requirement we are using more than one queue manager details to connect. – Mrinal Pramanik May 27 '22 at 09:11
  • Do you see any errors on the queue manager side? look into errors directory of that specific queue manager – Shashi May 27 '22 at 10:07
  • Yes, from the queue manager it's generating "The maximum number of instances 300 channel reached". And from the application it's throwing exception that queue manager is not available/ running. – Mrinal Pramanik May 27 '22 at 10:23
  • Okay that points to connections not being closed. Similar question here: https://stackoverflow.com/questions/56901040/mq-the-maximum-number-of-instances-of-channel-was-reached – Shashi May 27 '22 at 10:34
  • I have tested your code (xms client 9.2.0.4) against MQ server 9.2.0.2. Having no issues connecting/disconnecting. How is your instance of IConnectionFactory created? Possible to provide a complete example of failing code? – kaseidu May 30 '22 at 10:31
  • Issue got solved. Thanks. – Mrinal Pramanik May 31 '22 at 12:37
  • 2
    What was the solution. You can write a self answer. – JoshMc Jun 01 '22 at 03:45

1 Answers1

0

After a successful completion of connectionFactory.CreateConnection() you need to make sure to close the connection finally. The code you provide does not guarantee this, there are some calls which could fail, with the result it would skip closing the connection. You could for example move everything behind CreateConnection() into a try block and move close() call to the corresponding finally block.

Nine Friends
  • 156
  • 5