Custom Java Simple Function Sample

The math.sbapp sample application demonstrates a custom Java simple function. The application takes two doubles as input representing the lenghts of the right angle sides of a triangle and outputs the hypotenuse of these inputs using several different methods.

The application generates the output using these three expressions in a Map operator:

  • hypotenuse(x, y)

  • hypot(x, y)

  • calljava('java.lang.Math', 'hypot', x, y)

The first expression uses a function alias defined in the sbd.sbconf file that will use the Hypotenuse class defined in the Hypotenuse.java sample file. The second expression uses a function alias that will use the java.lang.math method. The final expression uses calljava function to call the java.lang.math method directly.

This topic describes how to load and run the math.sbapp sample. For more information about custom Java functions, see the topic, Using the StreamBase Java Function Wizard, in the API Guide.

Installed Sample File Locations

By default, the sample files are installed in:

  • On Windows: C:\Program Files\StreamBase Systems\StreamBase.n.m\sample\custom-java-function

  • On UNIX: /opt/streambase/sample/custom-java-function

The sample has of the following files:

  • The source code for the function, Hypotenuse.java

  • A sample configuration file, sbd.sbconf, which tells the StreamBase Server to load the custom function and defines function aliases used.

  • A sample application, math.sbapp, which shows different ways to invoke the function.

  • An ant build.xml file that can be used to create the function's JAR file.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, click FileLoad StreamBase Sample.

  • Select the custom-java-function sample from the Extending StreamBase list.

  • Click OK.

StreamBase Studio creates a project for each sample.

How We Created the Custom Java Simple Function Sample

  1. Created the custom Java application:

    1. Edited and saved Hypotenuse.java in a text editor.

    2. Built Hyponetuse.java using our favorite Java development tools, to generate Hypotenuse.jar:

      Note

      The installation includes a build file, build.xml, which you can use to build Hypotenuse.java if ant is installed on your system. Additionally, on windows a solution file is provided.

  2. Launched StreamBase Studio

  3. Created the custom-java-function project. Selected the option to Create template server configuration file.

  4. From the top menu, in the Authoring perspective, selected FileNewEventFlow Application. Selected the custom-java-function project, and entered math.sbapp for the diagram name.

  5. Created an input stream:

    1. Dragged an input stream from the palette to the canvas.

    2. Clicked it on the canvas, which invoked the Input Stream Properties view.

    3. On the Edit Schema tab, added:

      Field Name: x, Type: double, Size: 8

      Field Name: y, Type: double, Size: 8

  6. Set up the Map operator:

    1. Dragged a Map operator from the palette to the canvas and opened its Properties view.

    2. Dragged a connector from the Input Stream to the input of the Map1 operator.

    3. On the Output Settings tab, clicked on the Add button to add a new row. Edited the row fields as follows:

      • Action: Add

      • Field Name : customHypot

      • Expression: hypotenuse(x, y)

    4. Clicked the Add button to add another row:

      • Action: Add

      • Field Name : directMathHypot

      • Expression: calljava('java.lang.Math', 'hypot', x, y)

    5. Clicked the Add button to add another row:

      • Action: Add

      • Field Name : aliasedMathHypot

      • Expression: hypot(x, y)

  7. Created an output stream:

    1. Dragged an output stream from the palette to the canvas.

    2. Connected the Map1 operator to the output stream.

  8. Double-clicked sbd.sbconf in the Resources folder to open it in the StreamBase editor. In the file, removed unnecessary template code and added a java-vm element referring to the Hypotenuse.jar custom Java program. Also add the two custom-function alias definitions.

Running the Custom Java Simple Function Application

Running math.sbapp in StreamBase Studio

  1. In the Package Explorer, double-click to open the math.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run StreamBase Application (Default Launch) button. (Not the Run button.) This opens the Test/Debug perspective and starts the application.

  3. When the server starts, StreamBase Studio switches to the Test/Debug perspective.

  4. On the Manual Input view, enter the following values in the x and y fields, then press Send Data:

    x: 2
    y: 2

  5. In the Application Output view, observe the tuple emitted when you press Send Data. You should observe the three calculated hypotenuse values are all identical and approximately 2.83.

Running math.sbapp in Terminal Windows

To run this sample on a machine where StreamBase is installed:

  1. Open three terminal windows on UNIX, or three Command Prompt windows on Windows. In each window, change directory to streambase-install-dir/sample/custom-java-function.

  2. In the first window, launch the StreamBase Server for the sample application:

    sbd -f sbd.sbconf math.sbapp

  3. In the second window, run a dequeuer so that you can see the output that will be produced:

    sbc dequeue

  4. In the third window:

    1. Run an enqueuer:

      sbc enqueue InputStream1

    2. Type the x and y values, separated by commas, into the enqueuer

  5. In the second (dequeuer) window, you should see output from the application.

  6. Type: Control-Z (Windows) or Control-D (UNIX). The sbc command will exit.

  7. In window 3, type: sbadmin shutdown. The sbadmin shutdown command will terminate the daemon and the dequeuer.

Back to Top ^