'In SOClass, how to prevent an Operation from completing from the client side?
Sometimes in your client code, you want to cancel the current operation or prevent it from completing. How can you do that?
Solution 1:[1]
One solution is to add a client rule to the FINAL_ACTION_REQUESTED event that consumes it. This will stop the operation from completing:
document.addRule(new Rule() {
@Override
protected void apply(KernelEvent e) {
e.consume();
}
}, KernelEventConstants.FINAL_ACTION_REQUESTED);
Since this could be a common scenario for you, you might also want to define a utility class to handle this situation. This also helps in preventing adding that rule multiple times across your code, which is not neat and also might have a performance penalty:
public final class OperationUtility {
private static volatile Rule preventOperationFromCompletingRule = null;
public static void preventOperationFromCompleting(KDocument doc) {
if (preventOperationFromCompletingRule == null) {
synchronized (OperationUtility.class) {
if (preventOperationFromCompletingRule == null) {
preventOperationFromCompletingRule = new Rule() {
@Override
protected void apply(KernelEvent e) {
e.consume();
}
};
doc.addRule(preventOperationFromCompletingRule, KernelEventConstants.FINAL_ACTION_REQUESTED);
}
}
}
}
}
This code uses the Singleton Pattern with Lazy Initialization as described here: https://en.wikipedia.org/wiki/Singleton_pattern#Lazy_initialization
And in your code you use the utility as follows:
OperationUtility.preventOperationFromCompleting(doc);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rashad Saleh |
