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.
  1. Upserts a document using the filter _id == name AND lockUntil <= now.
  2. If the document is inserted or an expired lock is updated, we have the lock.
  3. If a valid lock already exists, the insert fails with a duplicate key error and the lock is unavailable.
  4. 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

    Constructors
    Constructor
    Description
    MongoLockProvider(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 default shedLock collection with majority write concerns.
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    static net.javacrumbs.shedlock.core.ExtensibleLockProvider
    create(com.mongodb.client.MongoDatabase mongoDatabase, boolean useDbTime)
    Creates a lock provider using the default shedLock collection with majority read and write concerns.
    Optional<net.javacrumbs.shedlock.core.SimpleLock>
    lock(net.javacrumbs.shedlock.core.LockConfiguration lockConfiguration)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MongoLockProvider

      public MongoLockProvider(com.mongodb.client.MongoDatabase mongoDatabase)
      Uses the application server clock and the default shedLock collection 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 has WriteConcern.MAJORITY set. Read concern is not used by the atomic findOneAndUpdate operations.
      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 default shedLock collection 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 StorageBasedLockProvider and calling StorageBasedLockProvider.clearCache().

      Parameters:
      mongoDatabase - Mongo database to be used
      useDbTime - true to use the Mongo server clock, false to 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 has WriteConcern.MAJORITY set.

      See create(MongoDatabase, boolean) for MongoDB version requirements and lock name caching when using the Mongo server clock.

      Parameters:
      collection - Mongo collection to be used
      useDbTime - true to use the Mongo server clock, false to 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:
      lock in interface net.javacrumbs.shedlock.core.LockProvider