Distributed lock ensures your method cannot be run in parallel from multiple JVMs (cluster of servers, microservices, …). It uses a common store to keep track of used locks and your method needs to acquire lock to run.
By default, locks follow methods lifecycle. They are obtained at the start of the method and released at the end of the method. Manual controlling is supported and explained later in this document.
All locks acquired by SimpleLock implementations in this project will expire after 10 seconds by default.
These options are customizable.
Use the releaseAfter attribute to control the duration of holding the lock (default is 10 seconds)
Note this flag will override the releaseAfter if set altogether
Check the simplelock-example module for more context.
Using @SimpleJdbcLocked annotation:
@Component
public class MyJdbcLockedService {
/**
* Invoke with distributed JDBC lock, by holding the lock.
*/
@SimpleJdbcLocked
public void doSomething() {
// locked method body
}
/**
* Invoke with distributed JDBC lock, by releasing the lock immediately.
*/
@SimpleJdbcLocked(releaseStrategy = ReleaseStrategy.WITHOUT_DELAY)
public void doSomethingElse() {
// locked method body
}
}or programmatically, where you’re allowed to propagate the lock token further into your service and release the lock whenever suitable for your use case, e.g. by sending the token in an event payload and release the lock after the event has been received back from the message broker:
@Component
@RequiredArgsConstructor
@Slf4j
public class MyJdbcLockedService {
private final SimpleLock simpleLock;
public void doSomething(UUID id) {
simpleLock.acquire(id.toString())
// IMPORTANT: lock has been acquired successfully only if the token is present,
// so make sure you have this check wrapping your service logic that needs to be locked
.ifPresent(token -> {
log.info("Some action executed concurrently for id={}", id);
simpleLock.release(token);
});
}
}Project provides the following out-of-the-box SimpleLock implementations:
-
SQL (see
JdbcSimpleLock) -
NoSQL (see
MongoSimpleLock)
List of supported NoSQL databases:
-
MongoDB
| Property | Description | Default |
|---|---|---|
|
Whether the JDBC lock is enabled |
|
|
Whether the lock expiry is enabled |
|
|
Whether the release strategy is with_delay or without_delay |
|
|
Expiry delay |
|
|
Time unit of the expiry delay |
|
| Annotation |
|---|
|
| Property | Description | Default |
|---|---|---|
|
Whether the MongoDB lock is enabled |
|
|
Whether the lock expiry is enabled |
|
|
Whether the release strategy is with_delay or without_delay |
|
|
Expiry delay |
|
|
Time unit of the expiry delay |
|
| Annotation |
|---|
|
Add the jitpack repository into your pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>Use the BOM com.github.natureknight.simplelock:simplelock-dependencies:3.1.2 or add the project dependency into your pom.xml for SQL:
<dependencies>
<dependency>
<groupId>com.github.natureknight.simplelock</groupId>
<artifactId>simplelock-spring-starter-jdbc</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>Or add the project dependency into your pom.xml for NoSQL:
<dependencies>
<dependency>
<groupId>com.github.natureknight.simplelock</groupId>
<artifactId>simplelock-spring-starter-mongo</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>Add the jitpack repository into your build.gradle:
repositories {
maven {
url = 'https://jitpack.io'
}
}Add the project dependency into your build.gradle based on your needs (choose either SQL or NoSQL):
depedencyManagement {
imports {
mavenBom 'com.github.natureknight.simplelock:simplelock-dependencies:3.1.2'
}
}
dependencies {
implementation('com.github.natureknight.simplelock:simplelock-spring-starter-jdbc')
// or
implementation('com.github.natureknight.simplelock:simplelock-spring-starter-mongo')Or add the project dependency into your build.gradle without using the BOM:
implementation('com.github.natureknight.simplelock:simplelock-spring-starter-jdbc:3.1.2')
// or
implementation('com.github.natureknight.simplelock:simplelock-spring-starter-mongo:3.1.2')If you want to use custom lock implementations, simply implement com.simplelock.api.SimpleLock interface and register it in a configuration.
-
Simplify project’s logic
-
Fixed minor issue related to MongoDB index creating
-
Cleanup