The sample Java MIDP synchronization applications

There are a number of sample Java applications available to help you write Java synchronization applications for DB2 Everyplace.For the MIDP synchronization provider, the samples are located in:

%DSYINSTDIR%/Clients/Midp/samples

The primary sample is the Visiting Nurse application under com/ibm/mobileservices/demo, VNurse.java and NursesAid.jar. Under the same samples directory are two files which make up a simple application. This application does not provide Record Store Management (RMS) code or a solid user interface. The files are:

Details on the ISyncWorker.java file

The sample program SyncWorker.java demonstrates how to code a Sync Client application for DB2 Everyplace MIDP synchronization provider.

The Java sample application performs the following steps:

  1. Import the DB2 Everyplace synchronization packages.
    import com.ibm.mobileservices.isync.*;
    import com.ibm.mobileservices.isync.event.*;
    import com.ibm.mobileservices.isync.midp.*;
     
    
  2. Implement the eventIssued method of the ISyncListener interface for event notification during synchronization.
  3. Get an instance MIDPISyncProvider
  4. Get an instance of synchronization service from the provider object
  5. Get an instance of the configuration store from the service object
  6. Get an instance of the synchronization driver from the configuration store object
  7. Register your application listener object that implements the ISyncListener interface for event notification from the synchronization driver object during synchronization
  8. Perform synchronization on all enabled subscription sets. Check the return code and exception for the status of the synchronization.
  9. Close and free all resources allocated by the synchronization provider.

The ISyncSample.java example

The following example contains comments that refer to the steps in the previous section.

// Example 1:  ISync Java - Simple API usage
//
 
	// Step 1:  import the Sync Client Java packages 
      //
      import com.ibm.mobileservices.isync.*;
      import com.ibm.mobileservices.isync.event.*;
      import com.ibm.mobileservices.isync.midp.*;
      
 
 
 
/**
	Supporting class which handles all of the synchronization tasks.
	Called by ISyncSample.
*/
 
public class SyncWorker extends Thread implements ISyncListener
{
	private ISyncSample midlet;
	private boolean mCancel;
	private ISyncProvider provider;
	private ISyncService service;
	private ISyncConfigStore config;
	private ISyncDriver syncer;
	private String eventString;
	
	public SyncWorker(ISyncSample midlet)
	{
		this.midlet = midlet;
		mCancel = false;
	}
 
   // Step 2:  implement the eventIssued() method in the ISyncListener interface  
   //          if you are interested in event notification (optional)
   // 
	
	public int eventIssued(ISyncEvent evt)
	{
		int evtType = evt.getEventType();
		int evtCode = evt.getEventCode();
		int evtProg = evt.getSyncProgress();
        	String ssName = evt.getSubscriptionSetName();
		Object listenerInfo = evt.getEventInfo();
 
		Exception e = null;
		ConflictReader cr = null;
 
		if (listenerInfo instanceof Exception)
			e = (Exception) listenerInfo;
		else if (listenerInfo instanceof ConflictReader)
			cr = (ConflictReader) listenerInfo;
 
		eventString += evtCode + ":";
 
		switch(evtType)
		{
			// display event status
		  case ISync.EVTTYPE_INFO: 
 
             switch (evtCode)
		    {
               case ISync.EVT_INF_SYNCING_SUBS: 
                   midlet.updateSyncStat1("Synchronizing " + ssName);
                   midlet.updateSyncStat2(" ");
                   break;
               case ISync.EVT_INF_SYNC_STARTED: 
                   midlet.updateSyncStat1("Synchronization started");	
                   midlet.updateSyncStat2(" ");
                   break;
               case ISync.EVT_INF_PREP_MSG:
                   midlet.updateSyncStat2("Preparing message...");
                   break;	
               case ISync.EVT_INF_SEND_MSG:
                   midlet.updateSyncStat2("Sending message...");
                   break;	
               case ISync.EVT_INF_WAIT_MSG: 
             	    midlet.updateSyncStat2("Awaiting server reply...");
             	    break;
               case ISync.EVT_INF_APPLY_MSG: 
                   midlet.updateSyncStat2("Applying server message...");	
                   break;
               case ISync.EVT_INF_SYNC_CANCELED: 
			    midlet.updateSyncStat1("Synchronization canceled");
			    midlet.updateSyncStat2(" ");
			    break;    
		      case ISync.EVT_INF_SYNC_SUCCEEDED: 
			    midlet.updateSyncStat1("Synchronization succeeded");
			    midlet.updateSyncStat2(" ");  
			    break;	
               case ISync.EVT_INF_SYNC_FAILED: 
			    midlet.updateSyncStat1("Synchronization failed");
			    midlet.updateSyncStat2(" ");
			    break;		
               default:
                   break;
            }
 
			return ISync.RTNCB_DONE;
 
			case ISync.EVTTYPE_ERROR:
				midlet.updateSyncStat2("Error: " + evtCode);
				return ISync.RTNCB_DONE;
 
			case ISync.EVTTYPE_RETRY:
				midlet.updateSyncStat2("Retry: " + evtCode);
				return ISync.RTNCB_REPLY_YES;
 
			case ISync.EVTTYPE_CONFLICT:
			if (evtCode == ISync.EVT_CFT_REJECT)
			{
				String tabName = evt.getSubscriptionName();
                     midlet.updateSyncStat2("Conflict: " + tabName);
 
				/*
					Application needs to do the right thing with conflictRow.
				*/
 
				// System.out.println("Conflict table " + tabName
					// + " row: " + conflictRow);
			}   
			return ISync.RTNCB_DONE;
 
			// ignore other event types
			default:
				break;
			}
 
		 // let sync engine take default action
		return ISync.RTNCB_DEFAULT ;
 
	}  // end of eventIssued() 
	/*
		Synchronization is implemented in a thread to allow the
		user to cancel the request which single-threaded, might 
		be hung on in IO request
	*/
 
