The tour starts with features of Publish and Subscribe messaging.
This series of samples explores basic subscriptions, durable subscriptions, wildcards in topic hierarchies, filtered subscriptions, and batching in transacted sessions.
In the Chat
application, whenever anyone sends a
text message to a given topic, all active applications running
Chat
receive that message as subscribers to that
topic. This is the most basic form of publish and subscribe
activity.
To run the chat sample do the following:
In window 1, enter: ant chat1
, then
type Hello
,
and press Enter.
Window 1 displays:
Chatter_1: Hello
In window 2, enter: ant chat2
, then
type Pronto
, and press
Enter.
Both subscribers get the message so both windows display:
Chatter_2: Pronto
In window 3, enter: ant chat3
, then
type Bonjour
, and press
Enter.
All three subscribers get the message, so each window displays:
Chatter_3: Bonjour
In window 3, stop chat3
by pressing
Ctrl+C.
Send some messages in the chat1
and
chat2
windows.
In window 3, run: ant chat3
again.
Send some messages in the chat1
and
chat2
windows.
All three subscribers get the message. But
Chatter_3
gets only the messages since it
reconnected, and gets none of the messages that were sent while
it was disconnected.
If subscribers miss some of the messages, they pick up just the latest messages whenever they reconnect to the broker. Nothing is retained and nothing is guaranteed to be delivered, so throughput is fast.
In Pub/Sub messaging, when messages are produced, they are sent to all active consumers who subscribe to a topic. Some subscribers register an enduring interest in receiving messages that were sent while they were inactive. These durable subscriptions are permanent records in the broker’s persistent storage mechanism.
To run the DurableChatting sample do the following:
In window 1, enter ant durable1
.
In window 2, enter ant durable2
.
Type text in each window and press Enter. Each window displays all the messages. Connected durable subscribers are the same as connected nondurable subscribers.
In window 2, press Ctrl+C.
In window 1, type text, and press Enter.
In window 2, enter ant durable2
again.
When the window opens, it first displays all the messages that were stored for its subscription. (If you waited a while, messages sent more than 30 minutes ago were dropped.)
In window 3, enter ant durable3
.
In the console window that opens, no messages are displayed. While it is a durable subscriber, it had not yet established its durable subscription on this broker topic.
![]() | Important |
---|---|
If this sample has run against this broker before this run, the durable interest has been established so you do get the stored messages. You could unsubscribe the user from the durable subscription, but in the scope of this JMS exploration, you might find it as easy to close all the open windows, delete the /data directory, and then restart the exploration. |
![]() | Important |
---|---|
While some applications tolerate multiple instances of the same
user producing or consuming on the same destination, running a
second instance of one of these
|
FUSE Message Broker supports a hierarchical topic structure that allows wildcard subscriptions.
Each application instance specifies a publish topic and a subscribe topic, as follows:
Chat:
Publish to jms.samples.chat
Subscribe to
jms.samples.chat
DurableChat:
Publish to
jms.samples.durablechat
Subscribe to
jms.samples.durablechat
HierarchicalChat:
Publish to
jms.samples.hierarchicalchat
Subscribe to jms.samples.*
You can see that each of the applications is publishing to a different
topic. However, the HierarchicalChat
application is
subscribing to a topic that ends in asterisk (*
), a wildcard that accepts any
topic with the root jms.samples
. It is important to
note that this is referred to a hierarchical wildcard, as it must be
adjacent to dot delimiters.
To run the HierarchicalChatting sample do the following:
In window 1, enter ant chat1
In window 2, enter ant durable1
In window 3, enter ant wildcard
In the wildcard window, enter some text and then press Enter.
The message is displayed in only that window.
In the chat1 window, enter some text and then press Enter.
The message is displayed in that window and in the wildcard window.
In the durable1 window, enter some text and then press Enter.
The message is displayed in that window and in the wildcard window.
The MessageMonitor
sample application provides an
example of a supervisory application with a graphical interface. By
subscribing with the wildcard syntax used in the
HierarchicalChat
, the monitor gets messages on
all topics in the topic hierarchy. The application listens for any
message activity, and then displays each message in its window.
To start the MessageMonitor sample, enter ant
tmonitor
in window 3.
The MessageMonitor Java window opens.
To send messages to the MessageMonitor do the following:
In window 1, type Hello
, and press
Enter.
The message displays in the
MessageMonitor window, noting that the
message was received on the jms.samples.chat
topic
In window 2, type Hello
, and press
Enter.
The message displays in the
MessageMonitor window, noting that the
message was received on the
jms.samples.durablechat
topic.
Because the MessageMonitor subscribes
to the topic jms.samples.*
, messages are
received from both publishers. The Chat
and
DurableChat
applications subscribe to
only their respective topics.
Click the
button to empty the listed messages.While specific queues and topics provide focused content nodes for
messages that are of interest to an application, there are circumstances
where the application developer might want to qualify the scope of
interest a consumer has in messages using a syntax similar to an SQL
WHERE
clause.
In the SelectorChat
samples, each of the
SelectorChat
samples publishes to the
jms.samples.chat
topic with messages that have a
property set to a different value, and then sets the subscriber to
select only messages set to the appropriate property value.
To run the SelectorChatting samples do the following:
In window 1, enter ant
filterchat1
.
In window 2, enter ant
filterchat2
.
In window 3, enter ant chat1
.
In the SelectiveChatter_1 window, enter some text and then press Enter.
The message is displayed in that window and the Chatter_1 window.
In the SelectiveChatter_2 window, enter some text and then press Enter.
The message is displayed in that window and the Chatter_1 window.
In the Chatter_1 window, enter some text and then press Enter.
The message is displayed only in that window.
Transacted messages are a group of messages that form a single unit of work. Much like an accounting transaction made up of a set of balancing entries, a messaging example might be a set of financial statistics where each entry is a completely formed message and the full set of data comprises the update.
A session is declared as transacted when the session is created. While producers—PTP Senders and Pub/Sub Publishers—produce messages as usual, the messages are stored at the broker until the broker is notified to act on the transaction by delivering or deleting the messages. To determine when the transaction is complete, the programmer must:
Call the method to commit the set of
messages. The session's commit( )
method tells the broker to sequentially release each of the
messages that have been cached since the last transaction. In
this sample, the commit case is set for the string
COMMIT
.
Call the method to roll back the set of
messages. The session's rollback( )
method tells the broker to flush all the messages that have been
cached since the last transaction ended. In this sample, the
rollback case is set for the string
CANCEL
.
To run the Pub/Sub TransactedChat samples do the following:
In window 1, enter ant xnchat
.
In the console window that opens, type text in the window and press Enter.
Type COMMIT
in the window and press
Enter.
The message is delivered.
Type text in the window and press Enter.
Repeat to produce a few messages.
Type COMMIT
in the window and press
Enter.
The batch of messages is delivered as a series of individual messages.
Type text in the window and press Enter.
Repeat to produce a few messages.
Type CANCEL
in the window and press
Enter.
The batch of messages is dropped.
Subsequent entries will form a new transaction to either commit or rollback.