/sherlock-distributed-lock

Distributed locking library for JVM

Primary LanguageJavaApache License 2.0Apache-2.0

Sherlock Distributed Lock

Build Status Coverage Status Maven Central JavaDoc

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