使用IBM MQ Client的JMS连接池
我们正在IBM MQ 9.x服务器和IBM MQ客户端之间使用MQIPT 9.2.我们还使用Java中的IBM MQ客户端jar连接到队列管理器,以推送和接收正常运行的消息.但是,创建连接需要花费时间,如果我们及时创建连接,则每次都会花费时间.
We are using MQIPT 9.2 between our IBM MQ 9.x server and IBM MQ clients. We're also using the IBM MQ client jar in Java to connect to the queue manager to push and receive messages which is working fine. However connection creation is taking time and every time it will take time if we create connection just in time.
我们如何为IBM MQ实现JMS连接池?
How we can implement JMS connection pooling for IBM MQ?
以下描述了我们的连通性:
The following depicts our connectivity:
[] [1
有什么标准方法可以实现连接池?
Is there any standard way so that we can implement connection pooling?
以下使用的代码
System.out.println("<<<<<<<<<Starting test for push messages>>>>>>>>>>");
try {
// Create a keystore object for the truststore
KeyStore trustStore = KeyStore.getInstance("JKS");
char[] keyPassphrase = "*******".toCharArray();
trustStore.load(new FileInputStream(
"JKS File path"),
keyPassphrase);
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
System.out.println("SSL certificates loaded in message sending");
// Create default MQ connection factory
MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
factory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
factory.setQueueManager(QMGRNAME);
factory.setHostName(HOSTNAME);
factory.setChannel(CHANNEL);
factory.setPort(1414);
factory.setSSLFipsRequired(false);
factory.setSSLSocketFactory(sslSocketFactory);
factory.setClientReconnectTimeout(100);
factory.setStringProperty(WMQConstants.USERID, user);
factory.setStringProperty(WMQConstants.PASSWORD, password);
factory.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
factory.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "cipher suite");
mqConnection = (MQQueueConnection) factory.createQueueConnection();
MQQueueSession session = (MQQueueSession) mqConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
// Start the connection
System.out.println("Connection starting while sending message");
mqConnection.start();
System.out.println("Connection started while sending message");
for (int i = 0; i <50; i++) {
System.out.println("Preparing message before sending");
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session
.createTextMessage("SimplePTP - msg" + uniqueNumber);
System.out.println("message prepared while sending , text: " + message.getText());
Destination destination = session.createQueue(destinationName);
MQMessageProducer producer = (MQMessageProducer) session.createProducer(destination);
// And, send the message
producer.send(message);
System.out.println("Sent message****************:\n" + message);
}
/*
* if (connection != null) { System.out.
* println("*************connection closing after message sent********************"
* ); connection.close(); System.out.
* println("*************connection closed after message sent********************"
* ); }
*/
System.out.println("<<<<<<<<<<Test ended>>>>>>>>>>>>");
} catch (JMSException j) {
j.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finally block after message sent************ ");
if (mqConnection != null) {
try {
mqConnection.close();
System.out.println("connection closed after message sent in finally block\n");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("<<<<<<<<<<Test ended from finally >>>>>>>>>>>>");
}
使用上述代码时,创建连接需要时间,并且会为每个消息创建和关闭连接 .这是一种不好的做法,因此我创建了一个列表并向其中添加了连接,效果很好.但是,我想改用正确的连接池.
When using the above code connection creation takes time and it's creating and closing a connection for each messsage. This is a bad practice so I created a list and added connection into it which works well. However, I want to use a proper connection pool instead.
您可以使用:
<bean class="org.apache.activemq.jms.pool.PooledConnectionFactory"
id="source.pooledConnectionFactory" primary="true">
<property name="maxConnections" value="1"/>
<property name="idleTimeout" value="0"/>
<property name="connectionFactory" ref="factory"/>
</bean>
(对不起,当您发布Java DSL时使用XML,但是您知道了).基本上,将连接工厂与ActiveMQ JMS池连接工厂包装在一起.
(sorry for the XML when you posted Java DSL, but you get the idea). Basically, wrap your connection factory with the ActiveMQ JMS pooled connection factory.
或者,您可以使用:
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>1.1.0</version>
</dependency>
JmsPoolConnectionFactory pooledCF = new JmsPoolConnectionFactory();
pooledCF.setConnectionFactory(connectionFactory());
pooledCF.setMaxConnections(1);
org.messaginghub
项目是ActiveMQ代码的一个分支,没有ActiveMQ依赖项.
The org.messaginghub
project is a branch of the ActiveMQ code and has no ActiveMQ dependencies.