Copyright © TIBCO Software Inc. All Rights Reserved

> Publish and Subscribe Messaging Example
> 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.
> Create a Topic
> In the publish/subscribe model, you publish and subscribe to topics.
> To create a new topic in the administration tool, use the create topic command to create a new topic named myTopic:
> tcp://localhost:7222> create topic myTopic
> For more information on the create topic command, refer to create topic on page 127. For more information on the commit command, see commit on page 123 and autocommit on page 123.
> Start the Subscriber Clients
> You start the subscribers first because they enable you to observe the messages being received when you start the publisher.
> To start user1 as a subscriber:

> 1. | In the first command line window, navigate to EMS_HOME/samples/java.

> 2. | Enter setup to set the environment and classpath:

> > setup

> 3. | Execute the tibjmsMsgConsumer client to assign user1 as a subscriber to the myTopic topic:

> > java tibjmsMsgConsumer -topic myTopic -user user1
> The screen will display a message showing that user1 is subscribed to myTopic.
> To start user2 as a subscriber:

> 1. | In the second command line window, navigate to the EMS_HOME/samples/java folder.

> 2. | Enter setup to set the environment and classpath:

> > setup

> 3. | Execute the tibjmsMsgConsumer application to assign user2 as a subscriber to the myTopic topic:

> > java tibjmsMsgConsumer -topic myTopic -user user2
> The screen will display a message showing that user2 is subscribed to myTopic.

> | The command windows do not return to the prompt when the subscribers are running.

> Start the Publisher Client and Send Messages
> 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.
> To start the publisher:

> 1. | In the third command line window, navigate to the EMS_HOME/samples/java folder.

> 2. | Enter setup to set the environment and classpath:

> > setup

> 3. | Execute the tibjmsMsgProducer client to direct user1 to publish some messages to the myTopic topic:

> > java tibjmsMsgProducer -topic myTopic -user user1 hello user2
> where ’hello’ and ’user2’ are separate messages.

> | In this example, user1 is both a publisher and subscriber.

> The command line window will display a message stating that both messages have been published:
> Publishing on topic 'myTopic'
> Published message: hello
> Published message: user2
> After the messages are published, the command window for the publisher returns to the prompt for further message publishing.

> | Note that if you attempt to use the form: java tibjmsMsgProducer -topic myTopic -user user1 without adding the messages, you will see an error message, reminding you that you must have at least one message text.

> The first and second command line windows containing the subscribers will show that each subscriber received the two messages:
> Subscribing to topic: myTopic
> Received message: TextMessage={ Header={ JMSMessageID={ID:EMS-SERVER.97C44203CDF A:1} JMSDestination={Topic[myTopic]} JMSReplyTo={null} JMSDeliveryMode={PERSISTENT} JMSRedelivered={false} JMSCorrelationID={null} JMSType={null} JMSTimestamp={Tue Mar 21
> 12:04:56 PST 2006} JMSExpiration={0} JMSPriority={4} }
> Properties={} Text={hello} }
> Received message: TextMessage={ Header={ JMSMessageID={ID:EMS-SERVER.97C44203CDFA:2} JMSDestination={Topic[myTopic]} JMSReplyTo={null} JMSDeliveryMode={PERSISTENT} JMSRedelivered={false} JMSCorrelationID={null} JMSType={null} JMSTimestamp={Tue Mar 21
> 12:04:56 PST 2006} JMSExpiration={0} JMSPriority={4} }
> Properties={} Text={user2} }
> Create a Secure Topic
> 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.
> Add the secure Property to the Topic
> 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.
> To enable server authorization and add the secure property to a topic, do the following steps:

> 1. | In each subscriber window, enter Control-C to stop each subscriber.

> 2. | In the administration tool, use the set server command to enable the authorization property:

> tcp://localhost:7222> set server authorization=enabled
> The authorization property enables checking of permissions set on destinations.

> 3. | Enter the following command to add the secure property to a topic named myTopic:

> tcp://localhost:7222> addprop topic myTopic secure
> For more information on the set server command, refer to set server on page 137. For more information on the addprop topic command, refer to addprop topic on page 123.
> Grant Topic Access Permissions to Users
> To see how permissions affect the ability to publish and receive messages, grant publish permission to user1 and subscribe permission to the user2.
> Use the grant topic command to grant permissions to users on the topic myTopic.
> In the administration tool, enter:
> tcp://localhost:7222> grant topic myTopic user1 publish
> tcp://localhost:7222> grant topic myTopic user2 subscribe
> For more information on the grant topic command, refer to grant topic on page 132.
> Start the Subscriber and Publisher Clients
> 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:
> Operation not permitted.
> 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.
> Create a Durable Subscriber
> 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.
> In this example, you create a durable subscriber that stores messages published to topic myTopic on the EMS server.
> To start user2 as a durable subscriber:

> 1. | In the a command line window, navigate to the EMS_HOME/samples/java folder.

> 2. | Enter setup to set the environment and classpath:

> > setup

> 3. | Execute the tibjmsDurable application to assign user2 as a durable subscriber to the myTopic topic:

> > java tibjmsDurable -topic myTopic -user user2

> 4. | In the administration tool, use the show durables command to confirm that user2 is a durable subscriber to myTopic:

> tcp://localhost:7222> show durables
> Topic Name Durable User Msgs Size
> * myTopic subscriber user2 0 0.0 Kb

> 5. | In the subscriber window, enter Ctrl+C to stop the subscriber.

> 6. | In another command line window, execute the tibjmsMsgProducer client, as described in Start the Publisher Client and Send Messages:

> > java tibjmsMsgProducer -topic myTopic -user user1 hello user2

> 7. | Restart the subscriber:

> > java tibjmsDurable -topic myTopic -user user2
> The stored messages are displayed in the subscriber window.

> 8. | Enter Ctrl+C to stop the subscriber and then unsubscribe the durable subscription:

> > java tibjmsDurable -unsubscribe
> The subscriber is no longer durable and any additional messages published to the myTopic topic are lost.

Copyright © TIBCO Software Inc. All Rights Reserved