我正在尝试从Junit测试用例将消息推送到JMS队列,但我一直收到错误:
javax.naming.NameNotFoundException: Name [ConnectionFactory] not bound
以下是测试用例代码:
public void testPushMessagesIntoQueue() {
JmsTemplate jmsTemplate = (JmsTemplate) getBean("batchMessageTemplate"); //ERROR HERE
assertNotNull(jmsTemplate);
try {
jmsTemplate.convertAndSend("batchQueue", messageList);
} catch(Exception ex ) {
ex.printStackTrace();
}
}
public ApplicationContext getContext() {
return new FileSystemXmlApplicationContext("/src/main/webapp/WEB-INF/service-context.xml");
}
public Object getBean(String beanName) {
return getContext().getBean(beanName);
}
这是我在service-context.xml文件中的连接工厂bean:
<bean name="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"/>
<property name="jndiName" value="ConnectionFactory" />
</bean>
我的jndiTemplate bean:
<bean id="jndiTemplate" class="com.xxx.batch.common.util.CustomJndiTemplate">
</bean>
在class customJndiTemplate中,我将属性设置为
public CustomJndiTemplate() throws JobException {
Properties environment = new Properties();
environment.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
environment.setProperty("java.naming.provider.url", "jnp://localhost:1099");
environment.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
setEnvironment(environment);
}
我的batchMessageTemplate bean:
<bean name="batchMessageTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="batchDestination" />
<property name="receiveTimeout" value="1" />
</bean>
在我的Jboss4中的Jbossmq-destinations-service.xml中,我定义了队列:
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=batchQueue">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
当我在启动jboss 4后运行测试用例时,我收到错误:
Error creating bean with name 'jmsConnectionFactory' defined in file [service-context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [ConnectionFactory] not bound;
只有在运行我的Junit测试用例时才会出现此问题。定义了一个在Jboss中运行的servlet,用于向队列添加消息,并且工作正常
我是JMS的新手,所以任何帮助都会受到赞赏。