Class MongoLockProvider
java.lang.Object
net.javacrumbs.shedlock.provider.mongo.MongoLockProvider
- All Implemented Interfaces:
net.javacrumbs.shedlock.core.ExtensibleLockProvider, net.javacrumbs.shedlock.core.LockProvider
public class MongoLockProvider
extends Object
implements net.javacrumbs.shedlock.core.ExtensibleLockProvider
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 is used for troubleshooting. lockedBy also identifies the owner when extending a lock.
- Upserts a document using the filter _id == name AND lockUntil <= now.
- If the document is inserted or an expired lock is updated, we have the lock.
- If a valid lock already exists, the insert fails with a duplicate key error and the lock is unavailable.
- When unlocking, lockUntil is set to now or lockAtLeastUntil, whichever is later.
Uses the application server clock. To use the Mongo server clock, use create(MongoDatabase, boolean) with useDbTime set to true.
-
Constructor Summary
ConstructorsConstructorDescriptionMongoLockProvider(com.mongodb.client.MongoCollection<org.bson.Document> collection) Uses the application server clock and the supplied collection.MongoLockProvider(com.mongodb.client.MongoDatabase mongoDatabase) Uses the application server clock and the defaultshedLockcollection with majority write concerns. -
Method Summary
Modifier and TypeMethodDescriptionstatic net.javacrumbs.shedlock.core.ExtensibleLockProvidercreate(com.mongodb.client.MongoCollection<org.bson.Document> collection, boolean useDbTime) Creates a lock provider using the supplied collection.static net.javacrumbs.shedlock.core.ExtensibleLockProvidercreate(com.mongodb.client.MongoDatabase mongoDatabase, boolean useDbTime) Creates a lock provider using the defaultshedLockcollection with majority read and write concerns.Optional<net.javacrumbs.shedlock.core.SimpleLock> lock(net.javacrumbs.shedlock.core.LockConfiguration lockConfiguration)
-
Constructor Details
-
MongoLockProvider
public MongoLockProvider(com.mongodb.client.MongoDatabase mongoDatabase) Uses the application server clock and the defaultshedLockcollection with majority write concerns. -
MongoLockProvider
public MongoLockProvider(com.mongodb.client.MongoCollection<org.bson.Document> collection) Uses the application server clock and the supplied collection. Please, make sure that the collection hasWriteConcern.MAJORITYset. Read concern is not used by the atomicfindOneAndUpdateoperations.- Parameters:
collection- Mongo collection to be used
-
-
Method Details
-
create
public static net.javacrumbs.shedlock.core.ExtensibleLockProvider create(com.mongodb.client.MongoDatabase mongoDatabase, boolean useDbTime) Creates a lock provider using the defaultshedLockcollection with majority read and write concerns.Using the Mongo server clock requires MongoDB >= 4.2. In this mode, existing lock names are cached, so externally deleted documents are not automatically recreated while their names remain cached. The cache can be cleared by casting the returned provider to
StorageBasedLockProviderand callingStorageBasedLockProvider.clearCache().- Parameters:
mongoDatabase- Mongo database to be useduseDbTime-trueto use the Mongo server clock,falseto use the application server clock- Returns:
- a lock provider using the selected clock
-
create
public static net.javacrumbs.shedlock.core.ExtensibleLockProvider create(com.mongodb.client.MongoCollection<org.bson.Document> collection, boolean useDbTime) Creates a lock provider using the supplied collection. Please, make sure that the collection hasWriteConcern.MAJORITYset.See
create(MongoDatabase, boolean)for MongoDB version requirements and lock name caching when using the Mongo server clock.- Parameters:
collection- Mongo collection to be useduseDbTime-trueto use the Mongo server clock,falseto use the application server clock- Returns:
- a lock provider using the selected clock
-
lock
public Optional<net.javacrumbs.shedlock.core.SimpleLock> lock(net.javacrumbs.shedlock.core.LockConfiguration lockConfiguration) - Specified by:
lockin interfacenet.javacrumbs.shedlock.core.LockProvider
-