使用redis锁:直接上代码,参考验证
RedisLock lock = null;
try {
lock =RedisLockClient.getLock(String.format("claimID_%s",claimId));
if (lock!=null) {
return bpmProcessService.rollBack(workitemId, handlerName);
}else {
throw new BPMClientException(BPMClientException.MSG_ID.SUBMIT_ACTION_PERFORMING, claimId);
}
} finally {
if (lock!=null) {
lock.unlock();
}
}
public void convertToImportList(List<ClaimInfoBean> list) {
List<RedisLock> lockList = new ArrayList<RedisLock>();
if (list != null && !list.isEmpty()) {
try {
for (int i = list.size() - 1; i > -1; i--) {
ClaimInfoBean claimInfoBean = list.get(i);
ClaimBase claimBase = claimInfoBean.getClaimBase();
String key = String.valueOf(claimBase.getClaimNo());
RedisLock lock = null;
try {
lock = RedisLockClient.getLock(key);
} catch (Exception e) {
logger.error(String.format("claimNo[%s] 加锁失败移除该批次", claimBase.getClaimNo()),e);
//获取redis锁异常后,去除该单据
list.remove(i);
continue;
}
if (lock != null) {
lockList.add(lock);
if (!claimBaseService.changeInvoiceStatusToImporting(claimBase.getClaimId())) {
logger.info(String.format("ClaimId[%s]ClaimNo[%s]当前状态为导入中[%s],请刷新状态", claimBase.getClaimId(), claimBase.getClaimNo(), ClaimExt.InvoiceState.DRZ.code));
list.remove(i);
}
} else {
// other status , skip the record.
logger.error(String.format("ClaimId[%s]ClaimNo[%s]并发控制,请稍候执行", claimBase.getClaimId(), claimBase.getClaimNo()));
list.remove(i);
}
}
ClaimTaskEsbBean esbBean = claimTaskEsbDAO.getClaimTaskEsbBeanByTaskCode(ClaimTaskConstant.CLAIM_TASK_51);
super.checkAndImport(list,esbBean,esbBean.getEsbUrl());
} finally {
RedisLockClient.unLock(lockList);
}
}
}
List<PayRequestLine> requestList = payRequestLineService.getPayRequestLineByOuCode(queryParams);
List<RedisLock> locks = null;
// 获取创建最大数量
if (requestList == null || requestList.isEmpty()) {
return;
}
try {
locks = RedisLockClient.getLock(requestList);
if (locks != null) {
List<List<PayRequestLine>> pageList = getPageRequestLineList(requestList, operator.getPaymentMax() == null ? 10 : operator.getPaymentMax());
if (pageList != null) {
for (List<PayRequestLine> lineList : pageList) {
this.createBatchByPayReqLines(operator, lineList, createMode);
}
}
}
} finally {
RedisLockClient.unLock(locks);
}
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import com.crc.fssc.efinance.bpm.service.process.Impl.BPMClient;
import com.crc.fssc.efinance.claim.common.pojo.ClaimBase;
import com.crc.fssc.efinance.claim.payment.pojo.PayLineDetail;
import com.crc.fssc.efinance.claim.payment.pojo.PayRequestLine;
import com.google.common.collect.Lists;
/**
RedisLockClient工具类
* @Auther:
* @Date:
* @Company:
* @Description:
*/
public class RedisLockClient {
private static final Log myLogger = LogFactory.getLog(BPMClient.class);
public static RedisLock getLock(final String key, long expireSeconds) {
// 每次重新生成一个RedisLockUtils
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
RedisLockUtils u = (RedisLockUtils) wac.getBean("redisLockUtils");
return u.getLock(key, expireSeconds);
}
public static RedisLock getLock(final String key) {
return getLock(key, 600);
}
public static List<RedisLock> getLock(final List<?> objects, String filedName) {
boolean isAllLock = true;
List<RedisLock> locks = new ArrayList<RedisLock>();
Set<String> lockSet = new HashSet<String>();
if (objects != null) {
for (Object dataObject : objects) {
if (dataObject != null) {
Field field = ReflectionUtils.findField(dataObject.getClass(), filedName);
String key;
if (field == null) {
isAllLock = false;
break;
} else {
field.setAccessible(true);
Object filedObject = ReflectionUtils.getField(field, dataObject);
if (filedObject != null) {
key = String.format("%s_%s", filedName, filedObject.toString());
if (lockSet.contains(key)) {
continue;
} else {
RedisLock lock = getLock(key, 900);
if (lock != null) {
locks.add(lock);
lockSet.add(key);
} else {
isAllLock = false;
break;
}
}
}
}
}
}
} else {
isAllLock = false;
}
if (!isAllLock) {
for (RedisLock l : locks) {
l.unlock();
}
locks = null;
}
return locks;
}
@SuppressWarnings("unchecked")
public static List<RedisLock> getLock(final List<?> objects) {
String filedName="";
Object o = objects.get(0);
if( o instanceof String) {
return getLockByString((List<String>) objects);
}else if( o instanceof PayRequestLine) {
filedName ="invoiceNo";
}else if( o instanceof PayLineDetail) {
filedName ="payLineDetailId";
}else if( o instanceof ClaimBase) {
filedName ="claimNo";
}else {
myLogger.error(String.format(String.format("Not defefine Object:[%s]", o.getClass().getName())));
return null;
}
return getLock(objects, filedName);
}
public static List<RedisLock> getLockByString(final List<String> keys) {
boolean isAllLock = true;
List<RedisLock> locks = new ArrayList<RedisLock>();
if (keys != null) {
for (String key : keys) {
RedisLock lock = getLock(key, 900);
if (lock != null) {
locks.add(lock);
} else {
isAllLock = false;
break;
}
}
}
if (!isAllLock) {
for (RedisLock l : locks) {
l.unlock();
}
locks = null;
}
return locks;
}
public static void unLock(RedisLock lock) {
if (lock != null) {
lock.unlock();
}
}
public static void unLock(List<RedisLock> lockList) {
if (lockList != null) {
for (RedisLock lock : lockList) {
unLock(lock);
}
}
}
}
版权声明:本文为weixin_41500775原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。