- ๐ Out of the box
- ๐ Automatic relocking
- ๐ Lock retry
- โ๏ธ Reentrant lock
- โ๏ธ Declarative locking
- ..........๏ผTo be continued๏ผ
- redis v6.0.0+
- jdk 1.8+
- ......
....๏ผTo be continued๏ผ
<dependency>
<groupId>io.github.disaster1-tesk</groupId>
<artifactId>lock-layer-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>io.github.disaster1-tesk</groupId>
<artifactId>lock-layer-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
# About Configuration
LockManager lockManager = LockManager.create(LockConfig.build().setClient(new JedisClient(new JedisPool("127.0.0.1", 6379, null, "123456"))));
RedisLockLayerLayer redisLockLayer = new RedisLockLayerLayer(lockManager);
#Using
Thread thread = new Thread(() -> {
redisLockLayer.tryLock("test_key");
try {
Thread.sleep(10000);
redisLockLayer.unLock("test_key");
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "thread1");
Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
redisLockLayer.tryLock("test_key");
}, "thread2");
thread.start();
thread1.start();
while (true) {
}
....๏ผTo be continued๏ผ
# If the lock layer reuses the configuration of spring-redis, the normal use of spring-redis will not be affected
spring:
redis:
port: 6379
host: localhost
password: 123456
jedis:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
timeout: 2000
lock:
layer:
declaration:
enable: true # This configuration enables declarative locking. The default value is false
Just DI LockLayer into the class to use
@Autowired
private LockLayer redisLockLayer;
@Component
public class Lock {
@LockLayer(key = "test_key", expireTime = 100)
public void lock() {
}
@LockLayer(key = "test_key", expireTime = 100, reentryLock = true)
public void retryLock() {
}
//When an exception is thrown in a annotated method, the lock layer automatically releases the lock
@LockLayer(key = "test_key", expireTime = 10)
public void lockException() {
throw new RuntimeException();
}
}
@SpringBootApplication
public class LockLayerApplication {
@Autowired
private LockLayer redisLockLayer;
@Autowired
private Lock lock;
public static void main(String[] args) {
SpringApplication.run(LockLayerApplication.class, args);
}
@Bean
public ApplicationRunner applicationRunner1(){
return args -> {
lock.lock();
lock.lockException();
};
}
@Bean
public ApplicationRunner applicationRunner(){
return args -> {
Thread thread = new Thread(() -> {
redisLockLayer.tryLock("test_key");
try {
Thread.sleep(10000);
redisLockLayer.unLock("test_key");
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "thread1");
Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
redisLockLayer.tryLock("test_key");
}, "thread2");
thread.start();
thread1.start();
};
}
}
If the lock layer is not dynamically configured, the default lock layer is used
#Global Settings, it should be noted that this yml file name must be lock-layer-extend.yml, if it is any other file name, the lock layer will not be able to load its configuration
lock:
layer:
max_expire_count: 3 # The maximum number of consecutive locks was set
max_retry_time: 30000 # The maximum lock retry time, after which the lock fails
max_expire_time : 60 # Maximum renewal time, which is the same as max_expire_count
max_reentry_count: 3 # The number of reentrants allowed
renew:
type: redis # Whether to retry locks in pub/sub mode. Thread pool is used by default
log:
enable: true # Enable lock layer logs. This function is disabled by default
The lock layer provides two interfaces, LockHeatProcessor and LockProcessor, to allow developers to expand operations when locks are successfully added, fail to be added, and when the lock duration times out
//LockHeatProcessor Indicates the interface for subsequent operations performed when the lock timeout occurs
@Service
public class LockHeatProcessorImpl implements LockHeatProcessor {
@Override
public void lockHeartRemovedProcessor(LockHeartBeatEntity value) {
System.out.println(value.getExpireCount());
}
}
//LockProcessor Indicates the extension interface for subsequent operations when a lock succeeds or fails
@Service
public class LockProcessorImpl implements LockProcessor {
@Override
public void failLockProcessor(LockEntity lockEntity) {
System.out.println("lock failure"+lockEntity.get_key());
}
@Override
public void successLockProcessor(LockEntity lockEntity) {
System.out.println("lock success"+lockEntity.get_key());
}
}