3

JMS のスプリング構成をセットアップしました。遅延ロードを行うことができないように見えることを除いて、問題なく動作します(以下のコードで default-lazy-init true に注意してください)。以下の構成から jmsContainer(DMLC) をコメントアウトすると、遅延読み込みが期待どおりに機能します。それ以外の場合は、DMLC がインスタンス化され、キューと接続ファクトリが作成されます。

私は何が欠けていますか?

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>

そして、それを駆動するために使用しているテスト、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() {

    }

}

jmsContainer がコメント アウトされている場合、上記のテストはパスします。それ以外の場合は、次のようになります。

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]

「connectionFactory」Bean は「jmsContainer」の依存関係としてインスタンス化され、失敗します。「jmsContainer」をコメントアウトすると、「connectionFactory」がインスタンス化されません。

jms コードは問題なく動作しますが、JNDI 名を意図的に変更して、開始時期を確認できるようにしました。

4

2 に答える 2

4

OK、これはかなりあいまいですがDefaultMessageListenerContainerLifecycleインターフェースを実装し、これを実装する Bean はコンテキスト自体のライフサイクルに結び付けられます - コンテキストが起動すると、Lifecycle実装する Bean が初期化されて開始されます。これは、lazy-init 構成が本質的に無視されることを意味します。

于 2010-12-11T00:03:52.587 に答える
1

解決策は、autoStartup を false に設定することです。以下のコードを参照してください。

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

〜シャム

于 2012-10-25T13:41:59.637 に答える