Java Distributed lock library with database migration capabilities
Look for details in the documentation.
- supports multiple databases - SQL databases, MongoDB and more
- multiple types of locks - multiple ways to acquire a lock: reentrant, single-entrant and more
- basic DB migration - provides database migration process based on locks. No need for another library.
- synchronous and reactive API - exposes synchronous and reactive API (supports synchronous calls, Reactor, RxJava, Kotlin coroutines)
- minimal set of dependencies - the main dependency is a database driver
Add dependency to build.gradle
:
dependencies {
implementation "com.coditory.sherlock:sherlock-mongo:$version"
}
Create synchronous MongoDB backed lock:
// Get mongo collection
// You can also use other DB or reactive API
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017/sherlock");
MongoCollection<Document> collection = mongoClient
.getDatabase("sherlock")
.getCollection("locks");
// Create sherlock
Sherlock sherlock = MongoSherlock.create(collection);
// Create a lock
DistributedLock lock = sherlock.createLock("sample-lock");
// Acquire a lock, run action and finally release the lock
lock.runLocked(() -> System.out.println("Lock granted!"));