Developers: Obtaining Monitoring Data from a StreamBase Application

Home
Documentation
Library
Sample Code and Applications
FAQs
Articles
Community
Training
Download Center
Contact DevZone

Printer Friendly

Library Articles

Obtaining Monitoring Data from a StreamBase Application

Author: Dr. John Lifter
StreamBase Systems
5-April-2007

Topics:

Introduction 

For a running StreamBase application, you can view performance statistics in the StreamBase Studio Monitor View, or in a terminal window by using the sbmonitor command to launch the text-based performance monitor utility or the jsbmonitor command to launch a GUI-based performance monitor. An alternative to viewing the statistics in StreamBase Studio or the command output is to write your own StreamBase Monitor listener client in Java, by extending the classes in the package com.streambase.sb.monitor. Compared to the prewritten monitoring utilities, writing your own monitoring application gives you a much higher level of control over the presented information.

The Monitor API contains only five classes and an interface. You write an application that uses an instance of the StreamBaseMonitor class to connect to an sbd instance. The StreamBaseMonitor class is responsible for retrieving performance data from the daemon process.

Your application must also include a class that implements the MonitorListener interface. This interface has only one method, snapshotReceived, in which you can review the performance data and/or carry out some processing logic in response to your analysis of the data. For example, as part of this method's implementation you could send the monitoring data to some other monitoring frameworks for display.

This article was developed using the StreamBase Java Toolkit for Eclipse, v3.7, which supports development of enqueuer and dequeuer clients, custom Java operators, Java functions, and embedded adapters that are compatible with StreamBase 3.5.x or 3.7.x

Installation of the StreamBase Java Toolkit for Eclipse is detailed in Getting Started with the StreamBase Java Toolkit for Eclipse.

StreamBase Monitor API 

MonitorListener Interface

Your implementation of the MonitorListener interface receives instances of the Snapshot class from the StreamBaseMonitor class. You must provide an implementation of the snapshotReceived method.

Snapshot Class

The Snapshot object stores a time dependent view (snapshot) of performance data from a StreamBase application. Snapshot objects are created by the StreamBaseMonitor class from information received from the monitored sbd, and are consumed by implementations of the MonitorListener interface. The sbd produces performance data at regular intervals, depending on the period specified in the daemon's sbd.sbconf file. The Snapshot object contains the current performance data as well as incremental changes from the previous snapshot.

The Snapshot class includes methods that your code may use to access the performance data specific to each operator or application thread. Additionally, the method prettyPrint may be used to display all of the data encapsulated by a Snapshot instance. If you want to stop monitoring the performance of the sbd, your code may invoke the terminate method, which notifies the StreamBaseMonitor instance to stop monitoring as soon as processing of this Snapshot instance has completed.

The Snapshot object is only valid within the scope of the snapshotReceived method to which it was passed.

StreamBaseMonitor Class

This class monitors a StreamBase daemon and gathers statistics. It periodically sends Snapshot objects to all registered MonitorListeners. The constructor takes the URL for connecting to sbd. To start processing, your client calls the run method. The proxy will then begin reading snapshots until either the connection is terminated, the client is interrupted or the terminate method is called (in either another thread of the client application or a listener registered with this StreamBaseMonitor instance).

After the StreamBaseMonitor instance retrieves performance data from the sbd, it creates an instance of the Snapshot class and passes it to the snapshotReceived method of each MonitorListener implementation registered with the StreamBaseMonitor instance.

OperatorInfo Class

Instances of this class represent information about a single operator in a StreamBase application. Instances of this class should be retrieved from the Snapshot instance passed to the MonitorListener's snapshotReceived method.

The OperatorInfo object implements the java.lang.Cloneable interface and provide a public implementation of the java.lang.Object clone method. You can, therefore, retain operator information by cloning these objects and then passing them to another thread for further processing.

ThreadInfo Class

Represents information about a single thread in a StreamBase Server. Instances of this class should be retrieved from the Snapshot instance passed to the MonitorListener's snapshotReceived method.

