Sample code


Running the sample program

The distribution comes with a sample implementation in file test_awt.java

Before the first usage of that sample, it is necessary to make sure ottobus-1.0-bin.jar is in your CLASSPATH:

where <location-of-ottobus-jar> is the directory containing ottobus-1.0-bin.jar

The test application can then be launched like this:

The above example will startup with a 640 by 480 application window (no window dimensions were specified) and it will listen for all OttoBus messages for subject "fred":

If an initial window with different dimensions is required (for example 800 by 600), just add the initial width and height after the subscription subject:

For a real test, you would start a second instance of the test program, preferably with a different subject (althoug that is not required) or the sample remote debugger (which allows to send messages to any subject as well)

Here is a sample started with subject "barney":

Both of these screenshots were taken from a session where one sent a message to the other.

It is not necessary to run publisher and subscriber on the same machine. Beware of routers, though: they may or may not allow multicast packets to pass through.

Usage of the sample program

The test application interface consists of two parts:

The input part asks for both a subject and a data part. Each of these has a data type and a data value. Available data types are:

Note that not all data types work for the subject part. Basically, a byte array containing a 7-bit ASCII string, a 7-bit ASCII and an UTF-8 string are somewhat interoperable (provided the actual values are 7-bit ASCII strings)

Don't expect an int to be compatible with a UTF-8 subject (even if their value is "123")

Discussion of the sample code

Here are some of the highligts of that sample program:

These imports are required for OttoBus applications.

An OttoBus subscriber is required to implement interface IsOttoBusSubscriber

The OttoBusDispatcher.subscribe() statement above subscribes the current object to the subject stored in variable _receive_subject and specifies the string "blabla" as the user definable parameter that will be used in calls to the callback method consume()

After this call, all incoming messages for the specified subject will result in a call to the callback method where they can be processed.

The try{}catch() is required to trap exceptions that can be thrown by the subscribe() method.

The optional user argument to subscribe() is not used for anything special in the sample program. However, it would be perfectly possible to setup one central subscriber for all subjects. In such cases it would make sense to specify additional data to that subscriber for proper handling of received the messages.

The call to OttoBusLogger.connect() activates the remote debugging features of the OttoBus logger facility. This call is not required in a a real life application.

The above method creates a temporary OttoBus publisher for the subject entered in the "Subject:" textfield, publishes the data entered in the "Value:" textfield and terminates by releasing the temporary publish object.

Convenience method grabField() combines the value from the specified textfield and its corresponding data type into one OttoBusField object.

Once both subject and data payload are available in OttoBusField format, publishing the message is straightforward: a new OttoBusPublisher object is created for the subject and a message is published on it (paper.publish(fldval))

The code above implements a minimalistic version of the consume() callback method. Note that the user argument is assumed bo te a String in this example. This user argument could be used to distinguish between different subjects if the object is subscribed to more than one subject (although a call to p.getSubject() would return the subject of the received message)

The sample code dumps the formatted message on standard error and appends it to the log in the bottom half of the application window.

Note that as of OttoBus version 1.0, no port information is available anymore for OttoBusMessage objects.

Also note that OttoBusField.toString() is implemented to show a bit more information than the default Object.toString()


OttoBus home

Last Update: 16 September 2001