JNDI does not work with HornetQ and tomcat

2019-05-19 01:17发布

问题:

I'm trying running JMS application using hornetq on tomcat! I tried following this article. I put jndi.properties in my client class path; jndi.properties:

java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory 
java.naming.factory.url.pkgs=org.apache.naming

I added these dependencies to pom.xml:

<dependency> 
    <groupId>tomcat</groupId> 
    <artifactId>naming-factory</artifactId> 
    <version>5.5.23</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
     <groupId>tomcat</groupId> 
     <artifactId>naming-resources</artifactId> 
     <version>5.5.23</version> 
     <scope>test</scope> 
 </dependency>

My JMS spring beans:

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">org.apache.naming.java.javaURLContextFactory</prop>
                <prop key="java.naming.factory.url.pkgs">org.apache.naming</prop>
            </props>
        </property>
    </bean>

<!-- Connection Factory -->
<bean id="hornetqConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate" ref="jndiTemplate"/>
    <property name="jndiName" value="/ConnectionFactory" />
</bean>

<!-- Destinations -->
<bean id="annotationDeleteCommandDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate" ref="jndiTemplate"/>
    <property name="jndiName" value="/queue/command/annotation/deleteQueue" />
</bean>

I'm using HornetQ default server (standalone, non-clustered)

hornetq-jms.xml:

<queue name="annotationDeleteCommandQueue">
    <entry name="/queue/command/annotation/deleteQueue"/>
</queue>

<connection-factory name="NettyConnectionFactory">
      <xa>false</xa>
      <connectors>
         <connector-ref connector-name="netty"/>
      </connectors>
      <entries>
         <entry name="/ConnectionFactory"/>
      </entries>
   </connection-factory>

But when i starting tomcat i get this error:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hornetqConnectionFactory' defined in ServletContext resource [/WEB-INF/classes/config/spring/applicationContext-jms.xml]: 
Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name ConnectionFactory is not bound in this Context .........

What is wrong? Should i put any jar files in the tomcat classpath? (which jars?) Should i put queues and connection factories definitions in the tomcat configs? (how?) Can i disable JNDI in tomcat and use hornetq standalone JNDI instead?

回答1:

I solved this problem by modification of jndiTemplate (using jboss naming) and adding jnp-client.jar to client classpath:

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
            <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
            <prop key="java.naming.provider.url">jnp://localhost:1099</prop>
        </props>
    </property>
</bean>


回答2:

Are you sur HornetQ is started correctly? The error log seems to indicate that the ConnectionFactory is not present.

Have you tried to access HornetQ using another tool? You should be able to use JMX or HermesJMS (http://www.hermesjms.com) to verify the presence of your ConnectionFactory