Alachisoft NCache 4.1 - Online Documentation

Implementing BridgeConflictResolver Interface

 
Implementing BridgeConflictResolver
 
Following is a list of methods needs to be defined by the class implementing interface and a brief description of their purpose.
 
  • void init(HsahMap parameters);
 
This method performs tasks like allocating resources, acquiring connections etc. When cache is initialized and Conflict Resolvers is enabled, then it calls init method to notify the client that cache has initialized and you may initialize your conflict resolver too. Parameters passed as an argument contains all the parameters (if any) that were specified using NCache Manager cache/cluster views.
 
  • void dispose();
 
This method performs tasks like releasing the resources etc. When Cache is stopped or the task is completed, it calls the Dispose() method to notify the client that you can free the resources related to your task.
 
  • ConflictResolution resolve(ProviderBridgeItem oldEntry, ProviderBridgeItem newEntry);
 
This method should contain the logic to resolve the conflict occured while replicating an operation from bridge for a key that already exists in the cache. Whenever any conflict occurs, cache calls BridgeConflictResolution to decides which operation to apply on the cache depending upon rules implemented on it. It takes two parameters/entries, both are of ProviderBridgeItem type. ProviderBridgeItem contains the following parameters:
 
  • BridgeItemVersion contains version of an item.
  • Key contains key of an item.
  • Value of an item contains value of an item.
  • BridgeItemOpCodes contains conflicted operation.
 
Using BridgeConflictResolver:
 
Here is how your class should look like for BridgeConflictResolver:
 
public class BridgeConflictResolverImpl implements BridgeConflictResolver {
 
    private boolean enableLogging;
    private FileWriter textLogger;
 
    public void init(HashMap parameters) throws Exception {
 
        //If logging key is provided then based on it enable the bridge logging
        if(parameters.containsKey("logging"))
        {
            enableLogging = Boolean.getBoolean(parameters.get("logging").toString());
        }
 
        if(enableLogging)
        {
            textLogger = new FileWriter("bridgeResolverLog.txt");
        }
    }
 
    public ConflictResolution resolve(ProviderBridgeItem oldEntry, ProviderBridgeItem newEntry) throws Exception {
 
            ConflictResolution conflictResolution = new ConflictResolution();
            switch (oldEntry.getBridgeItemVersion())
            {
               
//If Old cache entry bridge item version is less than the new entry then replace the cache item with new Entry received from bridge
 
                case OLD:
                {
                    conflictResolution.setResolutionAction(ResolutionAction.ReplaceWithNewEntry);
                }
                break;
 
                //If old cache entry bridge item version is greater than the new entry then keep the old entry
                case LATEST:
                {
                    conflictResolution.setResolutionAction(ResolutionAction.KeepOldEntry);
                }
                break;
                //If both items have the same bridge item version
 
                case SAME:
                {
                    //Replace the new entry only if old entry operation is of Remove; else keep the old entry
 
                    if (oldEntry.getOpCode() == BridgeItemOpCodes.Remove)
                    {
                        conflictResolution.setResolutionAction(ResolutionAction.ReplaceWithNewEntry);
                    }
                    else
                    {
                        conflictResolution.setResolutionAction(ResolutionAction.KeepOldEntry);
                    }
                }
                break;
            }
 
            if(enableLogging)
            {
                textLogger.write("Resolve Key " + oldEntry.getKey() + " and decision taken " + conflictResolution.getResolutionAction().value());
            }
            return conflictResolution;
    }
 
    public void dispose() throws Exception {
            if(enableLogging)
            {
                textLogger.close();
            }
    }
 
}
 
NCache logs the warnings in Application event log in case of an exception during loading the jar files.
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.