|
@@ -7,12 +7,14 @@ import com.google.common.cache.Cache;
|
|
import com.dayou.enums.CodeMsgEnumInterface;
|
|
import com.dayou.enums.CodeMsgEnumInterface;
|
|
import com.dayou.exception.ErrorCode;
|
|
import com.dayou.exception.ErrorCode;
|
|
import com.dayou.utils.LoginContext;
|
|
import com.dayou.utils.LoginContext;
|
|
|
|
+import com.google.common.cache.LoadingCache;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -31,9 +33,9 @@ public class AsyncManager {
|
|
|
|
|
|
private static ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
private static ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
|
|
|
- @Autowired
|
|
|
|
- @Qualifier("lockCache")
|
|
|
|
- private Cache<String, Object> lockCache;
|
|
|
|
|
|
+ private static Cache<String, Object> lockCache;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
@Qualifier("mainThreadPool")
|
|
@Qualifier("mainThreadPool")
|
|
@@ -41,6 +43,16 @@ public class AsyncManager {
|
|
AsyncManager.threadPoolTaskExecutor = threadPoolTaskExecutor;
|
|
AsyncManager.threadPoolTaskExecutor = threadPoolTaskExecutor;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ @Qualifier("lockCache")
|
|
|
|
+ public void setLockCache(Cache<String, Object> cache) {
|
|
|
|
+ AsyncManager.lockCache = cache;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Cache<String, Object> getLockCache() {
|
|
|
|
+ return lockCache;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
|
|
public static ThreadPoolTaskExecutor getThreadPoolTaskExecutor() {
|
|
return threadPoolTaskExecutor;
|
|
return threadPoolTaskExecutor;
|
|
}
|
|
}
|
|
@@ -215,21 +227,23 @@ public class AsyncManager {
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
String key = null;
|
|
String key = null;
|
|
Long userId = LoginContext.getLoginCacheUserBO().getId();
|
|
Long userId = LoginContext.getLoginCacheUserBO().getId();
|
|
- if (userId == null) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
switch (asyncLockStrategy) {
|
|
switch (asyncLockStrategy) {
|
|
case LOCK_BIZ:
|
|
case LOCK_BIZ:
|
|
key = prefix + asyncBiz.getCode();
|
|
key = prefix + asyncBiz.getCode();
|
|
break;
|
|
break;
|
|
case LOCK_USER:
|
|
case LOCK_USER:
|
|
|
|
+ if (userId == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
key = prefix + userId;
|
|
key = prefix + userId;
|
|
break;
|
|
break;
|
|
case LOCK_USER_BIZ:
|
|
case LOCK_USER_BIZ:
|
|
|
|
+ if (userId == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
key = prefix + userId + "_" + asyncBiz.getCode();
|
|
key = prefix + userId + "_" + asyncBiz.getCode();
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
-
|
|
|
|
if (lockCache.getIfPresent(key) != null) {
|
|
if (lockCache.getIfPresent(key) != null) {
|
|
ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR, asyncLockStrategy.tips);
|
|
ErrorCode.throwBusinessException(ErrorCode.CUSTOM_ERROR, asyncLockStrategy.tips);
|
|
}
|
|
}
|
|
@@ -240,7 +254,9 @@ public class AsyncManager {
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
try {
|
|
try {
|
|
- bizHandler.doCustom();
|
|
|
|
|
|
+ if (bizHandler.validBefore()){
|
|
|
|
+ bizHandler.doCustom();
|
|
|
|
+ }
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("异步任务异常", e);
|
|
log.error("异步任务异常", e);
|
|
} finally {
|
|
} finally {
|
|
@@ -283,6 +299,7 @@ public class AsyncManager {
|
|
* 异步业务类型
|
|
* 异步业务类型
|
|
*/
|
|
*/
|
|
public enum AsyncBiz implements CodeMsgEnumInterface<String, String> {
|
|
public enum AsyncBiz implements CodeMsgEnumInterface<String, String> {
|
|
|
|
+ BROKERAGE_SETTLE("BROKERAGE_SETTLE","提成结算")
|
|
;
|
|
;
|
|
|
|
|
|
private String key;
|
|
private String key;
|
|
@@ -352,7 +369,7 @@ public class AsyncManager {
|
|
* 前序校验
|
|
* 前序校验
|
|
* 在异步任务之前
|
|
* 在异步任务之前
|
|
*/
|
|
*/
|
|
- void validBefore();
|
|
|
|
|
|
+ Boolean validBefore();
|
|
|
|
|
|
Object doCustom();
|
|
Object doCustom();
|
|
}
|
|
}
|