Publish and Subscribe Example


This section describes an example of publishing and subscribing, using the administration tool and the client samples.

Overview of the Example

In general, to run a client sample, you enter appropriate commands in the sample console window and the EMS administration tool. For more information on the sample clients, see the Readme in the sample folder. For further information on the administration tool, see Starting the Administration Tool and Command Listing.

In this chapter, you will use the client samples. You will create a topic and then create two users for that topic. You will give one user permission to subscribe on that topic, and the other user permission to publish on that topic. Then you will publish two messages, and observe as they are received.

This example is simple and limited. For example, in a real application, the users would be connections to working applications, while in this example, users only send and receive messages. This example also does not attempt to include examples of all permissions, properties or commands, and it does not describe any of the queue commands. Nevertheless, the example can give a partial overview of the process.

Creating a Topic

In this section, you will create a topic. Several topics are included with the client samples. For this sample, however, you will create a new topic. You create topics in the EMS administration tool.

You must start the server and the administration tool before creating a topic. For information on running the server, refer to Running the Server. For information on starting the administration tool, refer to Starting the Administration Tool.

Using the Administration Tool

To begin using the administration tool, see Starting the Administration Tool.

On a computer running Windows, you can also start the administration tool from the Start menu, following the path Programs>TIBCO Enterprise Message Service 4.3>Start EMS administration tool.

When you have started the administration tool, you need to connect it to the EMS server.

To connect the EMS administration tool to the EMS server, execute one of the following commands:

Creating the Topic

Once you have connected the administration tool to the server, you can create a new topic.

To create a new topic:

  1. Startup the administration tool, then enter the following command:
  2.    create topic foo 
    
  3. Enter the commit command to save the topic as a permanent topic.
  4. For more information on the create topic command, refer to create topic. For more information on the commit command, see commit and autocommit.

Adding the secure Property to the Topic

You will add the secure property to the topic. With the secure property added, 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. For more information on topic permissions, see Chapter 9, Authentication and Permissions.

To enable server authorization and add the secure property to a topic, do the following steps:

  1. Start the server. For more information on starting the server, refer to Starting the Server.
  2. Startup the administration tool.
  3. Enable the authorization property by entering the following command:
  4.    set server authorization=enabled 
     

    The authorization property enables checking of permissions set on destinations.

  5. Enter the following command to add the secure property to a topic named foo:
  6.    addprop topic foo secure 
     

    For more information on the addprop topic command, refer to addprop topic .

Creating Users

Using the client samples, you will create several users and give them various permissions to publish and subscribe to the topic foo.

The first step is creating the users.

Creating the Users

This section illustrates creating users with the administration tool.

To create two users:

  1. Startup the administration tool, then enter the following command:
  2.    create user user1 
     

    The tool will display the message: user user1 has been created.

  3. Enter the command:
  4.    create user user2 
    
  5. Enter the commit command.

You have now created two users.

For more information on the create user command, refer to create user.

Granting Permissions

In order to see how permissions affect the ability to publish and receive messages, you will give the two users various permissions on the topic foo. You will give publish permission to one user, subscribe permission to the second user, and both publish and subscribe to the third user.

To grant permissions to users on the topic foo:

  1. Startup the administration tool, then enter the following commands:
  2.    grant topic foo user1 publish 
       grant topic foo user2 subscribe 
    
  3. Enter the commit command to save the permissions.

For more information on the grant topic command, refer to grant topic.

Next, you will use the client samples to publish and receive on topic foo.

Publishing and Subscribing

In previous sections, you have created the topic foo, assigned the property secure to that topic. You also made two users, and assigned permissions to publish and subscribe on foo to these users. In this section, you will use the client samples to publish and subscribe on foo.

To publish and subscribe on foo, you will use the client samples.

Running Client Samples

In order to run the client samples, you must give them commands from within the sample directory. The sample directory contains the compiled samples.

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 Files.

Starting Subscribers

First, you will attempt to start both of your users as subscribers on foo. Because only one user has permission to subscribe on foo, only one of the users will actually start as a subscriber.

You will start the subscribers first, because the subscribers enable you to observe the messages being received when you start the publisher.

To start a subscriber:

  1. Set a command line window and navigate to the folder containing the client samples.
  2. At the prompt, enter: setup
  3. Entering setup sets the environment and classpath, and returns you to the prompt.

  4. After the prompt, enter:
  5.    java tibjmsTopicSubscriber -topic foo -user user1 
     

    However, in this example, foo is a secure topic, and user1 has permission to publish, but not to subscribe. Therefore, you will receive an exception message including the statement:

    Operation not permitted.

    The window will then return to the prompt.

  6. After the prompt, enter:
  7.    java tibjmsTopicSubscriber -topic foo -user user2 
     

    The screen will display a message showing that user2 is subscribed to foo.

    In this example, foo is a secure topic, and user2 has permission to subscribe to foo.

    The window does not return to the prompt, because the subscription is running, and no further action needs to be taken.

Starting the Publisher and Sending 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. Set a command line window and navigate to the folder containing the client samples.
  2. At the prompt, enter: setup
  3. Entering setup sets the environment and classpath, and returns you to the prompt.

  4. After the prompt, enter:
  5.    java tibjmsTopicPublisher -topic foo -user user1 m1 m2 
     

    where m1 and m2 are messages.

The command line window containing the publisher will display a message stating that both messages have been published. The command line windows containing the subscriber will show the two messages being received.

After the messages are published, this window returns to the prompt for further message publishing.

Note that if you attempt to use the form:

java tibjmsTopicPublisher -topic foo -user user1 

without adding the messages, you will see an error message, reminding you that you must have at least one message text.


TIBCO Enterprise Message Service™ User’s Guide
Software Release 4.3, February 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com