This section describes terms that you should be familiar with for developing publish and subscribe applications.
For more information about the role of the broker in publish and subscribe messaging, see Understanding the micro broker components.
What is a publisher?
A publisher is a client application that creates a publication message, associates it with a topic, and sends the message to the broker. The broker is responsible for distributing the message to all applications subscribed to that topic.
What is a publication?
A publication is a message that is associated with a particular topic. A client application publishes a publication to the broker. The broker then matches the publication to all applications that have subscribed to the publication's topic and distributes it to those applications.
If configured to do so, the broker also distributes the publication to all connected and networked brokers that have subscribers for the publication. The network can include any applications that can connect to the broker, such as WebSphere® MQ and WebSphere Message Broker.
In general, after sending a publication to all of a topic's subscribers, the broker deletes it. However, the topic publisher can request that the broker keep a copy of the publication, which is then called a retained publication
. The broker saves a retained publication and any new subscriber to its topic receives a copy of the publication. Note that only a single retained publication is saved per topic.
The advantage of retained publications is that applications making new subscriptions to the topic will immediately receive the retained publication and will not have to wait until the next message on the topic is published. This allows the application to initialize itself immediately, rather than having to wait an indeterminate amount of time until it first receives a message.
Only MQTT messages may be published as retained messages (see Messaging protocols
for more information). JMS messages do not support this function. Both MQTT and JMS clients support receiving retained publications, but JMS clients can see no difference between normal and retained publications, whereas MQTT clients can detect the difference.
What is a subscriber?
A subscriber is a client application that requests to receive messages published on a specific set of topics. The application sends one or more subscription messages to the broker. These messages define the topics in which the application is interested. The broker then sends publication messages to the subscribing application whenever a publication matching one of the set of client topics is processed. When the application no longer wants to receive publications, it can unsubscribe from topics to which it has previously subscribed.
A publish and subscribe messaging topology is a flexible arrangement that allows applications to be added and removed without impacting other applications. When you add a new subscribing application, you do not need to modify the publishing application to know that an additional subscriber has been added. Similarly, you can add new applications to publish on particular topics, without the need for subscribing applications to be modified to be made aware of this.
Of course, the topic name and the format of the data in the message have to be well defined, as these are the common pieces of information shared between a publishing and subscribing application. Once these are established, you can add or remove applications to publish or subscribe as required.
Table 1. Example topologies
|One publisher to many subscribers||One publishing application is generating publications that are used by many subscribing applications. An example is an application that publishes stock prices. Many applications might subscribe for this information, including brokers, Web sites, portfolios and many other applications.|
|Many publishers to one subscriber||The subscribing application is aggregating information from different sources together to make a decision. An example is a weather forecasting application that needs information from air pressure sensors, wind speed sensors, humidity sensors, and temperature sensors to make an accurate forecast.|
|Many publishers to many subscribers||An example of this topology is an extension of the weather forecasting example above. Here, multiple forecasting simulations might be running in parallel. Each simulation is a subscribing application aggregating data from all the publishing applications.|
When subscribing, either an absolute topic name or a topic name with wildcard characters can be used. See Topics and hierarchical topic names
for more information about topic names.
When a publisher sends a message that has multiple subscribers, the broker sends a copy of the message to each subscriber. Once a system has been deployed, new applications can be added at a later date without having to change the applications already deployed. The new applications simply subscribe to the existing topics set.
What is a subscription?
A subscription is created by a subscriber to define a set of publications that the subscriber should receive from the broker. A subscription in its simplest form consists of one or more topics.
Some clients also allow additional filtering to be performed based on properties associated with the publication. Publication property filtering is achieved using SQL syntax, so a subscriber would only receive a matching publication if the subscription topic matched the publication topic and the SQL query of the publication properties returned a true result. The SQL filter associated with a subscription is referred to as the selector.
An application can create multiple subscriptions if required.
An example use of properties in subscriptions would be an application that is only interested when a stock price rises above a certain value. Suppose the current stock price is published on topic finance/stock/ibm/currentprice
and the publication also contains a property called CURRENT_PRICE
. The subscribing application would subscribe to topic finance/stock/ibm/currentprice and specify a selector of 'CURRENT_PRICE > 90
'. This would ensure that while the stock price was below or $90, the subscriber would not receive any publications.
There are two types of subscriptions available to applications:
A non-durable subscription is one whose lifetime is limited to the duration of the connection between the subscribing application and the broker. A non-durable subscription is removed from the broker when the application disconnects or when the application explicitly unsubscribes.Durable subscriptions
A durable subscription is one whose lifetime is beyond the duration of the connection between the subscribing application and the broker. A durable subscription is only removed from the broker when the application explicitly unsubscribes. While the application is not connected, matching publication messages are stored in the broker until the application reconnects, at which time they are forwarded to the subscriber.
Publish/Subscribe messaging is supported in the micro broker using both the MQTT protocol and the JMS API; these are discussed further in Messaging protocols
Parent topic: Publish and subscribe messaging: XPD621