Skip to content
Andrew Barchuk edited this page Jun 20, 2015 · 14 revisions

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.

Usage

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();
}

Contracts

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.

Standalone

  • Clone the git repository and type: "ant jar" - the jar file is in build/dist

  • Add the library to your CLASSPATH

Maven

<dependency>
    <groupId>com.deftlabs</groupId>
    <artifactId>mongo-java-distributed-lock</artifactId>
    <version>0.1.6</version>
</dependency>

Ivy

<dependency org="com.deftlabs" name="mongo-java-distributed-lock" rev="0.1.6">
    <artifact name="mongo-java-distributed-lock" type="jar" />
</dependency>

Buildr

'com.deftlabs:mongo-java-distributed-lock:jar:0.1.6'

Grape

@Grapes( 
@Grab(group='com.deftlabs', module='mongo-java-distributed-lock', version='0.1.6') 
)
Clone this wiki locally