MobiLink Synchronization User's Guide
Writing Synchronization Scripts in .NET
Writing .NET synchronization logic
You can define start classes that are loaded automatically when the server is started. The purpose of this feature is to allow you to write .NET code that executes at the time the MobiLink server starts the CLR—before the first synchronization. This means you can create connections or cache data before a user synchronization request.
You do this with the MLStartClasses option of the dbmlsrv9 -sl dnet option. For example, the following is part of a dbmlsrv9 command line. It causes mycl1 and mycl2 to be loaded as start classes.
-sl dnet(-MLStartClasses=com.test.mycl1,com.test.mycl2)
Classes are loaded in the order in which they are listed. If the same class is listed more than once, more than one instance is created.
All start classes must be public and must have a public constructor that either accepts no arguments or accepts one argument of type MobiLink.Script.ServerContext.
The names of loaded start classes are output to the MobiLink log with the message "Loaded .NET start class: classname".
For more information about .NET CLR, see -sl dnet option.
To see the start classes that are constructed at server start time, see GetStartClassInstances method.
Following is a template start class. It starts a daemon thread that processes events and creates a database connection. (Not all start classes will need to create a thread but if a thread is spawned it should be a daemon thread.)
using System; using System.IO; using System.Threading; using iAnywhere.MobiLink.Script;
namespace TestScripts { public class MyStartClass { ServerContext _sc; bool _exit_loop; Thread _thread; OdbcConnection _conn;
public MyStartClass( ServerContext sc ) //===================================== { // perform setup first so that an exception will // cause MobiLink startup to fail _sc = sc; // create connection for use later _conn = _sc.makeConnection(); _exit_loop = false; _thread = new Thread( new ThreadStart( run ) ) ; _thread.IsBackground = true;
_thread.Start(); }
public void run() //=============== { ShutdownCallback callback = new ShutdownCallback( shutdownPerformed );
_sc.ShutdownListener += callback; // we can't throw any exceptions through run() try { handlerLoop(); _conn.close(); _conn = null; } catch( Exception e ) { // print some error output to the MobiLink log Console.Error.Write( e.ToString() ); // we will die so we don't need to be notified of // shutdown _sc.ShutdownListener -= callback; // ask server to shutdown so that this fatal error will // be fixed _sc.Shutdown(); } // shortly after return this thread will no longer // exist return; }
public void shutdownPerformed( ServerContext sc ) //=============================================== // stop our event handler loop { try { _exit_loop = true; // wait max 10 seconds for thread to die _thread.Join( 10*1000 ); } catch( Exception e ) { // print some error output to the MobiLink log Console.Error.Write( e.ToString() ); } }
private void handlerLoop() //======================== { while( !_exit_loop ) { // handle events in this loop Thread.Sleep( 1*1000 ); } } } }