我是 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)
如有任何帮助,我们将不胜感激。
问候。
最佳答案
您无法在 JNDI
中找到 amqConnectionFactory
,因为您尝试获取具有错误 JNDI
名称的对象。您可能希望从 dq_dataloader-amq.properties
获取 connectionFactoryName
属性值而不是 key 。
使用 ${}
从属性文件中获取值。
<bean id="amqConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="${connectionFactoryName}" />
</bean>
关于java - 主动 MQ、JNDI、Spring 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21233135/