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 7 and Windows Vista:
  C:\Users\username\Documents\StreamBase Studio n.m Workspace\
      sample_custom-java-function

This Sample's Files

The sample has 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 ^