Contents
This sample demonstrates the use of output filtering to allow multiple StreamBase client applications to connect to an instance of StreamBase Server, where each client receives only the tuples that match a keyword associated with that client.
The client applications in this sample connect to an instance of StreamBase Server, then send their connection ID and a keyword. The filter expression in the output stream only allows tuples with the keyword associated with the connection ID to be sent to the clients.
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.
In StreamBase Studio, import this sample with the following steps:
-
From the top-level menu, select
> . -
Enter
filtering
to narrow the list of options. -
Select Filtering tuples by client connection ID from the Applications category.
-
Click
.
StreamBase Studio creates a single project containing the sample files.
The Java version of this sample is intended to be run in StreamBase Studio, and has
no provision for running at the command line. Experienced Java developers can do so
by adding the /target/classes/
directory to the
classpath.
When you load this sample in Studio, the sample places two launchers for the Java dequeuer programs into Studio's Run History list. The overall procedure is:
-
Run the first StreamBase application,
outputfilter.sbapp
, in Studio. The StreamBase application accepts input from the Manual Input and shows output in the Output Streams view as normal. -
Use the launcher in the Run History list to run the
OFilterClient Red
and JavaOFilterClient Blue
client programs at the same time the StreamBase application is running. -
Send tuples to the application via the Manual Input view.
-
Watch the results in the Console views for the two client programs.
Run the Java client launchers from the Run History list using one of the following methods:
-
From the Studio top-level menu, invoke
> , and select the specified launcher from the list. -
Open the Run Configurations dialog, select the specified launcher from the Java application category, then click
. -
Click the down-arrow next to the Run button in the Studio toolbar, and select the specified launcher from the list.
The Java client launchers placed in the Run History list presume that Studio is running the StreamBase application on port 10000, which is the installed default port for StreamBase application launches. Before you proceed, use these steps to confirm that port 10000 is the default launch port:
-
Open
> . -
In the left side column, make sure outputfilter is selected under EventFlow Fragment.
-
Confirm that the StreamBase engine Configuration or Default Port setting is 10000. If not, change it to 10000.
-
Click
.
Follow these steps to run this sample in StreamBase Studio:
-
In the Project Explorer view, open this sample's folder.
Keep an eye on the bottom right status bar of the Studio window. Make sure any
Updating
,Downloading
,Building
, orRebuild project
messages finish before you proceed. -
Open the
src/main/eventflow/
folder.packageName
-
Double-click to open the
outputfilter.sbapp
module. Make sure the module is the currently active tab in the EventFlow Editor. -
Click the Run button. This opens the SB Test/Debug perspective and starts the module.
-
Wait for the Waiting for fragment to initialize message to clear.
-
Open the Run History list as described in Introduction and Setup, and select the
launcher. This runs theOFilterClient Blue
ClientAccess.java
program, which connects tooutputfilter.sbapp
. -
Open the Run History list again, and run the
OFilterClient Red
launcher. -
In the Manual Input view, select the
in
stream and enter val=1 and keyword=yellow.Notice that there are no changes in the Console view.
-
In the Manual Input view, enter val=2 and keyword=red.
The Console view switches to the console for
OFilterClient Red
and shows output similar to:Read tuple: 2,5FC5E0D15F3D55266007922477871768
The long hexadecimal value is different for each run of the program.
-
In the Manual Input view, enter val=3 and keyword=purple.
Notice that there are no changes in the Console view.
-
In the Manual Input view, enter val=4 and keyword=blue.
The Console view switches to the console for
OFilterClient Blue
and shows output similar to:Read tuple: 4,0D2401453C0A0D02A0A7C51B15054F40
-
Continue entering tuples to confirm that keyword=red goes to one Console window, keyword=blue to another, and any other keyword has no Console output.
-
When done, press F9 or click the Terminate EventFlow Fragment button. This closes both the StreamBase application and the two dequeuer programs.
Repeat steps 2 through 10 for each of the two other StreamBase applications. These applications achieve the same ends, but use a Java class to implement the selection logic.
The output filtering sample consists of:
-
Three StreamBase applications that illustrate three approaches that achieve the same results:
- outputfilter.sbapp
-
This version of the application uses only Spotfire Streaming operators and functions. For a large number of clients, this approach is somewhat less efficient than the versions of the application that use custom Java functions.
- outputfilter-calljava.sbapp
-
This version of the application uses custom Java functions to maintain the association between connection IDs and keywords.
- outputfilter-jfun.sbapp
-
This version of the application also uses custom Java functions to maintain the association between connection IDs and keywords. In this case, the functions are named in the configuration file so they can be called directly, rather than with the
calljava
function.
-
The Java source file
OFilterClient.java
has the dequeuing client andClientAccess.java
has the filter function used. These files are placed in thesrc/main/java/
folder, and are built automatically by StreamBase Studio.packagename
-
A server configuration file,
engine.conf
, that maps the custom Java functions.
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_outputfiltering
See Default Installation
Directories for the default location of studio-workspace
on your system.