Distributed redis lock implemented and integrated with spring boot.
To use redlock-spring-boot-starter, you import dependency into your POM.xml:
<dependency>
<groupId>com.lomagicode.spring.boot</groupId>
<artifactId>redlock-spring-boot-starter</artifactId>
<version>${spring-boot-redlock.version}</version>
</dependency>
Redlock-spring-boot is very easy-to-use, it exposed a @RedisLock annotation which you can just add to your method that you want to be executed in a redis lock. For example:
@Component
public class Example {
@RedisLock(lockKey="your-lock-key", expire=10, timeUnit=TimeUnit.SECONDS)
public void doTask() {
try {
// Do some task...
} catch (Exception e) {
// Handle exception.
}
}
}
With above code, when you call Example#doTask()
, it will try to aquire and hold the lock before really executing the method.
if it failed to aquire the lock, the method wouldn't be executed which was as expected.
Because
@RedisLock
is implemented with spring aop, theExample
class should must be a spring containered bean.
Exception in code wrapped with
@RedisLock
annotation MUST be caught and handled.
That's all, enjoy it!