mvn clean install
<dependency>
<groupId>com.qingyuan1232</groupId>
<artifactId>redis-lock-spring-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
@PostMapping("/test")
@SLock(key = {"#user"}, lockFailHandler = "lockFailHandler")
public Object test(String user) throws InterruptedException {
Thread.sleep(1000);
return Thread.currentThread() + "success" + user;
}
public Object lockFailHandler(String user) {
return Thread.currentThread() + "lock fail" + user;
}
@Autowired
private LockTemplate lockTemplate;
@PostMapping("/test")
public Object test(String user) throws InterruptedException {
try {
if (lockTemplate.lock(user,1000L,1000L)){
Thread.sleep(1000);
}else {
//加锁失败
}
}finally {
lockTemplate.unLock(user);
}
return Thread.currentThread() + "success" + user;
}