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.

Sample Location

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

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace. StreamBase Systems recommends that you use the workspace copy of the sample, especially on UNIX, where you may not have write access to /opt/streambase. In the default installation, the path to this sample in your Studio workspace is:

UNIX:       
  ~/streambase-studio-n.m-workspace/sample_custom-java-function
Windows XP:
  C:\Documents and Settings\username\My Documents\StreamBase Studio n.m Workspace\
      sample_custom-java-function
Windows Vista:
  C:\Users\username\Documents\StreamBase Studio n.m Workspace\
      sample_custom-java-function

This Sample's Files

The sample has of the following files:

  • The source code for the function, Hypotenuse.java

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

  • 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.

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 button. This opens the SB Test/Debug perspective and starts the application.

  3. When the server starts, StreamBase Studio switches to the SB 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 see the three calculated hypotenuse values are all identical and are approximately 2.83.

  6. When done, press F9 or click the Stop Running Application button.

Running math.sbapp in Terminal Windows

This section describes how to run the sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.

  1. Open three terminal windows on UNIX, or three StreamBase Command Prompts on Windows. In each window, navigate to the directory where the sample is installed, or to your workspace copy of the sample, as described above.

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

    sbd -f sbd.sbconf math.sbapp

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

    sbc dequeue

  4. In window 3:

    1. Run an enqueuer:

      sbc enqueue InputStream1

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

  5. In window 2, look for output from the application.

  6. In window 2, type: Ctrl-Z (Windows) or Ctrl-D (UNIX) to close the sbc command.

  7. In window 3, type: sbadmin shutdown. This terminates the server and the dequeuer.

Back to Top ^

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 SB 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.

Back to Top ^