Skip to content

coditory/sherlock-distributed-lock

Sherlock Distributed Lock

Build Coverage Maven Central

Java Distributed lock library with database migration capabilities

Look for details in the documentation.

Sample usage

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!"));