Contents
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
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.
In StreamBase Studio, import this sample with the following steps:
-
From the top-level menu, select
> . -
Enter
custom
to narrow the list of options. -
Select Custom Java operator that shows the OperatorConfigurationAccessor from the Extending StreamBase category.
-
Click
.
StreamBase Studio creates a single project for the sample.
-
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
> from the context menu. -
Open the
src/main/eventflow/
folder.packageName
-
Open the
module.sbapp
file. -
Select the Java operator, open its Operator Properties tab, and observe that the
drop-down menu is set to select . -
Click the Run button. This opens the SB Test/Debug perspective and starts the module.
-
Click
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 forchoice-1
insbd.sbconf
-
The Decrement Map operator casts
offset
from string to int and subtracts it fromid
to populate the added field answer.
-
-
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
. -
Each time you click
, the id and answer fields increment but the offset field does not change. -
Press F9 or click the Terminate EventFlow Fragment button and return to the SB Authoring view.
-
In the Java operator's Operator Properties tab, set the
drop-down to . This changes the value retrieved fromsbd.sbconf
from 0 to 1. -
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.
-
Press F9 or click the Terminate EventFlow Fragment button when you are finished running the module.
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.