In this section, you will execute a message producer client and two message consumer clients that demonstrate the publish/subscribe messaging model described in
Publish and Subscribe. This example is not intended to be comprehensive or representative of a robust application.
To execute the client samples, you must give them commands from within the sample directory that contains the compiled samples. For this exercise, open three separate command line windows and navigate to the
EMS_HOME/samples/java directory in each window.
For more information on the samples, refer to the readme within the sample directory. For more information on compiling the samples, refer to
Compiling the Sample Clients.
To create a new topic in the administration tool, use the create topic command to create a new topic named
myTopic:
For more information on the create topic command, refer to
create topic on page 125. For more information on the
commit command, see
commit on page 121 and
autocommit on page 121.
Setting up the publisher is very similar to setting up the subscriber. However, while the subscriber requires the name of the topic and the user, the publisher also requires messages.
In this example, you make myTopic into a
secure topic and grant
user1 permission to publish to the
myTopic and
user2 permission to subscribe to
myTopic.
When the secure property is added to a topic, only users who have been assigned a certain permission can perform the actions allowed by that permission. For example, only users with publish permission on the topic can publish, while other users cannot publish.
If the secure property is not added to a topic, all authenticated users have all permissions (publish, subscribe, create durable subscribers) on that topic.
For more information on the secure property, see the section about
secure, page 68. For more information on topic permissions, see
Chapter 8, Authentication and Permissions.
For more information on the set server command, refer to
set server on page 135. For more information on the
addprop topic command, refer to
addprop topic on page 121.
Use the grant topic command to grant permissions to users on the topic
myTopic.
Start the subscribers, as described in Start the Subscriber Clients. Note that you cannot start
user1 as a subscriber because user1 has permission to publish, but not to subscribe. As a result, you receive an exception message including the statement:
User2 should start as a subscriber in the same manner as before.
You can now start user1 as the publisher and send messages to
user2, as described in
Start the Publisher Client and Send Messages.
As described in Publish and Subscribe, subscribers, by default, only receive messages when they are active. If messages are published when the subscriber is not available, the subscriber does not receive those messages. You can create durable subscriptions, where subscriptions are stored on the server and subscribers can receive messages even if it was inactive when the message was originally delivered.