A synchronization task as used here can be roughly viewed as the entry showing in the synchronization UI. However, at the technical level, it is represented by a SyncUnit implementation that contains a valid URI recognized by the SyncManager as defining something that requires synchronization. So, in order to create a new synchronization task, we are actually creating a new SyncUnit implementation.
To create a SyncUnit, two things are required:
- The type identifier
- The SyncUnit implementation class associated with the particular type identifier
The type identifier and the SyncUnit implementation class will be provided by the Sync Service (Sync Engine) provider.
The best way to explain how to create a new SyncUnit is via example. In this particular example, assume there is a Sync Service and Sync Engine that are capable of syncing files to a server. The com.ibm.filesync
type identifier is to be used, and the FileSyncUnit
is the implementation of the SyncUnit class used for the com.ibm.filesync
type. The Sync Engine needs to be told the local file and the target server to synchronize with. The code needs to call the SyncManager to create a SyncUnit.
- SyncManager sm = SyncManagerFactory.getSyncManager();
- FileSyncUnit template = (FileSyncUnit)sm.createSyncUnit( "com.ibm.filesync" );
- template.setLocalFile( "c:\\\\sync\\\\file1" );
- template.setTargetServer( "http://my.filesyncserver.com" );
- FileSyncUnit file1Instance = (FileSyncUnit)sm.add( template );
The SyncUnit returned on line 2 should be considered a template, which can then be modified through the setters on the class. The template SyncUnit has not yet actually been contributed to the Sync Manager and will therefore not display in the synchronization UI. Once the SyncUnit has been updated appropriately (calling getters and setters as needed (lines 3 and 4), then the Sync Manager is called again (line 5) to add this SyncUnit so that synchronization can occur in the future. The SyncUnit (line 5) returned from the add
method can be used in subsequent operations, such as adding a SyncEventListener. Only after the SyncUnit has been returned from the SyncManager, will it contain a valid URI that can be used later to obtain a SyncUnit representing the same synchronization task.
Once the template has been used to add a SyncUnit to the Sync Manager (line 5), the template can be updated and reused to add additional SyncUnits to the Sync Manager.
Parent topic: SyncManager class uses: XPD621