DDTH's API for shared lock(s).
Project home: https://github.com/DDTH/ddth-lock
OSGi environment: ddth-lock
is packaged as an OSGi bundle.
See LICENSE.txt for details. Copyright (c) 2016 Thanh Ba Nguyen.
Third party libraries are distributed under their own licenses.
Latest release version: 0.1.0
. See RELEASE-NOTES.md.
Maven dependency: if only a sub-set of ddth-lock
functionality is used, choose the corresponding
dependency artifact(s) to reduce the number of unused jar files.
ddth-lock-core: in-memory locks, all other dependencies optional.
<dependency>
<groupId>com.github.ddth</groupId>
<artifactId>ddth-lock-core</artifactId>
<version>0.1.0</version>
</dependency>
ddth-lock-zk: distributed lock implementation based on Apache ZooKeeper, include all ddth-lock-core and ZooKeeper dependencies.
<dependency>
<groupId>com.github.ddth</groupId>
<artifactId>ddth-lock-zk</artifactId>
<version>0.1.0</version>
<type>pom</type>
</dependency>
- Obtain the lock manager
// in-memory lock manager: at one time, only one thread within the JVM can acquire the lock.
ILockManager lm = new InmemLockManager().init();
// ZooKeeper-based lock manager: at one time, only one thread (across JVMs) can acquire the lock.
ZooKeeperClient zkClient = new ZooKeeperClient();
{
zkClient.setConnectString("localhost:2181").setSessionTimeout(5000);
zkClient.init();
}
ZkLockManager lm = new ZkLockManager();
{
lm.setZkClient(zkClient);
lm.init();
}
- Lock/Unlock
java.util.concurrent.locks.Lock lock = lm.getLock("resource-id");
lock.lock();
try {
...
} finally {
lock.unlock();
//or lm.unlock(lock);
}
...
- Destroy the lock manager when done
((AbstractLockManager)manager).destroy();