	public void run()
	{
		sync();
	}
 
	public void cancel()
	{
		try
		{
			if (syncer != null)
				syncer.cancelSync();	
		}
		catch (ISyncException iex)
		{}
 
		mCancel = true;   
	}
 
	private void sync()
	{
		try
		{
			eventString = " ";
 
			String user = "nurse1";
			String password = "nurse1";
			String host = "localhost";
			String port = "9080";
			
			/*
				If jad file has values, use them, see
				DeployManifest.java in tools.
 
				In the Sun WirelessToolkit, under Settings, you can enter
				values in the User Defined tab.
			*/
 
			String x = midlet.getAppProperty("Db2eSyncUserName");
				if (x != null)
				user = x;
			x = midlet.getAppProperty("Db2eSyncPassword");
			if (x != null)
				password = x;
			x = midlet.getAppProperty("Db2eSyncHost");
			if (x != null)
				host = x;
			x = midlet.getAppProperty("Db2eSyncPort");
			if (x != null)
				port = x;
			midlet.appendForm(host + ":" + port + " " + user + "/" +
           password);
 
		
         // Step 3:  get an instance MIDPISyncProvider 
         //
			provider = MIDPISyncProvider.getInstance();
 
         // Step 4:  get an instance of synchronization service from the provider
         //
				Hashtable ht = new Hashtable();
				ht.put("isync.user", userName);
				ht.put("isync.password", password);
				ht.put("isync.trace", "detailed");
 
				service = provider.createSyncService(URI, ht);
 
         // Step 5:  get an instance of the configuration store
         // 
			config = service.getConfigStore(null);
 
         // Step 6:  get an instance of the sync driver to perform synchronization
         // 
			syncer = config.getSyncDriver();
 
         // Step 7:  set the listener object for event notification 
         //     from the syncer object during synchronization
         // 
			syncer.setSyncListener(this);
 
			// Step 8:  perform synchronization on all enabled subscription sets
            //
 
			int rc = syncer.sync();
 
			switch (rc)
			{
			  case ISync.RTN_SUCCEEDED:
				midlet.reportSyncStatus("Synchronization succeeded "
					+ eventString);
				break;
 
			  case ISync.RTN_CANCELED:
				midlet.reportSyncStatus("Synchronization canceled "
				 	+ eventString);      
				break;
 
			  default: 
				midlet.reportSyncStatus("Synchronization failed "
				 	+ eventString);        
				break;
			}        
		//  Step 9: Close all resources
		//
 
			close();
		}
		catch (ISyncException iex)
		{
			midlet.reportSyncStatus("Exception Code: "
				+ iex.getCode() + ", Event codes: " + eventString);	
		}
		catch (Exception e)
		{
			midlet.reportSyncStatus(e.toString());
		}
		finally
		{
			mCancel = false;   
		}
	}
	
	private void close() throws ISyncException
	{
		if (syncer != null)
		{
			syncer.close();
			syncer = null;
		}
		if (config != null)
		{
			config.close();
			config = null;
		}
 
		if (service != null)
		{
			service.close();
			service = null; 
		}
 
		provider = null;
	}
}
 

Související úlohy

Související koncepce