org.esbtools.eventhandler.lightblue.locking

Class LightblueAutoPingLockStrategy

  • java.lang.Object
    • org.esbtools.eventhandler.lightblue.locking.LightblueAutoPingLockStrategy
  • All Implemented Interfaces:
    LockStrategy


    public class LightblueAutoPingLockStrategy
    extends Object
    implements LockStrategy
    Uses lightblue's locking APIs with TTL, automatically pinging the lock in the background until it is released.

    Lightblue locks without TTLs are unsafe. If a client crashes, the lock will not be released. However, locks with TTLs may expire prematurely. By pinging the lock in a separate thread periodically, we can substantially lesson the likelihood of unintentionally losing the lock.

    Consumers are expected to check the lock at critical points to ensure it did not expire by calling LockedResource.ensureAcquiredOrThrow(String).

    • Constructor Detail

      • LightblueAutoPingLockStrategy

        public LightblueAutoPingLockStrategy(com.redhat.lightblue.client.Locking locking,
                                             java.time.Duration autoPingInterval)
        Same as LightblueAutoPingLockStrategy(Locking, Duration, Duration) except the timeToLive duration defaults to 5 times the autoPingInterval.
      • LightblueAutoPingLockStrategy

        public LightblueAutoPingLockStrategy(com.redhat.lightblue.client.Locking locking,
                                             java.time.Duration autoPingInterval,
                                             java.time.Duration timeToLive)
        Validates timeToLive is greater than the autoPingInterval.
        Parameters:
        locking - The locking client to use which holds knowledge of the locking domain to use. The default clientId is ignored.
        autoPingInterval - Amount of time in between automatic pings of acquired locks.
        timeToLive - Time until locks automatically expire. Should be [much] larger than the autoPingInterval to ensure locks do not accidentally expire.
    • Method Detail

      • tryAcquire

        public <T> LockedResource<T> tryAcquire(String resourceId,
                                                T resource)
                                         throws LockNotAvailableException
        Description copied from interface: LockStrategy
        Attempts to acquire a lock by the provided resourceId for the logical resource provided by resource.

        After a resource is locked, it is expected that nothing else can acquire it again until it is released (intentionally or not), no matter if it is the same thread, another thread, or across a network.

        Specified by:
        tryAcquire in interface LockStrategy
        Type Parameters:
        T - The type of the resource being acquired.
        Parameters:
        resourceId - A string which identifies the resource and only that resource.
        resource - A logically locked resource as a result of the id being locked. Participants must agree on a scheme of resourceIds to resources.
        Returns:
        A wrapper around the locked resource with methods to manage the acquired lock.
        Throws:
        LockNotAvailableException - If for whatever reason the lock could not be acquired. Generally this is simply because another client has the lock, but it could also be due to network failure, etc.

Copyright © 2017. All rights reserved.