Java Distributed lock library with database migration capabilities
Look for details in the documentation.
- minimal set of dependencies - the main dependency is a database driver
- multiple types of locks - multiple ways to acquire a lock
- basic DB migration - basic database migration process using locks, no need for another library
- synchronous and reactive API - exposes synchronous and reactive API (supports Reactor and RxJava)
Add dependency to build.gradle
:
dependencies {
implementation "com.coditory.sherlock:sherlock-mongo-sync:0.4.14"
}
Create synchronous MongoDB backed lock:
// Get mongo locks collection
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017/sherlock");
MongoCollection<Document> collection = mongoClient
.getDatabase("sherlock")
.getCollection("locks");
// Create sherlock
Sherlock sherlock = MongoSherlock.builder()
.withLocksCollection(collection)
.build();
// Create a lock
DistributedLock lock = sherlock.createLock("sample-lock");
// Acquire a lock
lock.acquireAndExecute(() -> {
System.out.println("Lock granted!");
});