ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » General IBM MQ Support » MQ connection not being closed.

Post new topic  Reply to topic
 MQ connection not being closed. « View previous topic :: View next topic » 
Author Message
jfbaro
PostPosted: Mon Dec 14, 2009 12:13 pm    Post subject: MQ connection not being closed. Reply with quote

Novice

Joined: 05 Jun 2009
Posts: 10

Hi,

We have an application running on WAS 5.0 which connects to MQ 5.3. We are not using JMS, but MQ (IBM classes) instead.

By sniffing the connections we can see the connections are not being closed, which at some point makes new connections take a long time to complete.

The code I use to send the message to MQ (and also to open and close the connection - on every message) seems OK, but I would like to hear from you guys.

We have a firewall set which closes connections which are not being used for longer than 1h. In this case I would imagine MQManager (on WAS) will not be aware of that, so it will try to use the same connection it was supposed to, and will probably keep trying it before openning a new one.

Any help will be much appreciated.

See below the code we are using:

Code:

/*
 * @(#)ManagerQueue.java       1.0 07/04/2009 11:30
 */

package com.xxxxx.yyyyyy.jms.message.sender;

/**
 * @author XXXXXXXXX
 * @version 1.0 (XXXXXXX)
 */

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

import com.ibm.mq.MQC;
import com.ibm.mq.MQException;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

import com.xxxxx.yyyyyy.jms.exception.ManagerQueueException;
import com.xxxxx.yyyyyy.jms.exception.ServiceLocatorException;
import com.xxxxx.yyyyyy.jms.util.Property;
import com.xxxxx.yyyyyy.jms.util.ServiceLocator;
import com.xxxxx.yyyyyy.util.TokenLogger;

public class ManagerMQQueue implements ManagerQueue {

   /**
    * Fields
    */
   private String host;
   private int port;
   private String userId;
   private String password;
   private String channel;
   private int charSet;
   private String encoding;
   private String queueManager;
   private String queueName;
   private MQQueueManager mqQueueManager;
   private MQPutMessageOptions mqPutMsgOptions;
   private MQQueue mqQueue;

   private TokenLogger logger = TokenLogger.getInstance();

   /**
    * Constructor
    */
   public ManagerMQQueue() {
   }

   /**
    * Puts the message into the Queue.
    * Reads the value from mqConfig.properties:
    *    - mq.host
    *    - mq.port
    *    - mq.channel
    *    - mq.charSet (-1 indicates it is not used)
    *    - mq.queueManager
    *    - mq.queueName
    *    - mq.userId
    *    - mq.password
    *    - mq.encoding
    *
    * @param message
    * @throws ManagerQueueException
    */
   public void sendMessage(String message) throws ManagerQueueException {

      try {

         readConfigParameters();
         connect(
            host,
            port,
            channel,
            charSet,
            queueManager,
            queueName,
            userId,
            password);
         sendMQMessage(message, charSet, encoding);

      } catch (NumberFormatException e) {
         StringWriter sw = new StringWriter();
         e.printStackTrace(new PrintWriter(sw));
         String stackTrace = sw.toString();
         logger.logInfo(
            "NumberFormatException: " + "\nStackTrace:\n" + stackTrace);
         throw new ManagerQueueException(
            "Error sending message: " + e + "\nStackTrace:\n" + stackTrace);
      } catch (Exception e) {
         StringWriter sw = new StringWriter();
         e.printStackTrace(new PrintWriter(sw));
         String stackTrace = sw.toString();
         logger.logInfo("Exception: " + "\nStackTrace:\n" + stackTrace);
         throw new ManagerQueueException(
            "Error sending message: " + e + "\nStackTrace:\n" + stackTrace);
      } catch (Throwable e) {
         StringWriter sw = new StringWriter();
         e.printStackTrace(new PrintWriter(sw));
         String stackTrace = sw.toString();
         logger.logInfo("Throwable: " + "\nStackTrace:\n" + stackTrace);
         throw new ManagerQueueException(
            "(Throwable)Error sending message: " + e + "\nStackTrace:\n" + stackTrace);
      } finally {
         releaseResources();
      }
   }

