hell_storm2004
hell_storm2004

Reputation: 1605

ActiveMQ JMS Connection Error with Tomcat

I have currently configured my Tomcat's context.xml like this:

<Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="tcp://MY_LOCALHOST_URL:7270" brokerName="LocalActiveMQBroker" useEmbeddedBroker="false"/>
    
<Resource name="AppJms-HVDIVD17CA50359" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="APP.QUEUE" />

I have my activemq.xml:

<transportConnectors>
    <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
    <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    <transportConnector name="appjms" uri="tcp://MY_LOCALHOST_URL:7270?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
</transportConnectors>

And I am starting the JMS in Java Code like this:

public void createMessageSubscriberJms(String host, int port, String jmsDestination) throws JMSException, UnknownHostException {
    
    String hostname = InetAddress.getLocalHost().getCanonicalHostName();
    String providerEndpoints = "tcp://" + hostname + ":" + port + "?wireFormat.maxInactivityDuration=7200000";

    // Set the trusted packages/classes to move back and forth on the ActiveMQ JMS service.
    ArrayList<String> trustedClasses = new ArrayList<String>();

    trustedClasses.add("com.gtt.common.shared.GTCMessage");

    // Obtain the factory
    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();

    activeMQConnectionFactory.setBrokerURL(providerEndpoints);

    // Add the trusted packages/classes to the ActiveMQ consumer.
    //activeMQConnectionFactory.setTrustedPackages(trustedClasses);
    activeMQConnectionFactory.setTrustAllPackages(true);

    //Create the connection
    setQueueConnection(activeMQConnectionFactory.createQueueConnection());
    getQueueConnection().setClientID(this.getName());

    // Make a session
    setSession(getQueueConnection().createQueueSession(false, Session.AUTO_ACKNOWLEDGE));

    getSession().createQueue(jmsDestination);

    // Create the destination
    Destination destination = getSession().createQueue(jmsDestination);

    String selector = "JMSCorrelationID = '" + getActionRequest().getOriginId() + "_" + getActionRequest().getRequestId() + "'" ;

    setConsumer(getSession().createConsumer(destination, selector));
    getConsumer().setMessageListener(new DefaultMessageListener(this));

    // Start ...
    // We'll need a message store now
    gtcMessages = new GTCMessageQueue<GTCMessage>();

    getQueueConnection().start();
}

But when I start Tomcat and the method is invoked I get the following error:

Name [AppJms-HVDIVD17CA50359] is not bound in this Context. Unable to find [AppJms-HVDIVD17CA50359].

Can you please tell me what I am doing wrong? I am sure the same configuration worked when I was on an older version of Tomcat and ActiveMQ.

Currently I am using Tomcat 9.0.45 and ActiveMQ 5.16.1.

Upvotes: 0

Views: 1238

Answers (1)

Justin Bertram
Justin Bertram

Reputation: 34988

Your definition of AppJms-HVDIVD17CA50359 looks a little strange to me:

<Resource name="AppJms-HVDIVD17CA50359" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="APP.QUEUE" />

It looks like you're defining a JMS queue since the physicalName attribute is APP.QUEUE. However, the type is org.apache.activemq.ActiveMQConnectionFactory which is the proper type for a JMS connection factory, not a queue. Furthermore, the org.apache.activemq.ActiveMQConnectionFactory class doesn't have a physicalName attribute. I think you should be using org.apache.activemq.command.ActiveMQQueue here for the type, e.g.:

<Resource name="AppJms-HVDIVD17CA50359" auth="Container" type="org.apache.activemq.command.ActiveMQQueue" factory="org.apache.activemq.jndi.JNDIReferenceFactory" physicalName="APP.QUEUE" />

It's worth noting that you don't appear to be using AppJms-HVDIVD17CA50359 or jms/ConnectionFactory in this code. I don't see any JNDI lookups using these names. Therefore, it's possible you could simply remove these resource definitions from your context.xml (assuming nothing else in your application uses them) and that would likely resolve the error as well.

That said, I would encourage you to use these resources as it will make it much easier to update your application in the future because if you need to change the connection factory or queue configuration then you'll only need to modify context.xml rather than your application's code. This is one of the major benefits of running your application in a server like Tomcat.

Upvotes: 1

Related Questions