Extend lock4j: Custom Executor, Key Builder, and Failure Strategy
masterlock4j provides several extension points:
1. Custom Executor: Implement your own lock executor and specify it in the @Lock4j(executor = ...) annotation. Ensure the implementation is registered as a Spring Bean.
2. Custom Lock Key Builder: Extend com.baomidou.lock.DefaultLockKeyBuilder and override buildKey to change how lock keys are generated.
3. Custom Lock Failure Strategy: Implement the LockFailureStrategy interface and override onLockFailure to define custom behavior when a lock cannot be acquired. The default is com.baomidou.lock.DefaultLockFailureStrategy.
// Custom Key Builder
@Component
public class MyLockKeyBuilder extends DefaultLockKeyBuilder {
@Override
public String buildKey(MethodInvocation invocation, String[] definitionKeys) {
String key = super.buildKey(invocation, definitionKeys);
// custom logic
return key;
}
}
// Custom Failure Strategy
@Component
public class MyLockFailureStrategy implements LockFailureStrategy {
@Override
public void onLockFailure(String key, long acquireTimeout, int acquireCount) {
// custom logic
}
}