【问题标题】:Active MQ, JNDI, Spring error活动 MQ、JNDI、Spring 错误
【发布时间】:2014-02-09 13:43:16
【问题描述】:

我是 JNDI 和 JMS 技术的初学者。

我的 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)

任何帮助将不胜感激。

问候。

【问题讨论】:

    标签: java spring jms activemq jndi


    【解决方案1】:

    您无法在JNDI 中找到amqConnectionFactory,因为您尝试获取具有错误JNDI 名称的对象。您可能希望从 dq_dataloader-amq.properties 获取 connectionFactoryName 属性值而不是键。

    使用${} 从属性文件中获取值。

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

    【讨论】:

    • 引起:javax.naming.NameNotFoundException:${connectionFactoryName}。再次找不到名称。抱歉,我在 JNDI 方面有点弱。但它似乎没有正确读取名称。
    • 尝试使用“queueConnectionFactory”而不是 ${connectionFactoryName} 来检查它是否有效。
    • 第一:引起:javax.naming.NameNotFoundException:queueConnectionFactory at org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235) 第二:引起:javax.naming.NameNotFoundException: ${queueConnectionFactory} 在 org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235)
    • 你确定JNDI中有对象名“queueConnectionFactory”吗?
    • 我无法确认。也就是activeMQ给出的JNDI文件。你会知道的更好。
    【解决方案2】:

    在我看来,您对连接工厂名称有一个简单的案例问题。

    应该是这样的大写Q:

    connectionFactoryName = QueueConnectionFactory
    

    而不是“queueConnectionFactory”。

    【讨论】:

      【解决方案3】:

      在 dq_dataloader-amq.properties 中

      你应该定义参数

      connectionFactoryName = queueConnectionFactory

      connectionFactoryNames = queueConnectionFactory
      

      然后更新为

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

      ActiveMQInitialContextFactory 中的代码

      protected String[] getConnectionFactoryNames(Map environment) {   
         String factoryNames = (String) environment.get("connectionFactoryNames");   
      
         if (factoryNames != null) {   
            List<String> list = new ArrayList<String>();   
      
            for (StringTokenizer enumeration = new StringTokenizer(factoryNames, ","); enumeration.hasMoreTokens();) {   
                list.add(enumeration.nextToken().trim());   
            }   
      
            int size = list.size();   
      
            if (size > 0) {   
                 String[] answer = new String[size];   
                 list.toArray(answer);   
                 return answer;   
            }   
         }   
      
         return DEFAULT_CONNECTION_FACTORY_NAMES;   
      }
      

      【讨论】:

        猜你喜欢
        • 2012-08-21
        • 1970-01-01
        • 2015-08-19
        • 2015-03-19
        • 1970-01-01
        • 2013-10-14
        • 2016-07-17
        • 2015-06-15
        • 2011-10-24
        相关资源
        最近更新 更多