Configuration File Access Sample

Sample Overview

This sample demonstrates how a custom Java operator or embedded adapter can access alternative values for properties from a StreamBase configuration file, typically sbd.sbconf, This capability lets instances of such components within an application access the same set of property values and override a default value. For example, each adapter or Java operator instance can select from several pre-configured data sources or connection strings.

The sample application has a Java operator containing a choice menu in its Operator Properties tab. The menu chooses one of two strings. The strings are selectors for <choice> blocks in the provided sbd.sbconf file. The operator's typecheck method determines if the selector is valid and, if it is, retrieves the indicated property from the configuration file.

When the sample application runs, the custom operator replaces the content of the first schema field, offset, of type String, with text the Choose One menu setting causes it to retrieve from the configuration file. The string will either be "0" or "1". The downstream Map operator casts offset to integer and subtracts it from the id value that the Sequence operator inserted in the schema in output field answer.

See the source sample's code for ConfigBasedOperatorSample.java and ConfigBasedOperatorSampleBeanInfo.java and the sbd.sbconf configuration file to learn more about the implementation. In the source file, the string configElement holds the property value retrieved from an XML node (<text> whose ID matches the choice string the user has selected.

Note

The Java code in this sample is delivered only in source code form. To build the code, you must load the sample into StreamBase Studio, which automatically builds the Java code.

Running This Sample in StreamBase Studio

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

  2. Select the Java operator, open its Operator Properties tab, and observe that the Choose One drop-down menu is set to select choice-1.

  3. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

  4. Click Send Data and observe:

    • The sequence operator adds int id to the tuple and sets it to 1

    • The GetOffset Java operator retrieves the value for offset specified for choice-1 in sbd.sbconf

    • The Decrement Map operator casts offset from string to int and subtracts it from id to populate the added field answer.

  5. The Application Output view displays the content of the output stream, {string offset, int id, int answer} with content offset=0, id=1, answer=1.

  6. Each time you click Send Data, the id and answer fields increment but the offset field does not change.

  7. Press F9 or click the Stop Running Application button and return to the SB Authoring view.

  8. In the Java operator's Operator Properties tab, set the Choose One drop-down to choice-2. This changes the value retrieved from sbd.sbconf from 0 to 1.

  9. Repeat steps 3 - 6 and observe the Application Output view again. It displays the content of the output stream, {string offset, int id, int answer}, with content offset=1, id=1, answer=0.

  10. Press F9 or click the Stop Running Application button when you are finished running the application.

For details on retrieving properties from configuration files, see <adapter-configurations> documentation.

Importing This Sample into StreamBase Studio

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

  • From the top menu, select FileLoad StreamBase Sample.

  • Type javaop to narrow the list of options.

  • Select javaoperator-confaccess from the Extending StreamBase category.

  • Click OK.

StreamBase Studio creates a single project for the sample.

Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids the permission problems that can occur when trying to work with the initially installed location of the sample. The default workspace location for this sample is:

studio-workspace/sample_javaoperator-confaccess

See Default Installation Directories for the location of studio-workspace on your system.

In the default TIBCO StreamBase installation, this sample's files are initially installed in:

streambase-install-dir/sample/javaoperator-confaccess

See Default Installation Directories for the default location of studio-workspace on your system.