首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当远程机器没有运行时,如何为远程JMS队列初始化ConnectionFactory?

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

Stack Overflow用户
提问于 2009-08-24 16:46:05
回答 2查看 16.4K关注 0票数 5

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

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

EN

回答 2

Stack Overflow用户

发布于 2009-08-24 18:43:55

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

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

票数 3
EN

Stack Overflow用户

发布于 2009-08-24 22:10:08

你说的完全正确。我尝试将lookupOnStartup设置为false和lazy-init=true。这只是将问题推迟到第一次尝试使用队列时。然后抛出如下异常:

代码语言:javascript
复制
[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配置的相关部分。

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1323489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档