-
Notifications
You must be signed in to change notification settings - Fork 13
Getting Started
This is the quick-and-dirty guide to adding mongo-java-distributed-lock to your application. By default, the library uses the database "mongo-distributed-lock" and the collections, "locks" and "lockHistory" - see the configuration doc for more information.
To obtain the lock service, you call:
DistributedLockSvcFactory lockSvcFactory
= new DistributedLockSvcFactory(new DistributedLockSvcOptions("mongodb://127.0.0.1:27017"));
DistributedLockSvc lockSvc = lockSvcFactory.getLockSvc();
Once you have the global lock service, you can create/access locks using:
// Make sure your lock names do not conflict with others.
Lock lock = lockSvc.create("com.deftlabs.mongo.testLock");
try {
lock.lock();
// Do something that requires a distributed lock
} finally {
lock.unlock();
}
There are contracts for the DistributedLockSvc and the DistributedLock objects.
-
DistributedLockSvc - You must call shutdown() when your process/application exits.
-
DistributedLock - You must call distributedLockSvc.destroy(distributedLock) when your application exits or if you wish to destroy the lock.
-
Clone the git repository and type: "ant jar" - the jar file is in build/dist
-
Add the library to your CLASSPATH
<dependency>
<groupId>com.deftlabs</groupId>
<artifactId>mongo-java-distributed-lock</artifactId>
<version>0.1.6</version>
</dependency>
<dependency org="com.deftlabs" name="mongo-java-distributed-lock" rev="0.1.6">
<artifact name="mongo-java-distributed-lock" type="jar" />
</dependency>
'com.deftlabs:mongo-java-distributed-lock:jar:0.1.6'
@Grapes(
@Grab(group='com.deftlabs', module='mongo-java-distributed-lock', version='0.1.6')
)