我在 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;
}
}
}
我的上下文.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"/>
显然,13 秒对于建立与队列管理器的连接来说太多了。我认为问题实际上出在您的网络上。请与网络管理员联系并修复问题并再次测试应用程序。
网络问题...在服务器端的网络连接上通过 tcp\ip 禁用 NetBios 可以解决问题