Developing Java™ native functions
You create Java™ native functions by adding Java™ annotations that describe Java™ public static methods as native functions to the SPL compiler.
You compile the Java™ source code and index the toolkit by running the spl-make-toolkit command or the sc command. Then, Teracloud® Streams creates the necessary toolkit artifacts that enable SPL code to call the Java™ method directly with the same syntax that is used to call an SPL function.
To define a Java™ public static method as a
native function, add the @Function
annotation to a public static method in the Java™ source code. If the function uses external libraries, you can
also add the @ToolkitLibraries
annotation to the containing class to specify the
locations of the libraries.
pow
:@Function (namespace=”spl.math.java”)
public static double pow(double x, double y) {
return Math.pow(x, y);
}
In the previous Java™ example,
the @Function
annotation has the optional namespace
attribute. When
the namespace
attribute is omitted, SPL gets the
values for that attribute from the value of the @Namespace
annotation
for the package of the annotated method, if it exists. Otherwise,
SPL gets the values for the namespace attribute from the package name
of the annotated method.
pow
function:use spl.math.java::*;
float64 res = pow(1.5, 3.1);
@Function
annotation has the optional name
and description
attributes.public class JavaFunctions {
@Function(name="getUser", description="Return value of USER environment variable")
public static String getUser() {
return System.getenv("USER");
}
When the Java™ compiler compiles
the Java™ source, the annotation
processing creates a nested class for the class that contains the
annotated method. The nested class is named class_name$StreamsModel
.
When the toolkit index is rebuilt, SPL artifacts are created under
the SPL_namespace/native.function directory.
- The function model file, javaFunction.xml, for the Java™ native functions in the namespace
- A C++ header file, which contains code to call the Java™ methods from SPL
For more information about creating a Java™ native
function, see the API documentation for the com.ibm.streams.function.model
package.