There are a number of sample Java programs available to help you write Java synchronization applications for DB2 Everyplace.
See Přehled vzorových aplikací produktu DB2 Everyplace for information on where the samples are located.
The sample program ISyncSample.java demonstrates how to code a Sync Client application for DB2 Everyplace native synchronization provider.
The major steps of the ISyncSample.java sample application are:
import com.ibm.mobileservices.isync.*;
import com.ibm.mobileservices.isync.event.*;
For JNI-based synchronization provider, import com.ibm.mobileservices.isync.db2e.jni.*;
For Trap-based synchronization provider, import com.ibm.mobileservices.isync.db2e.sti.*;
// 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.db2e.jni.*; // Step 2: implement the eventIssued() method in the ISyncListener // interface if you are interested in event notification (optional) // public class ISyncSample implements ISyncListener { public ISyncSample () {} public int eventIssued(ISyncEvent evt) { int evtType = evt.getEventType(); switch(evtType) { // display event status case ISync.EVTTYPE_INFO: case ISync.EVTTYPE_ERROR: System.out.println ("*********************"); System.out.println ("SubsSet: " + evt.getSubscriptionSetName() ); System.out.println ("Subs: " + evt.getSubscriptionName() ); System.out.println ("SubsType: " + evt.getSubscriptionType() ); System.out.println ("Event Type: " + evtType ); System.out.println ("Event Code: " + evt.getEventCode() ); System.out.println ("Progress: " + evt.getSyncProgress()); System.out.println ("**********************\n"); return ISync.RTNCB_DONE; case ISync.EVTTYPE_RETRY: return ISync.RTNCB_REPLY_YES; case ISync.EVTTYPE_CONFLICT: return ISync.RTNCB_DONE; // ignore other event types default: break; } // let sync engine take default action return ISync.RTNCB_DEFAULT ; } public void runSample(String host, String port, String userID, String passwrd) { ISyncProvider provider = null; ISyncService service = null; ISyncConfigStore config = null; ISyncDriver syncer = null; String path = "data"; // a data directory under current dir ISyncSubscriptionSet ssArr[] = null; int rc = 0; try { // Step 3: get an instance DB2eISyncProvider // provider = DB2eISyncProvider.getInstance(); // Step 4: get an instance of synchronization service from the provider // /* For the DB2j sync client, the JDBC driver and url are required String driver = "com.ibm.db2j.jdbc.DB2jDriver"; String jdbcUrl = jdbc:db2j:crtlDb;create=true; */ if (driver != null) userProps.put("target.db.driver", driver); if (jdbcUrl != null) userProps.put("target.db.url", jdbcUrl); Properties userProps = new Properties(); userProps.put("isync.user", user); userProps.put("isync.password", password); userProps.put("isync.trace", "detailed"); service = provider.createSyncService(uri, userProps); // Step 5: get an instance of the configuration store // config = service.getConfigStore(path); // 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 (optional) syncer.setSyncListener(this); // Step 8: perform synchronization on all enabled subscription sets // rc = syncer.sync(); switch (rc) { case ISync.RTN_SUCCEEDED: System.out.println("Synchronization succeeded"); break; case ISync.RTN_CANCELED: System.out.println ("Synchronization canceled"); break; default: System.out.println ("Synchronization failed"); break; } ssArr = config.getSubscriptionSets(); for (int i=0; i < ssArr.length; i++ ) { System.out.print ("Subscription Set: " + ssArr[i].getName() + " Status: "); switch(ssArr[i].getStatus()) { case ISync.STATUS_READY: System.out.println("READY"); break; case ISync.STATUS_COMPLETED: System.out.println ("COMPLETED"); break; case ISync.STATUS_CANCELED: System.out.println ("CANCELED"); break; default: System.out.println ("FAILED"); break; } } } catch (ISyncException ie) { System.out.println("Exception code: " + ie.getCode()); ie.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // Step 9: close and free all allocated resources // try { if (syncer != null) { syncer.close(); syncer = null; } if (config != null) { config.close(); config = null; } if (service != null) { service.close(); service = null; } } catch(ISyncException ie2) { System.out.println("Exception code: " + ie2.getCode()); ie2.printStackTrace(); } } } // end runSample() public static void main(String args[]) { String host = "localhost"; String port = "8080"; String userID = "nurse1"; String passwrd = "nurse1"; ISyncSample isa = new ISyncSample(); if (args.length > 0) { if (args.length == 4) { host = args[0]; port = args[1]; userID = args[2]; passwrd = args[3]; } else System.out.println("Usage: java ISyncSample [host] [port] " + "[userid] [password]"); } isa.runSample(host, port, userID, passwrd); } // end main() } // end ISyncSample class
Související úlohy
Související koncepce
Související odkazy