This example shows you how to send and receive a message to a JMS Topic with ActiveMQ Artemis.
Topics are a standard part of JMS, please consult the JMS 1.1 specification for full details.
A Topic is used to send messages using the publish-subscribe model, from a producer to 1 or more consumers.
To run the example, simply type mvn verify -Pexample
from this directory
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext();
Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(topic);
MessageConsumer messageConsumer = session.createConsumer(topic);
MessageConsumer messageConsumer2 = session.createConsumer(topic);
TextMessage message = session.createTextMessage("This is a text message");
messageProducer.send(message);
connection.start();
TextMessage messageReceived = (TextMessage) messageConsumer1.receive();
messageReceived = (TextMessage) messageConsumer2.receive();
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}