how to configure (spring) JMS connection Pool for

2019-02-20 17:47发布

问题:

I am trying to configure a JMS connection pool in spring/camel for Websphere MQ. I am seeing class cast exception, when tried to use CachingConnectionFactory from spring. Could not find a pool from WMQ, have anybody done connection pooling with WMQ, i didnt find any examples. There are lot of examples for ActiveMQ.

here is what i have so far, that is producing class cast exception.

<bean id="inCachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory" ref="inboundMqConnectionFactory1" />
    <property name="sessionCacheSize" value="5" />
</bean>

<bean id="inboundWebsphereMq1" class="org.apache.camel.component.jms.JmsComponent">
    <property name="connectionFactory" ref="inCachingConnectionFactory" />
    <property name="destinationResolver" ref="jmsDestinationResolver" />
    <property name="transacted" value="true" />
    <property name="transactionManager" ref="txManager1" />
</bean>

<bean id="inboundMqConnectionFactory1" class="com.ibm.mq.jms.MQQueueConnectionFactory">
    <property name="hostName" value="${isi.inbound.queue.host2}" />
    <property name="port" value="${isi.inbound.queue.port}" />
    <property name="queueManager" value="${isi.inbound.queue.queuemanager2}" />
    <property name="channel" value="${isi.inbound.queue.channel2}" />
    <property name="transportType" value="${isi.queue.transportType}" />
</bean>

The exception i see is here

trying to recover. Cause: com.sun.proxy.$Proxy37 cannot be cast to com.ibm.mq.jms.MQQueueSession

回答1:

In general:

  • do not use QueueConnectionFactory or TopicConnectionFactory, as ConnectionFactory (JMS 1.1) is replacement for both
  • Each ConnectionFactory from v7 WMQ JMS client jars provide caching logic on each own so in general you don't need CachingConnection Factory.

Now try it this way:

<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQConnectionFactory"
  p:queueManager="${QM_NAME}"
  p:hostName="${QM_HOST_NAME}"
  p:port="${QM_HOST_PORT}"
  p:channel="${QM_CHANNEL}"
  p:clientID="${QM_CLIENT_ID}">
  <property name="transportType">
    <util:constant static-field="com.ibm.msg.client.wmq.WMQConstants.WMQ_CM_CLIENT" />
  </property>
</bean>

<bean id="userConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter"
  p:targetConnectionFactory-ref="mqConnectionFactory"
  p:username="${QM_USERNAME}"
  p:password="${QM_PASSWORD}" />

<!-- this will work -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"
  p:targetConnectionFactory-ref="userConnectionFactory"
  p:cacheConsumers="true"
  p:reconnectOnException="true" />

Of course you can cache sessions instead of consumers if you want it that way. By my experience WMQ session caching is measurable performance improvement but only if you are limited on CPU power on WMQ machine or by actual message throughput; both situations are rare in majority of world applications. Caching consumers avoids excessive MQ OPEN calls which is expensive operation on WMQ so it helps too.

My rule of the thumb is consumer + session caching performance benefit is equal to 1/2 of performance benefit of connection caching and usually not wort of pursuing in your everyday JEE stack unless you are hardware limited.

Since WMQ v7, asynchronous consumers are realy realy fast with literally no CPU overhead when compared to spring MC, and are preferred way of consuming messages if you are HW limited. Most of the days I still use Spring as I prefer its easy-going nature.

Hope it helps.