This section discusses the use of JMS Topic objects in WebSphere MQ classes for Java Message Service applications.
This section describes the use of topic names within WebSphere MQ classes for Java Message Service.
Topic names in WebSphere MQ JMS are arranged in a tree-like hierarchy, an example of which is shown in Figure 3.
Figure 3. WebSphere MQ classes for Java Message Service topic name hierarchy
In a topic name, levels in the tree are separated by the "/" character. This means that the "Signings" node in Figure 3 is identified by the topic name:
Sport/Football/Spurs/Signings
A powerful feature of the topic system in WebSphere MQ classes for Java Message Service is the use of wildcards. These allow subscribers to subscribe to more than one topic at a time. Different brokers use different wildcard characters and different rules for their substitution. Use the broker version property of the topic (BROKERVER) to define which type of wildcards apply.
If a subscriber subscribes to the topic represented by the following topic name:
Sport/Football/*/Results
it receives publications on topics including:
If the subscription topic is:
Sport/Football/Spurs/*
it receives publications on topics including:
If the subscription topic is:
Sport/Football/*
it receives publications on topics including:
These wildcards can only be used to stand for complete levels within a topic; that is they can only be preceded by "/" or by start-of-string, and they can only be followed by "/" or by end-of-string.
If a subscriber subscribes to the topic represented by the following topic name:
Sport/Football/+/Results
it receives publications on topics including:
If a subscriber subscribes to the topic represented by the following topic name:
Sport/#/Results
it receives publications on topics including:
Although
Sport/Football/Spur?/Results
works with broker version 1, there is no equivalent for broker version 2 which does not support single character substitutions.
There is no need to administer the topic hierarchies that you use on the broker-side of your system explicitly. When the first publisher or subscriber on a given topic comes into existence, the broker automatically creates the state of the topics currently being published on, and subscribed to.
There are four ways to create Topic objects at runtime:
The constructor takes one argument, which should be a uniform resource identifier (URI). For WebSphere MQ classes for Java Message Service Topics, this should be of the form:
topic://TopicName[?property=value[&property=value]*]
For further details on URIs and the permitted name-value pairs, see Sending a message.
The following code creates a topic for non-persistent, priority 5 messages:
// Create a Topic using the one-argument MQTopic constructor String tSpec = "Sport/Football/Spurs/Results?persistence=1&priority=5"; Topic rtTopic = new MQTopic( "topic://" + tSpec );
After the object is created, set the baseTopicName property by using the setBaseTopicName method, passing in the required topic name.
// Create a Topic using the default MQTopic constructor Topic rtTopic = new MQTopic(); // Set the object properties using the setter methods ((MQTopic)rtTopic).setBaseTopicName( "Sport/Football/Spurs/Results" ); ((MQTopic)rtTopic).setPersistence(1); ((MQTopic)rtTopic).setPriority(5);
// Create a Topic using the session factory method Topic rtTopic = session.createTopic( "topic://Sport/Football/Spurs/Results" );
Although the createTopic method is in the JMS specification, the format of the string argument is vendor-specific. Therefore, using this method may make your code non-portable.
// Create a TemporaryTopic using the session factory method Topic rtTopic = session.createTemporaryTopic();