The ThreadInfo object implements the java.lang.Cloneable interface and provide a public implementation of the java.lang.Object clone method. You can, therefore, retain thread information by cloning these objects and then passing them to another thread for further processing.

SystemInfo Class

Represents system-level information. Instances of this class should be retrieved from the Snapshot instance passed to the MonitorListener's snapshotReceived method.

The SystemInfo object is not cloneable so if you need to retain the data encapsulated within this object, you will need to copy it into another representation.

Starting an Eclipse StreamBase Project 

Follow these steps to create an Eclipse project in which to build a monitoring client.

  1. In Eclipse, create a new Java project by selecting the File > New > Project... menu item, or right-click in the Package Explorer view and select New > Project... from the popup menu.

    In the New Project, Select a Wizard window, select Java Project and click the Next command button.

    In the New Project, Create a Java Project window, enter a Project name and click the Next command button.

  2. Use the New Java Project, Java Settings window to add the StreamBase client libraries to the project.

    Select the Libraries tab and click the Add Library... command button, which opens the Add Library window.

    Highlight the StreamBase Client API entry and click the Next command button.

    In the Add Library, StreamBase Client API Library window, select the version of StreamBase against which this dequeuer client will run and click the Finish command button.

    Add StreamBase Client Library

    Complete the process by clicking the Finish command button.

    New Java Project, Java Settings Window

    If you already have a Java project to which you want to add StreamBase Java client coding, you can add the StreamBase Client Library to the project by right-clicking on the project icon and selecting Build Path > Add Libraries... from the popup menu, which opens the Add Library window. Follow the preceding instructions to add the StreamBase Client Library to your existing project.

    Alternatively, you could right-click on the project icon and select Properties from the popup menu. In the Properties window, select the Java Build Path entry and then the Libraries tab. Click the Add Library... command button and continue as described previously.

Creating a Monitoring Client Application 

Follow these steps to generate starting point code using the StreamBase Java Toolkit.

  1. Highlight the icon corresponding to your new Java project, right-click and select New > Class from the popup menu. Alternatively, highlight the project and select the File > New > Class menu entry.

    The New Java Class, Java Class window opens. Enter package and class names and direct the wizard to create a main method. Click the Finish command button to generate the starting point code.

    New Java Class, Java Class Window

Adding Code to the Generated Framework 

The generated code contains only the class declaration and the main method.

package com.training;

public class SBMonitor {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
}

First add statements to import the StreamBase client API packages.

package com.training;

import com.streambase.sb.*;
import com.streambase.sb.monitor.*;

public class SBMonitor {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
}

Next, write an inline class that implements the MonitorListener interface. Within the body of the snapshotReceived method, add code that extracts information from the Snapshot instance passed to the method. You will need to review the Snapshot class API to learn about the available methods.

    public static void main(String[] args) {

        class MyMonListener implements MonitorListener {
            public void snapshotReceived(Snapshot snap){
                // add code here to extract information
                // from the Snapshot instance
            } // end of snapshotReceived

        } // end of MyMonListener class
} // end of main

Finally, complete the main method by adding code that instantiates an instance of your MonitorListener class and then registers this object with an instance of the StreamBaseMonitor class. Invoke the run method. When this client application runs, it will obtain monitoring information from the StreamBase server located at the URL used to initialize the StreamBaseMonitor instance. Note that in this example, the URL is hard-coded; a more likely scenario is to pass the URL as a command line argument when running this application.

    public static void main(String[] args) {

        class MyMonListener implements MonitorListener {
            public void snapshotReceived(Snapshot monSnap){
                // add code here to extract information
                // from the Snapshot instance
            } // end of snapshotReceived
			
        } // end of MyMonListener class

        MyMonListener listener = new MyMonListener();

        try {
            final String sburl = "sb://localhost:10000";
            StreamBaseMonitor sbmon = new StreamBaseMonitor(sburl);

            sbmon.addMonitorListener(listener);
            sbmon.run();
        } catch (StreamBaseException e){
            System.out.println("Exception in StreamBaseMonitor run(): " + e);
        }
    } // end of main
Related Topics

Back to Top ^