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 adapter-configurations.xml in src/main/resources, This capability lets instances of such components in 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 preconfigured 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 adapter-configurations.xml file. The operator's typecheck method determines whether the selector is valid and, if so, retrieves the indicated property from the configuration file.

When the sample module 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 int and subtracts it from the id value that the Sequence operator inserts in the schema in output field answer.

See the source sample's code for ConfigBasedOperatorSample.java and ConfigBasedOperatorSampleBeanInfo.java and the adapter-configurations.xml 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 selects.

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, or use javac on the command line.

Importing This Sample into StreamBase Studio

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

  • From the top-level menu, select File>Load StreamBase Sample.

  • Enter javaop to narrow the list of options.

  • Select Custom Java operator that shows the OperatorConfigurationAccessor from the Extending StreamBase category.

  • Click OK.

StreamBase Studio creates a single project for the sample.

Running This Sample in StreamBase Studio

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

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

    If the red marks do not resolve themselves after a minute, select the project, right-click, and select Maven>Update Project from the context menu.

  2. Open the src/main/eventflow/packageName folder.

  3. Open the module.sbapp file.

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

  5. Click the Run button. This opens the SB Test/Debug perspective and starts the module.

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

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

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

  9. Press F9 or click the Terminate EventFlow Fragment button and return to the SB Authoring view.

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

  11. Repeat steps 5 through 8 and observe the Output Streams view again. It displays the content of the output stream, {string offset, int id, int answer}, with content offset=1, id=1, answer=0.

  12. Press F9 or click the Terminate EventFlow Fragment button when you are finished running the module.

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.