2014-01-20 36 views
1

我是JNDI和JMS技術的入門者。Active MQ,JNDI,彈出錯誤

我有我的JNDI文件:

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory 

# use the following property to configure the default connector 
java.naming.provider.url = nio://localhost:61616 

# use the following property to specify the JNDI name the connection factory 
# should appear as. 
#jms.connectionFactoryNames = ConnectionFactory, queueConnectionFactory, topicConnectionFactry 
connectionFactoryName = queueConnectionFactory 
#connectionfactory.amqConnectionFactory = nio://localhost:61616 

# register some queues in JNDI using the form 
# queue.[jndiName] = [physicalName] 
queue.requestQueue = dq-dataloader.requestqueue 


# register some topics in JNDI using the form 
# topic.[jndiName] = [physicalName] 
#topic.MyTopic = example.MyTopic 

我的Spring配置文件是:

<?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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- <context:component-scan base-package="com.qpid.sample" /> --> 

    <bean id="jndiProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="location" value="classpath:dq_dataloader-amq.properties" /> 
    </bean> 

    <!-- JNDI template --> 
    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
     <property name="environment" ref="jndiProperties" /> 
    </bean> 


    <!-- local ActiveMQ connection factory from JNDI context available via jndiTemplate --> 
    <bean id="amqConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiTemplate" ref="jndiTemplate" /> 
     <property name="jndiName" value="connectionFactoryName" /> 
    </bean> 

    <!-- caching connection factory --> 
    <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> 
     <property name="targetConnectionFactory" ref="amqConnectionFactory" /> 

     <!-- set the session cache size --> 
     <property name="sessionCacheSize" value="10" /> 
    </bean> 

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

</beans> 

祈求類:

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.jndi.JndiTemplate; 

public class JMSSender { 

    private static Log logger = LogFactory.getLog(JMSSender.class); 
    JndiTemplate jndiTemplate; 

    public void init() { 
     ApplicationContext context = new ClassPathXmlApplicationContext("dq-dataloader-amq-beans.xml"); 
     jndiTemplate = (JndiTemplate) context.getBean("jndiTemplate"); 
     logger.info(""+jndiTemplate.getEnvironment().getProperty("java.naming.provider.url")); 

    } 

    public static void main(String [] argv) { 
     JMSSender sender = new JMSSender(); 
     sender.init(); 
    } 
} 

但是,當我試圖初始化它們我收到此錯誤:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amqConnectionFactory' defined in class path resource [dq-dataloader-amq-beans.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: connectionFactoryName 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
    at com.jpmorgan.cri.dqaf.amq.jms.JMSSender.init(JMSSender.java:15) 
    at com.jpmorgan.cri.dqaf.amq.jms.JMSSender.main(JMSSender.java:24) 
Caused by: javax.naming.NameNotFoundException: connectionFactoryName 
    at org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235) 
    at javax.naming.InitialContext.lookup(InitialContext.java:392) 
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) 
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) 

任何幫助,將不勝感激。

問候。

回答

1

由於您嘗試使用錯誤的JNDI名稱獲取對象,因此無法在JNDI中找到amqConnectionFactory。您可能想從dq_dataloader-amq.properties中獲取connectionFactoryName屬性值,而不是密鑰。使用${}從屬性文件中獲取值。

<bean id="amqConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiTemplate" ref="jndiTemplate" /> 
    <property name="jndiName" value="${connectionFactoryName}" /> 
</bean> 
+0

引起:javax.naming.NameNotFoundException:$ {connectionFactoryName}。名稱未找到。對不起,但我在JNDI有點弱。但它看起來不正確地讀取名稱。 – Vivek

+0

嘗試使用「queueConnectionFactory」而不是$ {connectionFactoryName}來檢查它是否工作。 –

+0

它是做什麼的?同樣的錯誤? –

1

在我看來,你有一個關於連接工廠名稱的簡單案例問題。

它應是一個上情況下,Q是這樣的:

connectionFactoryName = QueueConnectionFactory 

代替 「的QueueConnectionFactory」。