ActiveMQ supports topic hierarchies. With a topic hierarchy you can register a subscriber with a wild-card and that subscriber will receive any messages routed to an address that match the wildcard.
ActiveMQ wild-cards can use the character '#' which means "match any number of words", and the character '*' which means "match a single word". Words are delimited by the character "."
For example if I subscribe using the wild-card "news.europe.#", then that would match messages sent to the addresses "news.europe", "news.europe.sport" and "news.europe.entertainment", but it does not match messages sent to the address "news.usa.wrestling"
For more information on the wild-card syntax please consult the user manual.
To run the example, simply type mvn verify -Pexample
from this directory
In this example we will define a hierarchy of topics in the file activemq-jms.xml
<topic name="news">
<entry name="/topic/news"/>
</topic>
<topic name="news.usa">
<entry name="/topic/news.usa"/>
</topic>
<topic name="news.usa.wrestling">
<entry name="/topic/news.wrestling"/>
</topic>
<topic name="news.europe">
<entry name="/topic/news.europe"/>
</topic>
<topic name="news.europe.sport">
<entry name="/topic/news.europe.sport"/>
</topic>
<topic name="news.europe.entertainment">
<entry name="/topic/news.europe.entertainment"/>
</topic>
Then we will create a subscriber using the wildcard "news.europe.#".
We will then send three messages: one to the address news.usa.wrestling, one to the address news.europe.sport, and one to the address news.europe.entertainment.
We will verify that the message sent to news.usa.wrestling does not get received since it does not match, but the messages sent to the other two addresses do get received since they match.
initialContext = getContext(0);
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topicSubscribe = ActiveMQJMSClient.createActiveMQTopic("news.europe.#");
MessageConsumer messageConsumer = session.createConsumer(topicSubscribe);
MessageProducer producer = session.createProducer(null);
Topic topicNewsUsaWrestling = ActiveMQJMSClient.createActiveMQTopic("news.usa.wrestling");
Topic topicNewsEuropeSport = ActiveMQJMSClient.createActiveMQTopic("news.europe.sport");
Topic topicNewsEuropeEntertainment = ActiveMQJMSClient.createActiveMQTopic("news.europe.entertainment");
TextMessage messageWrestlingNews = session.createTextMessage("Hulk Hogan starts ballet classes");
producer.send(topicNewsUsaWrestling, messageWrestlingNews);
TextMessage messageEuropeSport = session.createTextMessage("Lewis Hamilton joins European synchronized swimming team");
producer.send(topicNewsEuropeSport, messageEuropeSport);
TextMessage messageEuropeEntertainment = session.createTextMessage("John Lennon resurrected from dead");
producer.send(topicNewsEuropeEntertainment, messageEuropeEntertainment);
connection.start();
TextMessage messageReceived1 = (TextMessage)messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived1.getText());
TextMessage messageReceived2 = (TextMessage)messageConsumer.receive(5000);
System.out.println("Received message: " + messageReceived2.getText());
Message message = messageConsumer.receive(1000);
if (message != null)
{
return false;
}
System.out.println("Didn't received any more message: " + message);
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}