   /**
    * This method reads the parameters from config file and set them to
    * local variables   
    */
   private void readConfigParameters() {

      logger.logInfo("Reading parameters from config file...");

      this.host = Property.getString("mq.host");
      this.port = Integer.parseInt(Property.getString("mq.port"));
      this.channel = Property.getString("mq.channel");

      String strCharSet = Property.getString("mq.charSet");
      if (strCharSet == null)
         this.charSet = -1;
      else
         this.charSet = Integer.parseInt(Property.getString("mq.charSet"));

      this.queueManager = Property.getString("mq.queueManager");
      this.queueName = Property.getString("mq.queueName");
      this.userId = Property.getString("mq.userId");
      this.password = Property.getString("mq.password");
      this.encoding = Property.getString("mq.encoding");

      logger.logInfo("Parameters read successfuly from config file." +
      "\nmq.host:" + this.host +
      "\nmq.port:" + this.port +
      "\nmq.channel:" + this.channel +
      "\nmq.charSet:" + this.charSet +
      "\nmq.queueManager:" + this.queueManager +
      "\nmq.queueName:" + this.queueName +
      "\nmq.userId:" + this.userId +
      "\nmq.password:" + this.password +
      "\nmq.encoding:" + this.encoding);
   }

   /**
    * This method connects to MQ using IBM's classes
    */
   private void connect(
      String host,
      int port,
      String channel,
      int charSet,
      String queueManager,
      String queue,
      String userId,
      String password)
      
      throws MQException {

      logger.logInfo(
         "Connecting to MQ... \nhost: "
            + host
            + "\nport: "
            + port
            + "\nchannel: "
            + channel
            + "\ncharSet: "
            + charSet
            + "\nqueueManager: "
            + queueManager
            + "\nqueue: "
            + queue
            + "\nuserId: "
            + userId
            + "\npassword: "
            + password);

      MQEnvironment.hostname = host;      //MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_MQSERIES_CLIENT);

      MQEnvironment.channel = channel;
      MQEnvironment.port = port;
      if (userId != null)
         MQEnvironment.userID = userId;
      if (password != null)
         MQEnvironment.password = password;

      if (charSet != -1)
         MQEnvironment.CCSID = charSet;

      mqQueueManager = new MQQueueManager(queueManager);

      int openOptions =
         MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE | MQC.MQOO_FAIL_IF_QUIESCING;

      mqQueue =
         mqQueueManager.accessQueue(queue, openOptions, null, null, null);

      mqPutMsgOptions = new MQPutMessageOptions();
      mqPutMsgOptions.options = MQC.MQPMO_NONE;

      logger.logInfo(
         "MQ connected... \nhost: "
            + host
            + "\nport: "
            + port
            + "\nchannel: "
            + channel
            + "\ncharSet: "
            + charSet
            + "\nqueueManager: "
            + queueManager
            + "\nqueue: "
            + queue
            + "\nuserId: "
            + userId
            + "\npassword: "
            + password);

   }

   /**
    * This method is responsible for sending the MQ message
    * using IBM's classes
    */
   private void sendMQMessage(String message, int charSet, String encoding)
      throws MQException, IOException {

      logger.logInfo(
         "Sending MQ message... \nmessage: "
            + message
            + "\ncharSet: "
            + charSet
            + "\nencoding: "
            + encoding);

      MQMessage mqMessage = new MQMessage();
      mqMessage.clearMessage();
      mqMessage.persistence = MQC.MQPER_NOT_PERSISTENT;
      mqMessage.correlationId = MQC.MQCI_NONE;
      mqMessage.messageId = MQC.MQMI_NONE;
      mqMessage.format = "MQSTR";

      if ((encoding != null) && (encoding.trim().length() > 0)) {
         try {
            message = (new String(message.getBytes(encoding)));
         } catch (UnsupportedEncodingException ex) {
            logger.logWarning(
               "Exception when encoding the message: "
                  + message
                  + " with the encoding:"
                  + encoding
                  + ". Using the original message.",
               ex);
         }
      }

      if (charSet != -1)
         mqMessage.characterSet = charSet;

      mqMessage.writeString(message);
      mqQueue.put(mqMessage, mqPutMsgOptions);

      logger.logInfo(
         "MQ message sent... \nmessage: "
            + message
            + "\ncharSet: "
            + charSet
            + "\nencoding: "
            + encoding);
   }

