如何改善MQ客户端创建连接时间?

时间:2014-12-21 06:27:45

标签: java jms ibm-mq mq

我在Tomcat 7.05上有一个连接到远程MQ管理器的应用程序,一切正常,只是创建连接需要花费大量时间~13秒(如果我将java代码和MQ服务器放在一台机器上,它需要不到一秒钟)。如何改善创建连接时间?

我的java代码:

import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.ibm.mq.constants.MQConstants;

      private MQldpResponse requestReply() {

            String messageReceive = null;
            MQldpResponse status = new MQldpResponse();

            status.setErrorCode(0);
            status.setErrorDesc("Success");

            if (message == null) {
                  LDPLogger.warning("Message is null for request service " + service);
            }       
            setMQVars();// Setting mq vars  
            QueueConnection queueConnection = null;
            QueueSender queueSender = null;
            QueueReceiver queueReceiver = null;
            try { 
                  Context context = (Context) new InitialContext().lookup("java:comp/env");
                  QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup(qcf);
                  queueConnection = queueConnectionFactory.createQueueConnection();//This takes 13~ seconds!!
                  queueConnection.start();
                  QueueSession session = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

                  Queue requestQueue = session.createQueue(requestQueueName);
                  queueSender = session.createSender(requestQueue); 
                  TextMessage requestTextMessage = session.createTextMessage(message); 
                  Queue replyQueue = session.createQueue(replyQueueName);
                  requestTextMessage.setJMSReplyTo(replyQueue);
                  requestTextMessage.setIntProperty("JMS_IBM_MsgType", MQConstants.MQMT_REQUEST); 
                  queueSender.send(requestTextMessage, deliveryMode, Message.DEFAULT_PRIORITY, expiration);
                  String messageID = requestTextMessage.getJMSMessageID();
                  if (Utils.isEmpty(messageID)) {
                        throw new Exception("invalid messageid");
                  }
                  String selector = "JMSCorrelationID = '" + messageID + "'";
                  queueReceiver = session.createReceiver(replyQueue, selector);
                  Message replyTextMessage = queueReceiver.receive(timeout);
                  if (replyTextMessage != null && replyTextMessage instanceof TextMessage) {
                        messageReceive = ((TextMessage) replyTextMessage).getText();
                        status.setResponseXML(messageReceive);
                  } else {
                        throw new Exception("No Response for service " + service + ".");
                        }
            } catch (Exception e) {
                  status.setErrorCode(MQldpResponse.ERROR_CODE_SYS_ERROR);
                  status.setErrorDesc(e.getMessage());
                  LDPLogger.error(e.getMessage(), e);
                  if (e instanceof JMSException) {
                        Exception linkedException = ((JMSException) e).getLinkedException();
                        if (linkedException != null) {
                              LDPLogger.error(linkedException.getMessage(), linkedException);
                              status.setErrorDesc(linkedException.getMessage());
                        }
                  }
            }
            finally {
                  closeJMS(queueConnection, queueSender, queueReceiver);
            }
            return status;
      }

      private void closeJMS(QueueConnection queueConnection,
                  QueueSender queueSender, QueueReceiver queueReceiver) {
            try {
                  if (queueSender != null) {
                        queueSender.close();
                        queueSender = null;
                  }
            } catch (JMSException e) {
                  queueSender = null;
            }

            try {
                  if (queueReceiver != null) {
                        queueReceiver.close();
                        queueReceiver = null;
                  }
            } catch (JMSException e) {
                  queueReceiver = null;
            }

            try {
                  if (queueConnection != null) {
                        queueConnection.close();
                        queueConnection = null;
                  }
            } catch (JMSException exception) {
                  queueConnection = null;
            }
      }
}

我的context.xml资源:

<Resource
  name="jms/QCF"
  auth="Container"
  type="com.ibm.mq.jms.MQQueueConnectionFactory"
  factory="com.ibm.mq.jms.MQQueueConnectionFactoryFactory"
  description="JMS Queue Connection Factory for sending messages"
  HOST="1.1.1.1"
  PORT="1414"
  CHAN="CHANNEL"
  TRAN="1"
  QMGR="QMGR"/>

2 个答案:

答案 0 :(得分:0)

显然,13秒对于建立与队列管理器的连接来说太过分了。我认为问题实际上与您的网络有关。与网络管理员联系并解决问题并再次测试您的应用程序。

答案 1 :(得分:0)

网络问题...在服务器端的网络连接上通过tcp \ ip禁用NetBios就行了