In Memory Distributed Lock¶
The in-memory connector was created for local development and testing purposes.
Usage¶
Add dependencies to build.gradle.kts
:
Create sherlock instance and distributed lock:
import com.coditory.sherlock.DistributedLock;
import com.coditory.sherlock.Sherlock;
import com.coditory.sherlock.inmem.InMemorySherlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InMemSyncLockSample {
private static final Logger logger = LoggerFactory.getLogger(InMemSyncLockSample.class);
public static void main(String[] args) {
Sherlock sherlock = InMemorySherlock.create();
DistributedLock lock = sherlock.createLock("sample-lock");
lock.runLocked(() -> logger.info("Lock acquired!"));
}
}
import com.coditory.sherlock.inmem.coroutines.InMemorySherlock
import kotlinx.coroutines.runBlocking
import org.slf4j.Logger
import org.slf4j.LoggerFactory
object InMemKtLockSample {
private val logger: Logger = LoggerFactory.getLogger(this.javaClass)
private suspend fun sample() {
val sherlock = InMemorySherlock.create()
val lock = sherlock.createLock("sample-lock")
lock.runLocked {
logger.info("Lock acquired!")
}
}
@JvmStatic
fun main(args: Array<String>) {
runBlocking { sample() }
}
}
import com.coditory.sherlock.inmem.reactor.InMemorySherlock;
import com.coditory.sherlock.reactor.DistributedLock;
import com.coditory.sherlock.reactor.Sherlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
public class InMemReactorLockSample {
private static final Logger logger = LoggerFactory.getLogger(InMemReactorLockSample.class);
public static void main(String[] args) {
Sherlock sherlock = InMemorySherlock.create();
DistributedLock lock = sherlock.createLock("sample-lock");
lock.runLocked(Mono.fromCallable(() -> {
logger.info("Lock acquired!");
return true;
})).block();
}
}
import com.coditory.sherlock.inmem.rxjava.InMemorySherlock;
import com.coditory.sherlock.rxjava.DistributedLock;
import com.coditory.sherlock.rxjava.Sherlock;
import io.reactivex.rxjava3.core.Single;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InMemRxLockSample {
private static final Logger logger = LoggerFactory.getLogger(InMemRxLockSample.class);
public static void main(String[] args) {
Sherlock sherlock = InMemorySherlock.create();
DistributedLock lock = sherlock.createLock("sample-lock");
lock.runLocked(Single.fromCallable(() -> {
logger.info("Lock acquired!");
return true;
})).blockingGet();
}
}
Learn more
See full source code example on Github.
Configuration¶
Configuration is available via sherlock builder:
Parameters:
clock
(default:Clock.systemUTC()
) - used to generate acquisition and expiration timestamps.lockDuration
(default:Duration.ofMinutes(5)
) - a default lock expiration time. If lock is not released and expiration time passes, the lock is treated as released.ownerIdPolicy
(default:uniqueOwnerId()
) - used to generate lock owner id. It's executed once for every lock, during lock creation. There are different policies available for generating lock ownerIds.sharedStorage
(default: no shared storage) - use shared storage for all in-mem locks.