正如Umapathy在选项3中所建议的那样,我们现在选择了使用Spring的CachingConnectionFactory的方法,即使对于非Spring应用程序,它也能很好地工作。您需要做的就是将Spring
Jars添加到类路径中,并用CachingConnectionFactory包装MQQueueConnectionFactory。
我们选择创建自己的Tomcat
QueueConnectionFactoryFactory,使我们能够完全保持原始应用程序代码不变,您只需要使用以下XML定义替换Tomcat配置文件(问题中所示)中的原始MQ连接工厂:
<Resource name="jms/XXXQCF" auth="Container" type="org.springframework.jms.connection.CachingConnectionFactory" factory="at.rsf4j.core.utilities.RSFCachingMQQueueConnectionFactoryFactory" description="JMS Queue Connection Factory" HOST="xxx.com" PORT="1429" CHAN="XXX" TRAN="1" QMGR="XXX" />
这是RSFCachingMQQueueConnectionFactoryFactory的(简化)代码(无错误检查):
public class RSFCachingMQQueueConnectionFactoryFactory implements ObjectFactory{public Object getObjectInstance (Object obj, Name name, Context nameCtx, Hashtable<?,?> environment) throws NamingException { Reference ref = (Reference) obj; String beanClassName = ref.getClassName(); Class<?> beanClass = Class.forName(beanClassName); if (CachingConnectionFactory.class.isAssignableFrom(beanClass)){ MQQueueConnectionFactoryFactory cff = new MQQueueConnectionFactoryFactory(); Reference mqReference = new Reference( MQQueueConnectionFactory.class.getName()); Enumeration<RefAddr> allAddrs = ref.getAll(); while (allAddrs.hasMoreElements()){ mqReference.add(allAddrs.nextElement()); } MQQueueConnectionFactory cf = (MQQueueConnectionFactory)cff.getObjectInstance(mqReference, name, nameCtx, environment); CachingConnectionFactory ccf = (CachingConnectionFactory)beanClass.newInstance(); ccf.setTargetConnectionFactory(cf); return ccf; } }


