Deployment 当远程机器未运行时,如何为远程JMS队列初始化ConnectionFactory?

Deployment 当远程机器未运行时,如何为远程JMS队列初始化ConnectionFactory?,deployment,jboss,jms,jndi,jbossmq,Deployment,Jboss,Jms,Jndi,Jbossmq,使用JBoss4.0.5、JBossMQ和Spring2.0.8,我试图将Spring配置为实例化依赖于远程JMS队列资源的bean。我遇到的所有示例都依赖于使用JNDI查找远程ConnectionFactory对象之类的对象 我的问题是,当尝试启动一台将消息放入远程队列的机器时,如果远程机器没有启动,JNDI查找就会失败,导致部署失败。有没有办法让Spring在不阻止其余部署的情况下继续尝试在后台查找此对象?如果不查看Spring配置,很难确定,但假设您使用Spring的JndiObjectF

使用JBoss4.0.5、JBossMQ和Spring2.0.8,我试图将Spring配置为实例化依赖于远程JMS队列资源的bean。我遇到的所有示例都依赖于使用JNDI查找远程ConnectionFactory对象之类的对象


我的问题是,当尝试启动一台将消息放入远程队列的机器时,如果远程机器没有启动,JNDI查找就会失败,导致部署失败。有没有办法让Spring在不阻止其余部署的情况下继续尝试在后台查找此对象?

如果不查看Spring配置,很难确定,但假设您使用Spring的
JndiObjectFactoryBean
来执行JNDI查找,然后,您可以将
lookupOnStartup
属性设置为false,这样即使JNDI目标不存在,也可以启动上下文。JNDI解析将在第一次使用ConnectionFactory时完成


然而,这只是将问题进一步向上转移,因为如果其他组件试图在启动时获得JMS
连接,那么您就回到了开始的位置。您可以在其他bean上使用
lazy init=“true”
属性来防止在部署时发生这种情况,但很容易在配置中意外地放入一些内容,从而强制所有内容初始化。

您完全正确。我尝试将lookupOnStartup设置为false,lazy init=true。这只是将问题推迟到首次尝试使用队列时。然后抛出一个异常,如下所示:

[org.jboss.mq.il.uil2.SocketManager] Failed to handle: org.jboss.mq.il.uil2.msgs.CloseMsg29702787[msgType: m_connectionClosing, msgID: -2147483606, error: null]
java.io.IOException: Client is not connected
此外,看起来再也不会尝试查找了。当带远程队列的机器重新启动时,随后不会处理任何消息。这看起来真的应该在J2EE无意义的用例范围内,但我没有太多的运气。。。感觉它甚至应该是一个解决了的问题

