Interface DistributedLock

  • All Known Implementing Classes:
    DistributedLockMock

    public interface DistributedLock
    A lock for distributed environment consisting of multiple application instances.
    See Also:
    Sherlock
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      boolean acquire()
      Try to acquire the lock.
      boolean acquire​(java.time.Duration duration)
      Try to acquire the lock for a given duration.
      default boolean acquireAndExecute​(java.lang.Runnable action)
      Acquire a lock and release it after action is executed.
      default boolean acquireAndExecute​(java.time.Duration duration, java.lang.Runnable action)
      Acquire a lock for a given duration and release it after action is executed.
      boolean acquireForever()
      Try to acquire the lock without expiring date.
      default boolean acquireForeverAndExecute​(java.lang.Runnable action)
      Acquire a lock without expiration time and release it after action is executed.
      java.lang.String getId()
      Return the lock id.
      boolean release()
      Release the lock
      default boolean releaseAndExecute​(java.lang.Runnable action)
      Run the action when lock is released
    • Method Detail

      • getId

        java.lang.String getId()
        Return the lock id.
        Returns:
        the lock id
      • acquire

        boolean acquire()
        Try to acquire the lock. Lock is acquired for a configured duration.
        Returns:
        true, if lock is acquired
      • acquire

        boolean acquire​(java.time.Duration duration)
        Try to acquire the lock for a given duration.
        Parameters:
        duration - how much time must pass for the acquired lock to expire
        Returns:
        true, if lock is acquired
      • acquireForever

        boolean acquireForever()
        Try to acquire the lock without expiring date. It is potentially dangerous. Lookout for a situation where the lock owning instance goes down with out releasing the lock.
        Returns:
        true, if lock is acquired
      • release

        boolean release()
        Release the lock
        Returns:
        true, if lock was released in this call. If lock has expired or was released by a different instance then false is returned.
      • acquireAndExecute

        default boolean acquireAndExecute​(java.lang.Runnable action)
        Acquire a lock and release it after action is executed.
        Parameters:
        action - to be executed when lock is acquired
        Returns:
        true if lock is acquired.
        See Also:
        acquire()
      • acquireAndExecute

        default boolean acquireAndExecute​(java.time.Duration duration,
                                          java.lang.Runnable action)
        Acquire a lock for a given duration and release it after action is executed.
        Parameters:
        duration - how much time must pass for the acquired lock to expire
        action - to be executed when lock is acquired
        Returns:
        true, if lock is acquired
        See Also:
        acquire(Duration)
      • acquireForeverAndExecute

        default boolean acquireForeverAndExecute​(java.lang.Runnable action)
        Acquire a lock without expiration time and release it after action is executed.
        Parameters:
        action - to be executed when lock is acquired
        Returns:
        true, if lock is acquired
        See Also:
        acquireForever()
      • releaseAndExecute

        default boolean releaseAndExecute​(java.lang.Runnable action)
        Run the action when lock is released
        Parameters:
        action - to be executed when lock is released
        Returns:
        true, if lock was release
        See Also:
        release()