Search code examples
javadockeractivemq-classic

NameNotFoundException while trying to use ActiveMQ Classic


I am running ActiveMQ Classic as a Docker container. It is running on port 61616. I haven't changed any settings. I have created this example queue in the webinterface:

enter image description here

Here is the queue definition:

<queues>
  <queue name="ExampleQueue">
    <stats size="0" consumerCount="0" enqueueCount="0" dequeueCount="0"/>
    <feed>
      <atom>queueBrowse/ExampleQueue?view=rss&amp;feedType=atom_1.0</atom>
      <rss>queueBrowse/ExampleQueue?view=rss&amp;feedType=rss_2.0</rss>
    </feed>
  </queue>
</queues>

I am trying to access this queue in Java with the following code:

package messageListener;

import javax.naming.*;
import java.util.Properties;
import javax.jms.*;

public class MessageListenerSender {

    // Member-Variables
    InitialContext initialContext;
    ConnectionFactory connectionFactory;
    Connection connection;
    Session session;

    Queue queue;
    MessageProducer queueMessageProducer;

    Topic topic;
    MessageProducer topicMessageProducer;

    // Connect to Message-Broker
    public void connectToMessageBroker() throws Exception {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
        properties.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");

        initialContext = new InitialContext(properties);

        connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        
        connection = connectionFactory.createConnection();

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    // Access Point-To-Point Queue
    public void accessPointToPointQueue() throws Exception {
        queue = (Queue) initialContext.lookup("/queue/ExampleQueue");
        queueMessageProducer = session.createProducer(queue);
    }

    // Access Topic-Orientated Queue
    public void accessTopicQueue() throws Exception {
        topic = (Topic) initialContext.lookup("/topic/ExampleTopic");
        topicMessageProducer = session.createProducer(topic);
    }

    // Send Messages
    public void sendMessages() throws Exception {
        Message message0 = session.createTextMessage("Hello World!");
        queueMessageProducer.send(message0);

        Message message1 = session.createTextMessage("Goodbye World!");
        topicMessageProducer.send(message1);
    }

    // Close all Connections
    public void closeConnections() throws Exception {
        if (initialContext != null) {
            initialContext.close();
        }

        if (connection != null) {
            connection.close();
        }
    }

    public static void main(String[] args) throws Exception {
        MessageListenerSender messageListenerSender = new MessageListenerSender();

        messageListenerSender.connectToMessageBroker();
        messageListenerSender.accessPointToPointQueue();
        messageListenerSender.accessTopicQueue();
        messageListenerSender.sendMessages();
        messageListenerSender.closeConnections();
    }
}

This code produces this error:

Exception in thread "main" javax.naming.NameNotFoundException: queue/ExampleQueue
    at org.apache.activemq.jndi.ReadOnlyContext.lookup(ReadOnlyContext.java:235)
    at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
    at messageListener.MessageListenerSender.accessPointToPointQueue(MessageListenerSender.java:40)
    at messageListener.MessageListenerSender.main(MessageListenerSender.java:74)

Sadly i can't figure out what I need to change to resolve this issue. Do I use the wrong path or sth like that?

I want to set up a sender and a receiver that communicate via ActiveMQ Classic.


Solution

  • Try using

    queue = (Queue) initialContext.lookup("dynamicQueues/ExampleQueue");
    topic = (Topic) initialContext.lookup("dynamicTopics/ExampleTopic");
    

    This will connect to your queue or create it if it doesnt exists, you can check the documentation here https://activemq.apache.org/components/classic/documentation/jndi-support