Search code examples
javatomcatjakarta-eejndiactivemq-artemis

Connecting Tomcat to standalone Artemis Broker via JNDI


I want to define a jms ConnectionFactory as global resource in tomcat server.xml to connect my webapps to standalone Artemis server.

Unfortunately I couldn't find any official or clean manual for such a common use case that describes required libraries and configurations.

Can somebody show me an example?


Solution

  • I couldn't find any native Artemis based solution. Artemis server is ActiveMQ client compatible. So we can follow ActiveMQ manual for this purpose.

    • Add ActiveMQ client libraries to $CATALINA_HOME/lib

      • geronimo-jms_2.0_spec-1.0-alpha-2.jar
      • activemq-client-5.14.3.jar
      • geronimo-j2ee-management_1.1_spec-1.0.1.jar
      • hawtbuf-1.11.jar
      • slf4j-api-1.7.6.jar
    • Add global resource to $CATALINA_HOME/conf/server.xml

      <Resource auth="Container"
          name="jms/ConnectionFactory"
          type="org.apache.activemq.ActiveMQConnectionFactory"
          factory="org.apache.activemq.jndi.JNDIReferenceFactory"
          brokerURL="tcp://127.0.0.1:61616"
          brokerName="MyActiveMQBroker"/>
      
    • Add resource link to $CATALINA_HOME/conf/context.xml

      <ResourceLink name="/ConnectionFactory" 
          global="jms/ConnectionFactory" 
          type="javax.jms.ConnectionFactory"/>
      
    • Look up connection factory in app context

      <bean id="jmsConnectionFactory" 
          class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName" value="java:comp/env/ConnectionFactory" />
          <property name="proxyInterface" value="javax.jms.ConnectionFactory"/>
      </bean>