Class MongoLockProvider
java.lang.Object
net.javacrumbs.shedlock.provider.mongo.MongoLockProvider
- All Implemented Interfaces:
LockProvider
public class MongoLockProvider extends Object implements LockProvider
Distributed lock using MongoDB >= 2.6. Requires mongo-java-driver > 3.4.0
It uses a collection that contains documents like this:
{
"_id" : "lock name",
"lockUntil" : ISODate("2017-01-07T16:52:04.071Z"),
"lockedAt" : ISODate("2017-01-07T16:52:03.932Z"),
"lockedBy" : "host name"
}
lockedAt and lockedBy are just for troubleshooting and are not read by the code
- Attempts to insert a new lock record. As an optimization, we keep in-memory track of created lock records. If the record has been inserted, returns lock.
- We will try to update lock record using filter _id == name AND lock_until <= now
- If the update succeeded (1 updated document), we have the lock. If the update failed (0 updated documents) somebody else holds the lock
- When unlocking, lock_until is set to now.
-
Constructor Summary
Constructors Constructor Description MongoLockProvider(com.mongodb.client.MongoCollection<org.bson.Document> collection)Uses Mongo to coordinate locksMongoLockProvider(com.mongodb.client.MongoDatabase mongoDatabase)Uses Mongo to coordinate locks -
Method Summary
Modifier and Type Method Description Optional<SimpleLock>lock(LockConfiguration lockConfiguration)
-
Constructor Details
-
MongoLockProvider
public MongoLockProvider(com.mongodb.client.MongoDatabase mongoDatabase)Uses Mongo to coordinate locks -
MongoLockProvider
public MongoLockProvider(com.mongodb.client.MongoCollection<org.bson.Document> collection)Uses Mongo to coordinate locks- Parameters:
collection- Mongo collection to be used
-
-
Method Details
-
lock
- Specified by:
lockin interfaceLockProvider
-