Setting up Java synchronization logic

When you install SQL Anywhere, the installer automatically sets the location of the MobiLink server API for Java classes. When you start the MobiLink server, it automatically includes these classes in your classpath. The MobiLink server API for Java classes are located in install-dir\Java\mlscript.jar.

 To implement synchronization scripts in Java
  1. Create your own class or classes. Write a method for each required synchronization script. These methods must be public. The class must be public in the package.

    See Methods.

    Each class with non-static methods should have a public constructor. The MobiLink server automatically instantiates each class the first time a method in that class is called.

    See Constructors.

  2. When compiling the class, you must include the JAR file java\mlscript.jar.

    For example,

    javac MyClass.java -classpath "C:\Program Files\SQL Anywhere 12\java\mlscript.jar"
  3. In the MobiLink system tables on your consolidated database, specify the name of the package, class, and method to call for each synchronization script. One class is permitted per script version.

    For example, you can add this information to the MobiLink system tables using the ml_add_java_connection_script stored procedure or the ml_add_java_table_script stored procedure.

    For example, the following SQL statement, when run in a SQL Anywhere database, specifies that for the script version ver1, myPackage.myClass.myMethod should be run whenever the authenticate_user connection-level event occurs. The method that is specified must be the fully qualified name of a public Java method, and the name is case sensitive.

    call ml_add_java_connection_script('ver1',
    'authenticate_user', 'myPackage.myClass.myMethod')

    For more information about adding scripts, see:

  4. Instruct the MobiLink server to load classes. A vital part of setting up Java synchronization logic is to tell the Java VM where to look for Java classes. There are two ways to do this:

    • Use the mlsrv12 -sl java -cp option to specify a set of directories or jar files in which to search for classes. For example, run the following command:

      mlsrv12 -c "dsn=consolidated1" -sl java (-cp %classpath%;c:\local\Java\myclasses.jar)

      The MobiLink server automatically appends the location of the MobiLink server API for Java classes (java\mlscript.jar) to the set of directories or jar files. The -sl java option also forces the Java VM to load on server startup.

      For more information about the available Java options, see -sl java mlsrv12 option.

    • Explicitly set the classpath. To set the classpath for user-defined classes, use a statement such as the following:

      SET classpath=%classpath%;c:\local\Java\myclasses.jar

      If your system classpath includes your Java synchronization logic classes, you do not need to make changes to your MobiLink server command line.

      You can use the -sl java option to force the Java VM to load at server startup. Otherwise, the Java VM is started when the first Java method is executed.

      For more information about the available Java options, see -sl java mlsrv12 option.

  5. On Unix, if you want to load a specific JRE, you should set the LD_LIBRARY_PATH (LIBPATH on IBM AIX, SHLIB_PATH on HP-UX) to include the directory containing the JRE. The directory must be listed before any of the SQL Anywhere installation directories.

 See also