coditory / sherlock-distributed-lock

Distributed lock for JVM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Distributed lock for JVM

License:Apache License 2.0


Languages

Language:Java 52.2%Language:Groovy 32.0%Language:Kotlin 15.8%