为了完整起见,下面是我的Spring配置的相关部分

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
            <prop key="java.naming.provider.url">localhost:1099</prop>
            <prop key="java.naming.factory.url.pkgs">org.jnp.interfaces:org.jboss.naming</prop>
            <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
            </props>
        </property>
    </bean>

    <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="jndiTemplate"/>
        </property>
        <property name="jndiName">
            <value>ConnectionFactory</value>
        </property>
    </bean>

    <bean id="remoteJndiTemplate" class="org.springframework.jndi.JndiTemplate" lazy-init="true"> 
        <property name="environment"> 
            <props> 
            <prop key="java.naming.provider.url">jnp://10.0.100.232:1099</prop>
            <prop key="java.naming.factory.url.pkgs">org.jnp.interfaces:org.jboss.naming</prop>
            <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop> 
            </props> 
        </property> 
    </bean> 

    <bean id="remoteConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <property name="jndiTemplate" ref="remoteJndiTemplate"/>
        <property name="jndiName" value="ConnectionFactory" />
        <property name="lookupOnStartup" value="false" />
        <property name="proxyInterface" value="javax.jms.ConnectionFactory" />
    </bean>

    <bean id="destinationResolver" class="com.foo.jms.FooDestinationResolver" />

    <bean id="localVoicemailTranscodingDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate" ref="jndiTemplate"/>
        <property name="jndiName" value="queue/voicemailTranscoding" />
    </bean>

    <bean id="globalVoicemailTranscodingDestination" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true" >
        <property name="jndiTemplate" ref="remoteJndiTemplate" />
        <property name="jndiName" value="queue/globalVoicemailTranscoding" />
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" >
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="defaultDestination" ref="localVoicemailTranscodingDestination" />
    </bean>

    <bean id="remoteJmsTemplate" class="org.springframework.jms.core.JmsTemplate" lazy-init="true">
        <property name="connectionFactory" ref="remoteConnectionFactory"/>
        <property name="destinationResolver" ref="destinationResolver"/>
    </bean>

    <bean id="globalQueueStatus" class="com.foo.bar.recording.GlobalQueueStatus" />

    <!-- Do not deploy this bean for machines other than transcoding machine -->
    <condbean:cond test="${transcoding.server}">
        <bean id="voicemailMDPListener"
              class="org.springframework.jms.listener.adapter.MessageListenerAdapter" lazy-init="true">
            <constructor-arg>
                <bean class="com.foo.bar.recording.mdp.VoicemailMDP" lazy-init="true">
                    <property name="manager" ref="vmMgr" />
                </bean>
            </constructor-arg>
        </bean>
    </condbean:cond>

    <bean id="voicemailForwardingMDPListener"
          class="org.springframework.jms.listener.adapter.MessageListenerAdapter" lazy-init="true">
        <constructor-arg>
            <bean class="com.foo.bar.recording.mdp.QueueForwardingMDP" lazy-init="true">
                <property name="queueStatus" ref="globalQueueStatus" />
                <property name="template" ref="remoteJmsTemplate" />
                <property name="remoteDestination" ref="globalVoicemailTranscodingDestination" />
            </bean>
        </constructor-arg>
    </bean>

    <bean id="prototypeListenerContainer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer"
          abstract="true"
          lazy-init="true">
        <property name="concurrentConsumers" value="5" />
        <property name="connectionFactory" ref="connectionFactory" />
        <!-- 2 is CLIENT_ACKNOWLEDGE: http://java.sun.com/j2ee/1.4/docs/api/constant-values.html#javax.jms.Session.CLIENT_ACKNOWLEDGE -->
        <!-- 1 is autoacknowldge -->
        <property name="sessionAcknowledgeMode" value="1" />
        <property name="sessionTransacted" value="true" />
     </bean>

     <!-- Do not deploy this bean for machines other than transcoding machine -->
     <condbean:cond test="${transcoding.server}">
         <bean id="voicemailMDPContainer" parent="prototypeListenerContainer" lazy-init="true">
           <property name="destination" ref="globalVoicemailTranscodingDestination" />
           <property name="messageListener" ref="voicemailMDPListener" />
         </bean>
     </condbean:cond>

     <bean id="voicemailForwardMDPContainer" parent="prototypeListenerContainer" lazy-init="true">
       <property name="destination" ref="localVoicemailTranscodingDestination" />
       <property name="messageListener" ref="voicemailForwardingMDPListener" />
     </bean>

本地主机:1099
接口:org.jboss.naming
org.jnp.interfaces.NamingContextFactory
连接工厂
jnp://10.0.100.232:1099
接口:org.jboss.naming
org.jnp.interfaces.NamingContextFactory

尝试删除所有定义中的lazy init,并将prototypeListenerContainer定义的AutoStart设置为false。这将阻止侦听器容器在上下文启动时尝试获取连接,但是您需要找到一种方法,自己在侦听器容器上手动调用start()。哦,欢迎来到可怕的JMS。如果我能让JNDI查找继续在后台进行,让我的客户机在没有服务器的情况下继续他们的快乐生活,一切都会很美好。我会试试你的建议。非常感谢。