Listeners: What are conflict listeners and how to work with them?
To allow users to handle document replication conflicts automatically, we introduced a Document Conflict listener. To create your own listener of this type, just implement IDocumentConflictListener interface.
public interface IDocumentConflictListener {
public boolean tryResolveConflict(String key, List<JsonDocument> results, Reference<JsonDocument> resolvedDocument);
}
Example
This example shows how to create an automatic conflict resolver by using IDocumentConflictListener
, which will pick the newest item from the list of conflicted documents:
public class ResolveInFavourOfNewest implements IDocumentConflictListener {
@Override
public boolean tryResolveConflict(String key, List<JsonDocument> conflictedDocs, Reference<JsonDocument> resolvedDocument) {
long maxDate = 0;
for (JsonDocument doc : conflictedDocs) {
if (doc.getLastModified().getTime() > maxDate) {
maxDate = doc.getLastModified().getTime();
resolvedDocument.value = doc;
}
}
return resolvedDocument.value != null;
}
}