复制代码代码如下::
Paket com.happyElements.odin.util;
Import static com.google.common.base.preconditions.Checknotnull;
import org.jetbrains.annotations.notnull;
Import com.happyElements.odin.jedis.jedisclient;
com.happyElements.rdcenter.commons.util.datetimeutil;
/**
* Benutzer: jude.wang
* Datum: 14-1-16
* Zeit: 上午 9:43
*/
öffentliche Klasse Concurrentutil {
public static final String user_lock_key_format = "user_lock_%d_%s";
public static Final String custom_lock_format = "custom_lock_%s";
public static final jedisclient redisclient = jedisclient.getInstance ();
öffentliche statische endgültige Zeichenfolge Unlocked = "0";
Public Static Final String gesperrt = "1";
private statische endgültige int max_repeat_times = 10;
@Notnull
public static String BuildUserLockkey (Long UserId, @notnull String Key) {
checknotnull (Schlüssel);
return string.format (user_lock_key_format, userId, key);
}
@Notnull
public static String BuildCustomlockkey (@notnull String Key) {
checknotnull (Schlüssel);
return string.format (custom_lock_format, key);
}
/**
* 此方法可以因为拿不到锁而导致 Operation 没有执行
*
* @param key
* @see com.happyelements.odin.util.concurrentutil#BuildCustomlockkey (String)
* @see com.happyelements.odin.util.concurrentutil#BuildUserLockkey (lang, String)
*
* @param operation
* @throws com.happyelements.odin.util.concurrentutil.operationNotexexception
* Operation 没有被执行
*/
public static void dojobwithlock (@notnull String Key, @notnull illockoperation Operation) löst OperationNotexeCexception {aus
boolean gesperrt = falsch;
versuchen {
checknotnull (Schlüssel);
checknotnull (operation);
gesperrt = lock (Schlüssel);
} catch (throwable t) {
Neue OperationNotexeCexception (Schlüssel, T) werfen;
}
versuchen {
if (gesperrt) {
// system.out.println (thread.currentThread () + "/t" + "lock");
Operation.dojob ();
} anders {
Neue OperationNotexeCexception (Schlüssel) werfen;
}
} Endlich {
if (gesperrt) {
entsperren (Schlüssel);
}
}
}
private static void entsperr (String -Schlüssel) {
versuchen {
checknotnull (Schlüssel);
String oldStatus = redisclient.getSet (Schlüssel, freigeschaltet);
if (isunlocked (OldStatus)) {
// lock-> dojob-> 过期-> entsperren
// TODO LOG
}
} catch (throwable t) {
// TODO LOG
}
// system.out.println (thread.currentThread () + "/t" + "entsperr");
}
private statische boolean isunlocked (String -Status) {
Rückgabestatus == NULL || status.equals (freigeschaltet);
}
private statische booleale Sperre (String Key) {
boolean gesperrt = falsch;
für (int i = 0; i <max_repeat_times; i ++) {
String oldStatus = redisclient.getSet (Schlüssel, gesperrt);
if (isunlocked (OldStatus)) {
if (OldStatus == null) {
redisclient.expire (key, datetimeutil.minute_second * 5);
gesperrt = wahr;
brechen;
}
gesperrt = wahr;
brechen;
}
}
Rücksperrung;
}
öffentliche statische Schnittstelle ILOCOLOCOPERATION {
void Dojob ();
}
/**
* {@link com.happyelements.odin.util.concurrentutil.illockoperation#dojob ()} 没有被执行
* 上层必须处理该异常 , 捕获到该异常可以 捕获到该异常可以 本次操作 , 或者包装成 {@link com.happyElements.rdcenter.Commons.Throwable.heexception} 抛出去
*/
öffentliche statische Klasse OperationNotexeCexception erweitert die Ausnahme {
public operationNotexexception () {
}
public operationNotexexception (String s) {
Super (s);
}
public operationNotexexception (String s, Throwable Throwable) {
Super (s, Throwable);
}
public operationNotexecexception (throwable Throwable) {
super (throwbar);
}
}
}