Example: Using Java Functions

Write a Java function that computes distances. After compiling the function as a .class or .jar file, declare it using the CREATE LIBRARY statement, and call the function as needed in your CCL project. Finally, link the library with the Event Stream Processor.

Note: The Java 1.7 runtime environment is included with SAP Sybase Event Stream Processor. If your function requires a different version of Java, set the environment variable ESP_JAVA_HOME to the location of the appropriate Java virtual machine shared library. This is usually libjvm.so on Linux and Solaris, and jvm.dll on Windows.
For example, to set the variable on a Linux or Solaris machine in the shell, use:
export ESP_JAVA_HOME=/user/bin/java/jre/lib/libjvm.so
  1. Write the function.

    Define all functions as a public static method inside the class. For example, this function computes distances:

    public class Distance {
    	public static double distance(double arg1, double arg2, 
    	double arg3) {
    		double sum = 0;
    		sum += arg1 * arg1;
    		sum += arg2 * arg2;
    		sum += arg3 * arg3;
    
    		return Math.sqrt(sum);
    	}
    }
    Note: The return and argument datatypes for the function must be written as int, long, double, or String.

    You cannot pass or return null values to external Java functions.

  2. Compile the function to a class file:
    javac -d /home/sybase/user/java/lib Distance.java
    You can also create Java archives (.jar files) of classes and refer to those when declaring the functions in the CCL project.
  3. Declare the function and library in the CCL project using the CREATE LIBRARY statement.
    CREATE LIBRARY DistanceLib LANGUAGE JAVA FROM 'Distance' (
    	float distance(float, float, float);
    );
    Note: 'Distance' is the name of the class. If the class is defined in a package, replace the class name with its directory, including the name.

    Ensure the function signature in the library has the same name as the function in the .class file.

    Use ESP datatypes to declare the function: int, long, double, and String in Java map respectively to integer, long, float, and string in ESP.

  4. Call the function in the project using DistanceLib.distance(float, float, float).
  5. Link the Java library to the Event Stream Processor Server.
    If running within Studio, specify the location of the class files by setting your CLASSPATH variable. If running outside of Studio, set the Java-classpath option in the project configuration file.