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.6 runtime environment is included with 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, UNIX, or Solaris and jvm.dll on Windows.
For example, to set the variable on a Linux, UNIX, 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: You cannot pass or return null values to external Java functions.
  2. Compile the function to a shared library:
    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' (
    	double distance(double arg1, double arg2, double arg3);
    );
    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, argument datatypes, and return datatypes as the function in the .class file.

  4. Call the function in the project using DistanceLib.distance(arg1, arg2, arg3).
  5. Link the Java library to the Event Stream Processor Server.

    The Event Stream Processor has a built-in Java runtime environment. To link the Java function to your application, start the server with the -j option.

    For .class files, specify only the directory of the file:
    sp -j /home/sybase/user/java/lib
    If the class is inside a .jar file located in, for example, /home/sybase/user/java, then specify the directory of the file including the file name:
    sp -j /home/sybase/user/java/Distance.jar

    Separate multiple paths using ":" in Linux/UNIX and ";" in Windows.