1

我们正在开发一个基于 Spring 的应用程序,该应用程序利用 JMSTemplate 向/从 Tibco EMS 服务器发送/接收 JMS 消息。

在当前实施中,如果 EMS 服务器关闭,则在 TomCat 启动期间项目将失败。这是因为在 Spring 配置文件中,我们有试图连接到 EMS 服务器的 JMS 相关 bean。

因此,一种解决方案是让所有与 JMS 相关的 bean 仅在需要时启动(而不是在启动期间)。为此,我们将所有与 JMS 相关的 bean 的 lazy-init 属性设置为 true。

摘录:

<bean id="jmsTransactionManager" class="org.springframework.jms.connection.JmsTransactionManager" lazy-init="true">
    <property name="internalJmsQueueConnectionFactory"> <ref bean="jmsQueueConnectionFactory" />
    </property>
</bean>

<bean id="jmsTemplateWithClientAcknowledge" class="org.springframework.jms.core.JmsTemplate" lazy-init="true">
    <property name="internalJmsQueueConnectionFactory" ref="jmsQueueConnectionFactory"/>
</bean>

问题出在:如果我们lazy-init="true"只在jmsTransactionManager bean 上设置,项目加载正常,没有问题。但是,一旦我们也设置lazy-init="true"jmsTemplateWithClientAcknowledge bean,项目就会失败。相同的失败原因:无法连接到 EMS 服务器

日志中的错误:

org.springframework.beans.factory.BeanCreationException:创建名为“jmsMsgSenderImpl”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.springframework.jms.core.JmsTemplate com.cv.pub.engine.service.impl.JmsMsgSenderImpl.jmsTemplate;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring/jms-context.xml] 中定义名称为 'jmsTemplateWithClientAcknowledge' 创建 bean 时出错:设置时无法解析对 bean 'internalJmsQueueConnectionFactory' 的引用bean 属性 'connectionFactory'; 嵌套异常是 org.springframework.beans.factory.BeanCreationException: Error created bean with name ' 在 ServletContext 资源 [/WEB-INF/spring/jms-context.xml] 中定义的 internalJmsQueueConnectionFactory':在设置 bean 属性 'targetConnectionFactory' 时无法解析对 bean 'targetJmsQueueConnectionFactory' 的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring/jms-context.xml] 中定义名称为“targetJmsQueueConnectionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 javax.naming.ServiceUnavailableException:无法查询 JNDI:无法连接到 tcp://localhost:7222 的服务器 [根异常是 javax.jms.JMSException:无法连接到 tcp://localhost 的服务器:7222] 设置 bean 属性“targetConnectionFactory”时无法解析对 bean“targetJmsQueueConnectionFactory”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring/jms-context.xml] 中定义名称为“targetJmsQueueConnectionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 javax.naming.ServiceUnavailableException:无法查询 JNDI:无法连接到 tcp://localhost:7222 的服务器 [根异常是 javax.jms.JMSException:无法连接到 tcp://localhost 的服务器:7222] 设置 bean 属性“targetConnectionFactory”时无法解析对 bean“targetJmsQueueConnectionFactory”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring/jms-context.xml] 中定义名称为“targetJmsQueueConnectionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 javax.naming.ServiceUnavailableException:无法查询 JNDI:无法连接到 tcp://localhost:7222 的服务器 [根异常是 javax.jms.JMSException:无法连接到 tcp://localhost 的服务器:7222] ServletContext 资源 [/WEB-INF/spring/jms-context.xml] 中定义的 targetJmsQueueConnectionFactory':调用 init 方法失败;嵌套异常是 javax.naming.ServiceUnavailableException:无法查询 JNDI:无法连接到 tcp://localhost:7222 的服务器 [根异常是 javax.jms.JMSException:无法连接到 tcp://localhost 的服务器:7222] ServletContext 资源 [/WEB-INF/spring/jms-context.xml] 中定义的 targetJmsQueueConnectionFactory':调用 init 方法失败;嵌套异常是 javax.naming.ServiceUnavailableException:无法查询 JNDI:无法连接到 tcp://localhost:7222 的服务器 [根异常是 javax.jms.JMSException:无法连接到 tcp://localhost 的服务器:7222]

我将非常感谢您的想法和帮助!

4

1 回答 1

0

targetJmsQueueConnectionFactory 是否被 internalJmsQueueConnectionFactory 使用?根据日志,它似乎是这样的。您将需要使 internalJmsQueueConnectionFactory 也惰性初始化。

于 2011-11-10T05:16:07.953 回答