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 and lockedBy are just for troubleshooting and are not read by the code
  1. 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.
  2. We will try to update lock record using filter _id == name AND lock_until <= now
  3. If the update succeeded (1 updated document), we have the lock. If the update failed (0 updated documents) somebody else holds the lock
  4. 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 locks
    MongoLockProvider(com.mongodb.client.MongoDatabase mongoDatabase)
    Uses Mongo to coordinate locks
  • Method Summary

    Modifier and Type
    Method
    Description
    Optional<net.javacrumbs.shedlock.core.SimpleLock>
    lock(net.javacrumbs.shedlock.core.LockConfiguration lockConfiguration)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public Optional<net.javacrumbs.shedlock.core.SimpleLock> lock(net.javacrumbs.shedlock.core.LockConfiguration lockConfiguration)
      Specified by:
      lock in interface net.javacrumbs.shedlock.core.LockProvider