Developers: Using the StreamBase Java Toolkit to Write a Custom Java Operator

Home
Documentation
FAQs
Component Exchange
New in StreamBase 6.x
Training
Download Center
Contact DevZone

Printer Friendly

Library Articles

Using the StreamBase Java Toolkit to Write a Custom Java Operator

Author: Dr. John Lifter
Contributor: Bingfang Song
StreamBase Systems
10-May-2007

Applicable To: StreamBase 3.5, 3.7

 

Topics:

Introduction 

To use the Java operator to add custom functionality to a StreamBase application requires a JAR file containing the Java class that implements the custom functionality.

This class must extend the class com.streambase.sb.operator.Operator. In addition, this custom class must either implement the interface com.streambase.sb.operator.Parameterizable or delegate to another class that implements this interface. Optionally, the JAR file may also include a class that extends java.bean.SimpleBeanInfo, which is used by StreamBase Studio to populate the entries on the Properties view tabs.

This article was developed using the StreamBase Java Toolkit for Eclipse, v3.7.2, 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.

Starting an Eclipse StreamBase Project 

Follow these steps to create an Eclipse project in which to build a StreamBase Custom Java Operator.

  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.

Using the StreamBase Java Toolkit to Create a Custom Java Operator   

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

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

    The New, Select a Wizard window opens; expand the options under the StreamBase entry, highlight the StreamBase Java Operator selection and click the Next command button.

  2. In the New StreamBase Java Operator, StreamBase Java Operator window, confirm that the Source folder: text box references the project directory, enter a package name into the Package: text box and a name for the operator class into the Name: text box. Use the controls associated with the Number of input ports: and Number of output ports: labels to specify the number of input and oputput ports required by the operator. For this example, give the package the designation com.training, give the class the name MyOperator, and specify a single input and output port; then click the Next command button. (You can enter any package and class name you want.)

    New StreamBase Java Operator Window

  3. In the New StreamBase Java Operator, Java Operator window add the class variables that will become the configurable parameters of the custom operator. To add a variable, click the Add command button; the New Operator Property window opens.

    New StreamBase Java Operator Window

    From the Property: dropdown list box, select the data type of the variable (note that you must scroll down the listing to see the Enum entry). After selecting the variable type, give the variable a name, a display name, and a default value. If it is optional that developers set a value for this variable, check the Optional property check box

    If the operator class has more than one configurable parameter, click the Add command button and add additional variables to the class definition. In this exercise, add a variable of each type, as illustrated in the following figure.

    New StreamBase Java Operator, Java Operator Window

    When adding the multiple entries in a String[] or Enum type, enter one of the valid entries into the Default Value: text box and then click the Add command button. This moves the entry to the Entered Values: listing. Repeat until all desired values have been entered.

    Check the desired checkboxes under the Which method stubs would you like to create? label and then click the Finish command button to generatre the starting point code.

Understanding the Generated Code 

The toolkit will generate two Java classes: an operator class and a bean info class. You will not need to edit the content of the bean info class, but you do need to provide implementations for the typecheck, processTuple, init and/or shutdown methods, and you will need to edit the body of the constructor method if you have an Enum type as a configurable parameter.

The Instance Variables

Review the code in the operator subclass. Note how the names assigned to the instance variables (and their corresponding getter and setter methods) are derived from the field name entries made in the New StreamBase Java Operator, Java Operator window.

The Constructor Method

The code within this method has three responsibilities: it invokes the superclass' constructor; it sets the number of input and output ports; and it sets the default value for each of the configurable parameters. Note that a default value is not set for Enum configurable parameters and consequently a control corresponding to this configuration field will not be included on the operator's Properties view, Parameters tab. To complete the coding within the constructor method, add the following line to the generated code.

    public MyOperator() {
        super();
        setPortHints(1, 1);
        setAnInt(0);
        setADouble(0.0);
        setABool(false);
        setAString("hello");
        setAStringArray(new String[] { "one", "two", "three" });
        setAnEnum("UPPER");

    }

The Process Tuple Method

Within this method you write the processing logic for the operator. In this example, the operator has one input port and one output port, so the generated code that extracts the schema associated with the input port and then emits the tuple to the output port does not need to be edited, but you do need to add code that performs the processing associated with the operator.

In this example, the first field in the tuple received by the operator will be a string field. You will simply transfer the value from the operator's Enum instance variable into the corresponding string field in the emitted tuple.

Following the TODO comment, enter the following line of code and then save the file. In the StreamBase Java API documentation, review the other methods through which a tuple field value may be set.

    // TODO Defines the work that the custom Java operator will perform.
    tuple.setField(0,getAnEnum());
Writing a Manifest File 

The manifest file that is included in the JAR file with a custom operator must contain entries that identify the operator class as a StreamBase Java operator. Therefore, you must add a manifest file containing the desired text to this project. Both the content and syntax of the manifest file is very specific. Fortunately, the toolkit will assist you in this task.

  1. In the Package Explorer, highlight the file that represents the custom Java operator class, right-click, and select StreamBase > Generate StreamBase Manifest... from the popup menu.
  2. In the Generate StreamBase Manifest window, the entry corresponding to your operator class should already be identified. Select the Workspace radio button within the Destination grouping, click the Browse command button, and in the Save As window, pick a location into which the file should be written and give the file a name.

    Save As Window

    Then click the OK command button twice and a manifest file, with the required content, is generated.

Creating a JAR File 

Before you can use your custom operator in a StreamBase application, you need to prepare a JAR file that contains the .class files and the manifest.

  1. In the Package Explorer, highlight the icon that represents your project and select File > Export... menu item. Alternatively, you can right-click and select Export... from the popup menu. In the Export window, select the JAR file entry under the Java category and click the Next command button.
  2. In the JAR Export, JAR File Specification window, check the checkbox corresponding to the package(s) you want to include in the JAR file and in the JAR file: text box, provide the path and name of the JAR file that will be created by this process.

    JAR Export Window

  3. Then click the Next command button twice to move to the JAR Export, JAR Manifest Specification window.

    Select the Use existing manifest from workspace radio button and then enter (or browse to) the path and name of the manifest file written by the toolkit.

    JAR Export, JAR Manifest Specification Window

    Complete the process by clicking the Finish command button. The JAR file will be given the specified name and written to the location provided in the JAR Export, JAR File Specification window.

Adding the Java Operator to a StreamBase Application 

Follow these steps to incorporate the custom Java operator into a StreamBase application.

  1. Start StreamBase Studio and create a new project that includes an Application Diagram.
  2. Import the JAR file (see Creating a JAR File).
  3. Next drag the Java Operator icon from the palette onto the canvas. The Select Operator Class window opens immediately and your operator class, com.training.MyOperator, is listed in the drop down list.
  4. Select the operator class and click the OK command button. The canvas now includes a Java Operator icon. Highlight the Java Operator icon and in the Properties view select the Parameters tab. Note how each configurable instance variable can be set from a control within the Parameters tab.
  5. Complete the application by adding an input and output stream. In the Properties view, Edit Schema tab of the input stream, define any schema you want, just be certain that the first field is of type string and at least 5 characters in size.

    Custom Java Operator StreamBase Application

Running the StreamBase Application 

From within StreamBase Studio, run the StreamBase application and note that whatever value is set for the Enum configurable parameter is contained in the first field in the tuple emitted from the output stream.

Related Topics 

Back to Top ^