Question

Je configuration ai une configuration de ressort pour JMS. Les choses fonctionnent bien, sauf que je ne peux pas sembler l'obtenir à la charge paresseux (avis le vrai-init paresseux par défaut dans le code ci-dessous). Si je commente le jmsContainer (DCTM) de ma config ci-dessous, les travaux de chargement paresseux comme prévu. Dans le cas contraire, il instancier la DCTM, qui à son tour crée la file d'attente et de l'usine connexion.

Qu'est-ce que je manque?

jmsContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-lazy-init="true">

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
                <prop key="java.naming.provider.url">t3:localhost:7001</prop>
            </props>
        </property>
    </bean>

    <bean id="queue" class="org.springframework.jndi.JndiObjectFactoryBean"
          p:jndiTemplate-ref="jndiTemplate" p:jndiName="jms/queue"/>

    <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"
          p:jndiTemplate-ref="jndiTemplate" p:jndiName="jms/connectionfactory"/>

    <bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver"
        p:jndiTemplate-ref="jndiTemplate" p:cache="true" />

    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"
          p:autoStartup="false"
          p:destination-ref="queue"
          p:destinationResolver-ref="jmsDestinationResolver"
          p:connectionFactory-ref="connectionFactory"
          p:messageListener-ref="queueListener" />

    <bean id="queueListener" class="com.blah.QueueListener"/>


</beans>

Et le test que je utilise pour conduire, DummyTest.java:

package blah;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:jmsContext.xml")
public class DummyTest {

    @Test
    public void shouldDoSomething() {

    }

}

Quand jmsContainer est commentée, le test ci-dessus passe. Sinon, je reçois ceci:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'jmsContainer' defined in class path resource [com/blah/config/jmsContext.xml]: 
Cannot resolve reference to bean 'connectionFactory' while setting bean property 'connectionFactory'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'connectionFactory' defined in class path resource [com/blah/config/jmsContext.xml]: 
Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: 
Exception in lookup.: `jms/connectionfactory' could not be found. 
[Root exception is weblogic.corba.cos.naming.NamingContextAnyPackage.NotFound: IDL:weblogic/corba/cos/naming/NamingContextAny/NotFound:1.0]

Le haricot « connectionFactory » s'instancié comme une dépendance de « jmsContainer » et il échoue. Avec "jmsContainer" commenté, "connectionFactory" ne soit pas instancié.

Le code jms fonctionne très bien, mais je l'ai rebaptisé mes noms JNDI exprès pour que je puisse voir quand les choses commencer.

Était-ce utile?

La solution

OK, cela est assez obscur, mais DefaultMessageListenerContainer met en œuvre l'interface Lifecycle, et les haricots qui implémentent ce sont liés au cycle de vie propre du contexte - lorsque le contexte démarre, les haricots de Lifecycle-mise en œuvre sont initialisés et a commencé. Cela signifie que votre config-init paresseux est essentiellement ignoré.

Autres conseils

La solution consiste à utiliser autoStartup false. Voir le code ci-dessous.

<bean id="listenerContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
     ........
    <property name="autoStartup" value="false"/>
</bean>

~ Shyam

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top