Class RedisLockMiddleware

  • All Implemented Interfaces:
    LockMiddleware, Middleware, TimeProviderMiddleware

    public abstract class RedisLockMiddleware
    extends java.lang.Object
    implements LockMiddleware
    This uses Redis as backend for locking, using SETNX a lock is acquired.

    An implementation of this class must implement getLockIdentifier, that should return lock identifier for this job.

    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String acquireLock​(Job job)
      This method should acquire lock from any source like Redis, File, Java Lock or any system and returns lock identifier for the same.
      protected java.time.Duration getLockDuration​(Job job)
      A lock can be required for forever or 1 second, that's all depends on the use case, bny default a lock is acquired for visibility time.
      protected abstract java.lang.String getLockIdentifier​(Job job)
      Returns lock identifier for this job, could be a simple job id, user id or any other depending on the user case.
      void releaseLock​(Job job, java.lang.String lockIdentifier)
      This method id called to release lock acquired using LockMiddleware.acquireLock(Job).
      • Methods inherited from class java.lang.Object

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

      • RedisLockMiddleware

        public RedisLockMiddleware​(org.springframework.data.redis.connection.RedisConnectionFactory redisConnectionFactory)
      • RedisLockMiddleware

        public RedisLockMiddleware​(RqueueRedisTemplate<java.lang.String> template)
    • Method Detail

      • getLockIdentifier

        protected abstract java.lang.String getLockIdentifier​(Job job)
        Returns lock identifier for this job, could be a simple job id, user id or any other depending on the user case. This method must returns non null value.
        Parameters:
        job - job object
        Returns:
        lock identifier
      • getLockDuration

        protected java.time.Duration getLockDuration​(Job job)
        A lock can be required for forever or 1 second, that's all depends on the use case, bny default a lock is acquired for visibility time.
        Parameters:
        job - job object
        Returns:
        duration for this lock
      • releaseLock

        public void releaseLock​(Job job,
                                java.lang.String lockIdentifier)
        Description copied from interface: LockMiddleware
        This method id called to release lock acquired using LockMiddleware.acquireLock(Job).

        This method is always called

        Specified by:
        releaseLock in interface LockMiddleware
        Parameters:
        job - the running job
        lockIdentifier - lock identifier
      • acquireLock

        public java.lang.String acquireLock​(Job job)
        Description copied from interface: LockMiddleware
        This method should acquire lock from any source like Redis, File, Java Lock or any system and returns lock identifier for the same. If it's unable to acquire lock than this method must return null/empty string.If locking system does not support lock identifier than you can return Job.getId().
        Specified by:
        acquireLock in interface LockMiddleware
        Parameters:
        job - running job object
        Returns:
        lock identifier