Commit 40230f12 by 王仕雄

redis分布式锁参照

1 parent 589393c2
...@@ -36,6 +36,7 @@ public class BootdoApplication { ...@@ -36,6 +36,7 @@ public class BootdoApplication {
System.out.println("111"); System.out.println("111");
System.out.println("000411"); System.out.println("000411");
System.out.println("0002"); System.out.println("0002");
System.out.println("0001");
return new Object(); return new Object();
} }
} }
package com.bootdo.common.utils;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class CommonRedisHelper {
public static final String LOCK_PREFIX = "redis_lock_saas_cso_job";
public static final int LOCK_EXPIRE = 1000*30; // ms 此处默认设置为30秒
@SuppressWarnings("rawtypes")
@Autowired
private RedisTemplate redisTemplate;
/**
* 分布式锁
* 如果锁持有者,在超时时间之后都没有释放锁(死锁),那么后续拿锁一样会成功
* @param key 键
* @param lockExpire 超时时间(单位毫秒)
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean lock(String key,int lockExpire){
String lock = LOCK_PREFIX + key;
// 利用lambda表达式
return (Boolean) redisTemplate.execute((RedisCallback) connection -> {
long expireAt = System.currentTimeMillis() + lockExpire + 1;
Boolean acquire = connection.setNX(lock.getBytes(), String.valueOf(expireAt).getBytes());
if (acquire) {
return true;
} else {
byte[] value = connection.get(lock.getBytes());
if (Objects.nonNull(value) && value.length > 0) {
long expireTime = Long.parseLong(new String(value));
if (expireTime < System.currentTimeMillis()) {
// 如果锁已经过期
byte[] oldValue = connection.getSet(lock.getBytes(), String.valueOf(System.currentTimeMillis() + lockExpire + 1).getBytes());
// 防止死锁
return Long.parseLong(new String(oldValue)) < System.currentTimeMillis();
}
}
}
return false;
});
}
/**
* 删除锁
*
* @param key
*/
@SuppressWarnings("unchecked")
public void delete(String key) {
redisTemplate.delete(LOCK_PREFIX + key);
}
// public static void main(String[] args) throws InterruptedException {
//
// System.out.println(new String(String.valueOf(System.currentTimeMillis() + LOCK_EXPIRE + 1).getBytes()));
// Thread.sleep(3000);
// byte [] ss = String.valueOf(System.currentTimeMillis() + LOCK_EXPIRE + 1).getBytes();
// System.out.println(ss);
// String value =new String(ss);
// System.out.println(value);
// System.out.println(Long.parseLong(value));
// }
}
package com.bootdo.common.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
*
* @ClassName: TestJoba
* @Description: redis分布式锁的测试类,可保留参照
* @author wangsx
* @date 2019年12月4日
*
*/
@Component
public class TestJoba {
@Autowired
private CommonRedisHelper commonRedisHelper;
public final static String timer_key = "xsbftimer";
@Scheduled(cron = "1 02 15 * * ?")
public void xsbfTimeTaskOfEnableDt()throws Exception{
try {
if(commonRedisHelper.lock(timer_key,1000*30)) {
System.out.println("job-1 开始工作了,拿锁成功...");
Thread.sleep(3000);//休眠3秒
}else {
System.out.println("job-1 拿锁失败...");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
commonRedisHelper.delete(timer_key);
System.out.println("job-1 工作结束了,释放锁...");
}
}
@Scheduled(cron = "52 02 15 * * ?")
public void xsbfTimeTaskOfEnableDt2()throws Exception{
try {
if(commonRedisHelper.lock(timer_key,1000*30)) {
System.out.println("job-2 开始工作了,拿锁成功...");
Thread.sleep(3000);//休眠3秒
}else {
System.out.println("job-2 拿锁失败...");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
commonRedisHelper.delete(timer_key);
System.out.println("job-2 工作结束了,释放锁...");
}
}
@Scheduled(cron = "3 02 15 * * ?")
public void xsbfTimeTaskOfEnableDt3()throws Exception{
try {
if(commonRedisHelper.lock(timer_key,1000*30)) {
System.out.println("job-3 开始工作了,拿锁成功...");
Thread.sleep(3000);//休眠3秒
}else {
System.out.println("job-3 拿锁失败...");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
commonRedisHelper.delete(timer_key);
System.out.println("job-3 工作结束了,释放锁...");
}
}
@Scheduled(cron = "4 02 15 * * ?")
public void xsbfTimeTaskOfEnableDt4()throws Exception{
try {
if(commonRedisHelper.lock(timer_key,1000*30)) {
System.out.println("job-4 开始工作了,拿锁成功...");
Thread.sleep(3000);//休眠3秒
}else {
System.out.println("job-4 拿锁失败...");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
commonRedisHelper.delete(timer_key);
System.out.println("job-4 工作结束了,释放锁...");
}
}
@Scheduled(cron = "5 02 15 * * ?")
public void xsbfTimeTaskOfEnableDt5()throws Exception{
try {
if(commonRedisHelper.lock(timer_key,1000*30)) {
System.out.println("job-5 开始工作了,拿锁成功...");
Thread.sleep(3000);//休眠3秒
}else {
System.out.println("job-5 拿锁失败...");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
commonRedisHelper.delete(timer_key);
System.out.println("job-5 工作结束了,释放锁...");
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!