Just as projects restrict the boundaries of where messages are sent, subjects partition the flow of messages within a project. A subject is a logical message address that can be thought of as providing a virtual connection between RTclients. Subjects allow an RTclient to publish a message to multiple processes with a single operation. Subjects are designated by a name, which can be any character string with a few restrictions.
A message in SmartSockets has both a sender and a destination property. (See the TIBCO SmartSockets User’s Guide for a full discussion of message properties.) When TipcConn peer-to-peer methods are used to send messages through connections, the sender and destination properties are not used. There are no predefined values for these properties when working with peer-to-peer connections.
For RTserver to RTclient communication, however, subjects specify the sender and destination properties. When an RTclient subscribes to a subject, it receives any published messages whose destination property is set to that subject. Think of this as the process signing up for messages sent to a particular subject. For example, in a network monitoring application, you might partition messages by the types of items to be monitoredrouters, bridges, switches, and so on. These areas can be declared as subjects such as /router
, /bridge
, and /switch
. All messages pertaining to routers are constructed with the /router
subject as their destination property. Any RTclient interested in receiving messages about routers subscribes to the /router
subject. This is also known as the publish-subscribe paradigm because RTclients publish messages to specific subjects and subscribe to subjects in which they are interested.
The SmartSockets publish-subscribe communications model allows an RTclient to effortlessly publish messages to multiple receivers. Simply by specifying a subject in the destination
property, you ensure that RTserver routes the message to all other RTclients in the same project that are subscribed to that subject. The RTclients can subscribe to or unsubscribe from subjects at any time, which allows the RTclients to control the quantity of incoming messages.
To provide greater flexibility and scalability for large applications, SmartSockets subject names are arranged in a hierarchical namespace, much like UNIX file names or World Wide Web URLs. This hierarchical namespace allows a large numbers of subject names to be created with similar, but not conflicting, names; for example, /stocks/NYSE/computer
and /stocks/NASDAQ/gold
. Many powerful operations, such as publish-subscribe with wildcards, can be performed in this namespace model. Small SmartSockets systems can be easily built without requiring large amounts of complexity, and large systems can also be more easily built with these hierarchical subject names.
A hierarchical subject name consists of components laid out left-to-right, separated by slashes (/). Each component can contain any characters except a slash, an asterisk (*), or an ellipsis (...), the latter two of which are used for publish-subscribe wildcards. This list represents some examples of hierarchical subject names:
The hierarchy can be specified to any depth. For more details on hierarchical subject names, see the TIBCO SmartSockets User’s Guide.
When subscribing or publishing to a subject, you can use wildcards in the specification of the subject name to match multiple subjects. Using wildcards in subjects is much like using wildcards for file names on an operating system command line. The asterisk (*
) wildcard operates much the same as it does on Windows, UNIX, or OpenVMS. It can be used for an entire subject name component, or as part of a more complicated wildcard containing other characters, such as foo*bar
. A wildcard component using an asterisk never matches across components, for example, "foo*bar"
does not match "foo/bar"
.
The ellipsis (...) wildcard operates much as it does on OpenVMS, where it matches any number of levels, including zero levels, of components. It must be used as an entire component, for example, auto
...
is not a wildcard. Multiple wildcards can be combined in a single subject name, for example, /a*b*/
.../d
. For more details on using wildcards with subjects see the TIBCO SmartSockets User’s Guide.
This section illustrates how a message originating from a single RTclient is published to multiple RTclients subscribed to the specified subject. In Figure 3, processes are represented by circles, and connections between processes are represented by dark lines. As you can see, there is a single sending process (Send), and two receivers (Receive1 and Receive2). Each of these RTclients is connected to the same RTserver. The Receive1 and Receive2 programs have both subscribed to the /sub1
subject. If the Send process wants to publish a message to the /sub1
subject, this sequence of events occurs:
Figure 3 shows the message flow through RTserver. Note that if the Send program was also subscribed to the /sub1
subject, it too would receive a copy of the message from RTserver.
Let’s go back to the sending and receiving examples earlier in this lesson. Line 10 of the sending program calls setDest
, as shown.
The setDest
method specifies the subject the message is being published to. For the receiving program to get the message, line 11 of the receiving program is required:
This line allows the receiving program to start receiving any messages published to the /ss/tutorial/lesson2
subject. Note the second parameter is set to true
. If this had been set to false
, it would indicate that the receiving program is not going to receive messages published to the /ss/tutorial/lesson2
subject. Setting the second parameter to false
unsubscribes the process from the specified subject.
Start the receiving program again
To illustrate this concept more clearly, you need three separate windows open. In the first window, make sure RTserver is running, then start up the receiving program:
Edit the receiving program
Now edit the receiving program, changing line 11 so the receiving program subscribes to the /smartsockets/foo
subject. Line 11 should now look like:
Save the modified receiving program and compile
Save your changes to receive.java
. Compile the program again:
Start the new receiving program
In the second window, start up the new receiving program:
Start the sending program
Finally, in the third window, start up the sending program:
You should see that your original receiving program got the message and printed it.
Notice that the new receiving program (subscribed to the /smartsockets/foo
subject) did not receive the message. In fact, it is still blocked, waiting for a message. Stop the blocked program’s execution with Ctrl-c and return to the operating system prompt.
Edit the receiving program again to subscribe to another subject
To see how one message can be delivered to two processes in a single operation, go back and edit the receiving program to once again receive the /ss/tutorial/lesson2
subject. Line 11 should now look like:
Compile the modified receiving program
Compile the program as before:
Start the new receiving program
Now start the new receiving program in the first window:
Start a second instance of the same receiving program
In the second window, start up a second instance of the same receiving program:
Start the sending program
Finally, in the third window, start up the sending program:
In a few moments, you should see that both windows where the receiving programs are running display:
If you wish, you can start any number of receiving programs, run the sending program, and observe as the message gets delivered to all of them with a single operation.
Note that you did not have to change a single line of code in the sending program to take advantage of this desirable feature. The ability to send a message to multiple processes with a single operation, without having to specify the location of the processes, is a key feature of SmartSockets. In addition to providing useful functionality, this feature makes the testing, debugging, and maintenance of your network application much easier. Through the use of subjects and SmartSockets publish-subscribe services, you also achieve location transparency. This implies that your programs can be easily relocated anywhere on your network without changing a single line of code.
TIBCO SmartSockets™ Java Library User’s Guide and Tutorial Software Release 6.8, July 2006 Copyright © TIBCO Software Inc. All rights reserved www.tibco.com |