Configuration File Access Sample

About This Sample

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.

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.

Running sample_javaoperator-confaccess.sbapp in StreamBase Studio

  1. In the Project Explorer, open the sample you just loaded.

  2. Open the src/main/eventflow folder.

  3. Open the package folder (most samples contain a single package folder. Open the top-level package folder if your sample contains more than one folder).

  4. Open the named application file.

    If you see red marks, wait a moment for the project in Studio to load its features.

    If red marks do not resolve themselves in a moment, select the project and right-click MavenUpdate Project from the context menu.

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

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

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

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

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

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

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

  12. Repeat steps 6 to 8 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.

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

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 permission problems. The default workspace location for this sample is:

studio-workspace/sample_javaoperator-confaccess

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