views:

2616

answers:

2

Using JBoss 4.0.5, JBossMQ, and Spring 2.0.8, I am trying to configure Spring to instantiate beans which depend on a remote JMS Queue resource. All of the examples I've come across depend on using JNDI to do lookup for things like the remote ConnectionFactory object.

My problem is when trying to bring up a machine which would put messages into the remote queue, if the remote machine is not up, JNDI lookup simply fails, causing deployment to fail. Is there a way to get Spring to keep trying to lookup this object in the background while not blocking the remainder of deployment?

+1  A: 

Iit's difficult to be sure without seeing your spring config, but assuming you're using Spring's JndiObjectFactoryBean to do the JNDI lookup, then you can set the lookupOnStartup property to false, which allows the context to start up even if the JNDI target isn't there. The JNDI resolution will be done the first time the ConnectionFactory is used.

However, this just shifts the problem further up the chain, because if some other component tries to get a JMS Connection on startup, then you're back where you started. You can use the lazy-init="true" attribute on your other beans to prevent this from happening on deployment, but it's easy to accidentally put something in your config which forces everything to initialize.

skaffman
A: 

You're absolutely right. I tried setting lookupOnStartup to false and lazy-init=true . This just defers the problem to the first time that the Queue is attempted to be used. Then an exception as follows is thrown:

[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

Moreover, it looks like the lookup is never attempted again. When the machine with the remote queue is brought back up, no messages are ever processed subsequently. This really does seem like it should be well within the envelope of use cases for J2EE nonsense, and yet I'm not having much luck... It feels like it should even maybe be a solved problem.

For completion's sake, the following is the pertinent portion of my Spring configuration.

<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>
Erik
Try removing lazy-init all of the definitions, and set the autoStartup of the prototypeListenerContainer definition to false. This will stop the listener container from trying to get a connection on context startup, but then you need to find a way of manually calling start() on the listener container yourself. Oh, and welcome to the awfulness that is JMS.
skaffman
IF I could just get the JNDI lookup to keep happening in the background and just let my clients go on their merry way without a server, everything would be peachy. I'll try your suggestion out. Thank you.
Erik