2014-01-20 37 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”。