Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
jizcode.lock.LockTool Maven / Gradle / Ivy
package jizcode.lock;
import jizcode.base.*;
import jizcode.base.function.ListResultReturnable;
import jizcode.base.function.ServiceResultReturnable;
import jizcode.base.function.SingleResultReturnable;
import jizcode.base.tool.CacheTool;
public class LockTool extends Tool {
public final static int TIMEOUT_SECONDS = 30;
CacheTool cacheTool;
LockTool(ServiceContext context) {
super(context);
this.cacheTool = context.getCache();
}
//region service result lock
/**
* 锁定key
*
* @param key key名称
* @param timeout 锁定超时时间
* @param returnable 运行方法
* @return
*/
public ServiceResult xLock(String key, int timeout, ServiceResultReturnable returnable) {
if (LockManager.addKey(this.cacheTool, key, timeout)) {
try {
ServiceResult result = returnable.run();
LockManager.removeKey(this.cacheTool, key);
return result;
} catch (Exception ex) {
LockManager.removeKey(this.cacheTool, key);
return new ServiceResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ServiceResult(1, "锁定失败,已存在其他锁");
}
}
/**
* 锁定key,并执行,默认锁定30s
*
* @param key key名称
* @param returnable 运行方法
* @return 执行结果
*/
public ServiceResult xLock(String key, ServiceResultReturnable returnable) {
return xLock(key, TIMEOUT_SECONDS, returnable);
}
public ServiceResult xLock(String[] keys, int timeout, ServiceResultReturnable returnable) {
if (LockManager.addKeys(this.cacheTool, keys, timeout)) {
try {
ServiceResult result = returnable.run();
LockManager.removeKeys(this.cacheTool, keys);
return result;
} catch (Exception ex) {
LockManager.removeKeys(this.cacheTool, keys);
return new ServiceResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ServiceResult(1, "锁定失败,已存在其他锁");
}
}
public ServiceResult xLock(String[] keys, ServiceResultReturnable returnable) {
return xLock(keys, TIMEOUT_SECONDS, returnable);
}
/**
* 锁定key,并锁定relKey
*
* @param primaryKey 业务key
* @param relatvieKey 关联业务key
* @param timeout 默认锁定时间
* @param returnable 运行逻辑
* @return 执行结果
*/
public ServiceResult xLock(String primaryKey, String relatvieKey, int timeout, ServiceResultReturnable returnable) {
if (LockManager.addKey(this.cacheTool, primaryKey, relatvieKey, timeout)) {
try {
ServiceResult result = returnable.run();
LockManager.removeKey(this.cacheTool, primaryKey, relatvieKey);
return result;
} catch (Exception ex) {
LockManager.removeKey(this.cacheTool, primaryKey, relatvieKey);
return new ServiceResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ServiceResult(1, "锁定失败,已存在其他锁");
}
}
/**
* 检查锁key的的状态,如果key未被锁定,则锁定key,同时锁定relKey。
* relKey的锁定状态,并不影响key的锁定是否成功。
* relKey是否锁定成功,也并不影响key的锁定是否成功。
*
* @param primaryKey 业务锁
* @param relativeKey 同时锁定relKey
* @param returnable
* @return
*/
public ServiceResult xLock(String primaryKey, String relativeKey, ServiceResultReturnable returnable) {
return xLock(primaryKey, relativeKey, TIMEOUT_SECONDS, returnable);
}
public ServiceResult xLock(String[] primaryKeys, String relativeKey, int timeout, ServiceResultReturnable returnable) {
if (LockManager.addKeys(this.cacheTool, primaryKeys, relativeKey, timeout)) {
try {
ServiceResult result = returnable.run();
LockManager.removeKeys(this.cacheTool, primaryKeys, relativeKey);
return result;
} catch (Exception ex) {
LockManager.removeKeys(this.cacheTool, primaryKeys, relativeKey);
return new ServiceResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ServiceResult(1, "锁定失败,已存在其他锁");
}
}
public ServiceResult xLock(String[] primaryKeys, String relativeKey, ServiceResultReturnable returnable) {
return xLock(primaryKeys, relativeKey, TIMEOUT_SECONDS, returnable);
}
//endregion
//region single result lock
public SingleResult xLock(String key, int timeout, SingleResultReturnable returnable) {
if (LockManager.addKey(this.cacheTool, key, timeout)) {
try {
SingleResult result = returnable.run();
LockManager.removeKey(this.cacheTool, key);
return result;
} catch (Exception ex) {
LockManager.removeKey(this.cacheTool, key);
return new SingleResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new SingleResult(1, "锁定失败,已存在其他锁");
}
}
public SingleResult xLock(String key, SingleResultReturnable runnable) {
return xLock(key, TIMEOUT_SECONDS, runnable);
}
public SingleResult xLock(String[] keys, int timeout, SingleResultReturnable returnable) {
if (LockManager.addKeys(this.cacheTool, keys, timeout)) {
try {
SingleResult result = returnable.run();
LockManager.removeKeys(this.cacheTool, keys);
return result;
} catch (Exception ex) {
LockManager.removeKeys(this.cacheTool, keys);
return new SingleResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new SingleResult(1, "锁定失败,已存在其他锁");
}
}
public SingleResult xLock(String[] keys, SingleResultReturnable runnable) {
return xLock(keys, TIMEOUT_SECONDS, runnable);
}
/**
* 锁定key,并锁定relKey
*
* @param primaryKey 业务key
* @param relatvieKey 关联业务key
* @param timeout 默认锁定时间
* @param returnable 运行逻辑
* @return 执行结果
*/
public SingleResult xLock(String primaryKey, String relatvieKey, int timeout, SingleResultReturnable returnable) {
if (LockManager.addKey(this.cacheTool, primaryKey, relatvieKey, timeout)) {
try {
SingleResult result = returnable.run();
LockManager.removeKey(this.cacheTool, primaryKey, relatvieKey);
return result;
} catch (Exception ex) {
LockManager.removeKey(this.cacheTool, primaryKey, relatvieKey);
return new SingleResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new SingleResult(1, "锁定失败,已存在其他锁");
}
}
/**
* 检查锁key的的状态,如果key未被锁定,则锁定key,同时锁定relKey。
* relKey的锁定状态,并不影响key的锁定是否成功。
* relKey是否锁定成功,也并不影响key的锁定是否成功。
*
* @param primaryKey 业务锁
* @param relativeKey 同时锁定relKey
* @param returnable
* @return
*/
public SingleResult xLock(String primaryKey, String relativeKey, SingleResultReturnable returnable) {
return xLock(primaryKey, relativeKey, TIMEOUT_SECONDS, returnable);
}
public SingleResult xLock(String[] primaryKeys, String relativeKey, int timeout, SingleResultReturnable returnable) {
if (LockManager.addKeys(this.cacheTool, primaryKeys, relativeKey, timeout)) {
try {
SingleResult result = returnable.run();
LockManager.removeKeys(this.cacheTool, primaryKeys, relativeKey);
return result;
} catch (Exception ex) {
LockManager.removeKeys(this.cacheTool, primaryKeys, relativeKey);
return new SingleResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new SingleResult(1, "锁定失败,已存在其他锁");
}
}
public SingleResult xLock(String[] primaryKeys, String relativeKey, SingleResultReturnable returnable) {
return xLock(primaryKeys, relativeKey, TIMEOUT_SECONDS, returnable);
}
//endregion
//region list result lock
public ListResult xLock(String key, int timeout, ListResultReturnable returnable) {
if (LockManager.addKey(this.cacheTool, key, timeout)) {
try {
ListResult result = returnable.run();
LockManager.removeKey(this.cacheTool, key);
return result;
} catch (Exception ex) {
LockManager.removeKey(this.cacheTool, key);
return new ListResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ListResult(1, "锁定失败,已存在其他锁");
}
}
public ListResult xLock(String key, ListResultReturnable runnable) {
return xLock(key, TIMEOUT_SECONDS, runnable);
}
public ListResult xLock(String[] keys, int timeout, ListResultReturnable returnable) {
if (LockManager.addKeys(this.cacheTool, keys, timeout)) {
try {
ListResult result = returnable.run();
LockManager.removeKeys(this.cacheTool, keys);
return result;
} catch (Exception ex) {
LockManager.removeKeys(this.cacheTool, keys);
return new ListResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ListResult(1, "锁定失败,已存在其他锁");
}
}
public ListResult xLock(String[] keys, ListResultReturnable runnable) {
return xLock(keys, TIMEOUT_SECONDS, runnable);
}
/**
* 锁定key,并锁定relKey
*
* @param primaryKey 业务key
* @param relatvieKey 关联业务key
* @param timeout 默认锁定时间
* @param returnable 运行逻辑
* @return 执行结果
*/
public ListResult xLock(String primaryKey, String relatvieKey, int timeout, ListResultReturnable returnable) {
if (LockManager.addKey(this.cacheTool, primaryKey, relatvieKey, timeout)) {
try {
ListResult result = returnable.run();
LockManager.removeKey(this.cacheTool, primaryKey, relatvieKey);
return result;
} catch (Exception ex) {
LockManager.removeKey(this.cacheTool, primaryKey, relatvieKey);
return new ListResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ListResult(1, "锁定失败,已存在其他锁");
}
}
/**
* 检查锁key的的状态,如果key未被锁定,则锁定key,同时锁定relKey。
* relKey的锁定状态,并不影响key的锁定是否成功。
* relKey是否锁定成功,也并不影响key的锁定是否成功。
*
* @param primaryKey 业务锁
* @param relativeKey 同时锁定relKey
* @param returnable
* @return
*/
public ListResult xLock(String primaryKey, String relativeKey, ListResultReturnable returnable) {
return xLock(primaryKey, relativeKey, TIMEOUT_SECONDS, returnable);
}
public ListResult xLock(String[] primaryKeys, String relativeKey, int timeout, ListResultReturnable returnable) {
if (LockManager.addKeys(this.cacheTool, primaryKeys, relativeKey, timeout)) {
try {
ListResult result = returnable.run();
LockManager.removeKeys(this.cacheTool, primaryKeys, relativeKey);
return result;
} catch (Exception ex) {
LockManager.removeKeys(this.cacheTool, primaryKeys, relativeKey);
return new ListResult(1, String.format("执行出错:", ex.getMessage()));
}
} else {
return new ListResult(1, "锁定失败,已存在其他锁");
}
}
public ListResult xLock(String[] primaryKeys, String relativeKey, ListResultReturnable returnable) {
return xLock(primaryKeys, relativeKey, TIMEOUT_SECONDS, returnable);
}
//endregion
}