复制代码代码如下:
pacote com.happyElements.odin.util;
importar com.google.common.base.preconditions.checknotnull;
importar org.Jetbrains.annotações.NotNull;
import com.happyElements.odin.jedis.jedIclient;
import com.happyElements.rdcenter.commons.util.dateTimeUtil;
/**
* Usuário: Jude.wang
* Data: 14-1-16
* Hora: 上午 9:43
*/
classe pública Concurrentutil {
public static final string user_lock_key_format = "user_lock_%d_%s";
public static final string custom_lock_format = "custom_lock_%s";
public estático final JedIclient redisclient = jedisclient.getInstance ();
public static final string desbloqueado = "0";
public static final string bloqueado = "1";
private estático final int max_repeat_times = 10;
@NotNull
public static string BuildUserlockKey (Long UserID, @NotNull String Key) {
checkNotNull (chave);
return string.format (user_lock_key_format, userID, chave);
}
@NotNull
public static string BuildCustomlockKey (@NotNull String Key) {
checkNotNull (chave);
return string.format (custom_lock_format, chave);
}
/**
* 此方法可以因为拿不到锁而导致 Operação 没有执行
*
* @param chave
* @see com.happyElements.odin.util.concurrentutil#BuildCustomlockKey (String)
* @see com.happyElements.odin.util.concurrentutil#BuildUserlockKey (long, string)
*
* Operação @param
* @throws com.happyElements.odin.util.concurrentutil.operationNotexException
* Operação 没有被执行
*/
public static void DojobWithlock (@NotNull String Key, @NotNull IlockOperation Operation) lança operaçãoNotexException {
booleano bloqueado = false;
tentar {
checkNotNull (chave);
checkNotNull (operação);
bloqueado = bloqueio (tecla);
} catch (throwable t) {
lançar uma nova operação não
}
tentar {
if (bloqueado) {
// system.out.println (thread.currentThread () + "/t" + "bloqueio");
Operação.dojob ();
} outro {
lançar uma nova operaçãoNotexException (chave);
}
} finalmente {
if (bloqueado) {
desbloquear (chave);
}
}
}
Desbloqueio de vazio estático privado (chave da string) {
tentar {
checkNotNull (chave);
String OldStatus = redisclient.getSet (chave, desbloqueado);
if (isunlocked (Oldstatus)) {
// Lock-> Dojob-> 过期-> desbloqueio
// TODO LOG
}
} catch (throwable t) {
// TODO LOG
}
// system.out.println (thread.currentThread () + "/t" + "desbloqueio");
}
private estático booleano isunlocked (status da string) {
status de retorno == null || status.equals (desbloqueado);
}
Private estático bloqueado estático (tecla de string) {
booleano bloqueado = false;
for (int i = 0; i <max_repeat_times; i ++) {
String oldstatus = redisclient.getSet (chave, bloqueado);
if (isunlocked (Oldstatus)) {
if (Oldstatus == null) {
redistlient.expire (chave, dateTimeUtil.minute_second * 5);
bloqueado = true;
quebrar;
}
bloqueado = true;
quebrar;
}
}
retornar trancado;
}
interface estática pública ilockoperation {
vazio dojob ();
}
/**
* {@link com.happyElements.odin.util.concurrentutil.ilockoperation#dojob ()} 没有被执行
* 上层必须处理该异常 , 捕获到该异常可以 RETILHA 本次操作 , 或者包装成 {@link com.happyElements.rdcenter.commons.throwable.heexception} 抛出去
*/
Classe estática pública OperaçãoNotexException estende a exceção {
Operação públicaNotexException () {
}
Operação públicaNotexException (strings) {
super (s);
}
Operação públicaNotexException (String S, jogável lançável) {
super (s, jogável);
}
Operação públicaNotexException (jogável lançável) {
super (jogável);
}
}
}