Skip to content

Latest commit

 

History

History
89 lines (63 loc) · 1.89 KB

README.md

File metadata and controls

89 lines (63 loc) · 1.89 KB

ddth-lock

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.

License

See LICENSE.txt for details. Copyright (c) 2016 Thanh Ba Nguyen.

Third party libraries are distributed under their own licenses.

Installation

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>

Usage

  1. 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();
}
  1. Lock/Unlock
java.util.concurrent.locks.Lock lock = lm.getLock("resource-id");

lock.lock();
try {
   ...
} finally {
    lock.unlock();
    //or lm.unlock(lock);
}
...
  1. Destroy the lock manager when done
((AbstractLockManager)manager).destroy();