   /**
    * This method is responsible for releasing the resources used
    * by MQ
    */
   private void releaseResources() throws ManagerQueueException {

      logger.logInfo("Releasing resources...");

      try {
         if (mqQueue.isOpen())
            mqQueue.close();
         if (mqQueueManager.isConnected())
            mqQueueManager.disconnect();
                        
         //************mqQueueManager.close();********
            
      } catch (MQException e) {
         StringWriter sw = new StringWriter();
         e.printStackTrace(new PrintWriter(sw));
         String stackTrace = sw.toString();
         logger.logInfo(
            "Exception when closing: " + "\nStackTrace:\n" + stackTrace);
         throw new ManagerQueueException(
            "Error releasing resources: "
               + e
               + "\nStackTrace:\n"
               + stackTrace);
      }

      logger.logInfo("Resources released.");
   }
}
[/code]
Back to top
View user's profile Send private message
mqjeff
PostPosted: Mon Dec 14, 2009 12:43 pm    Post subject: Re: MQ connection not being closed. Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

jfbaro wrote:
open and close the connection - on every message) seems OK,


It's not.

Never ever ever do this.
Back to top
View user's profile Send private message
jfbaro
PostPosted: Mon Dec 14, 2009 1:01 pm    Post subject: Reply with quote

Novice

Joined: 05 Jun 2009
Posts: 10

Hi mqjeff.

Thanks for the quick reply. I thought (and according to some co-workers) that the open() and close() were merely a request() and release() to the pool of connections.

What should I be doing them? Could you point me to some website or book?

Cheers
Back to top
View user's profile Send private message
jfbaro
PostPosted: Tue Dec 15, 2009 5:14 am    Post subject: Reply with quote

Novice

Joined: 05 Jun 2009
Posts: 10

anyone?
Back to top
View user's profile Send private message
JosephGramig
PostPosted: Tue Dec 15, 2009 5:16 am    Post subject: Reply with quote

Grand Master

Joined: 09 Feb 2006
Posts: 1244
Location: Gold Coast of Florida, USA

First, I'm sure WMQ 5.3 has reached 'End of Support' and I'm almost sure WAS 5.0 has too.

So task #1 is, migrate to 7.0 of both products.
Task #2, read manuals to understand how you should do this with the current release.
Back to top
View user's profile Send private message AIM Address
mqjeff
PostPosted: Tue Dec 15, 2009 5:50 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

I do not believe that in WAS v5 or any other level of WAS that using the plain MQ Java API gives you access to an explicit or implicit connection pool.

I could be wrong about that. It's been a while since I've played with WAS (thankfully).

If you were to use JMS, that would be an entirely different story - you would get access to a managed connection pool.

There is an Application Server connection pool thing available with the plain MQ Java API (at least at newer releases of MQ), but you have to explicitly code for them.
Back to top
View user's profile Send private message
jfbaro
PostPosted: Tue Dec 15, 2009 7:53 am    Post subject: Reply with quote

Novice

Joined: 05 Jun 2009
Posts: 10

Thanks you all for the answers.

OK, so I am not using the pool (I though it was used by default). But why are connections (like channels) still opened even after calling queue.close() and queueManager.disconnect() ?

P.S.: The customer can't go for newer version at the moment, unfortunately.

Regards.
Back to top
View user's profile Send private message
JosephGramig
PostPosted: Tue Dec 15, 2009 8:08 am    Post subject: Reply with quote

Grand Master

Joined: 09 Feb 2006
Posts: 1244
Location: Gold Coast of Florida, USA

There are many correction to MQ Java interface, so make sure you have at least the finial CSD (14 I think) for MQ applied.

WAS 5.0 reached EOS on 30 Sep 2006.

The support page has WAS 5.1 as the last maintenance offered.
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » General IBM MQ Support » MQ connection not being closed.
Jump to:  



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
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.