How to improve MQ client create conncetion time?

天大地大妈咪最大 提交于 2020-01-07 02:40:28

问题


I have an app on Tomcat 7.05 that connects to a remote MQ manager, everything work fine, it just that creating a connection takes an insane amount of time ~ 13 seconds (if I put both java code and MQ server on one machine, it takes less then a second). How do I improve create connection time?

My java code:

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;
            }
      }
}

My context.xml resource:

<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"/>

回答1:


Clearly 13 seconds is too much for establishing connection to queue manager. I think the problem is really with your network. Talk to your network administrator and fix the issues and test your application again.




回答2:


Network issue... disabling NetBios over tcp\ip on the network connection of the server side did the trick



来源:https://stackoverflow.com/questions/27587226/how-to-improve-mq-client-create-conncetion-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!