1

I am using Java SE. I create a topic when the app first starts like so:

connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
conn = connectionFactory.createTopicConnection();
session = conn.createTopicSession(false,
                TopicSession.AUTO_ACKNOWLEDGE);
conn.start();
session.createTopic(name);

I am confused on how to retrieve this Topic in my classes. Say for example I have a class, and it connects to the JMS Service just like above using:

connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
conn = connectionFactory.createTopicConnection();
session = conn.createTopicSession(false,
                    TopicSession.AUTO_ACKNOWLEDGE);
conn.start();

How can I then get a reference to the Topic I created a app startup to send messages?

I would imagine something along the lines of:

session.getTopic(name);

Would exist, but I cant find anything on it.

I have read how to do this using Java EE using JNDI lookup, but this service is not available to me since I am not running in a container.

user489041
  • 27,916
  • 55
  • 135
  • 204

1 Answers1

2

You don't 'retrieve' a topic. A Topic instance is just a piece of information. You construct an instance of it in your client if you want to subscribe to a topic (or queue), like is demonstrated in the ActiveMQ hello world example:

http://activemq.apache.org/hello-world.html

ex:

// the name should of course be the same as it exists on the producer side
Destination destination = session.createTopic("TEST.FOO");

// Create a MessageConsumer from the Session to the Topic or Queue
MessageConsumer consumer = session.createConsumer(destination);

This is all governed by the standardized and very mature JMS API, so you should refer to the JEE documentation. Any book on JMS will work for you as well.

Further reading: https://docs.oracle.com/javaee/6/tutorial/doc/bncdr.html API docs: http://docs.oracle.com/javaee/6/api/javax/jms/package-summary.html

Gimby
  • 5,095
  • 2
  • 35
  • 47