Commit 2128aee1 by huluobin

更新预约状态加锁

parent a8fa342f
package com.gogirl.infrastructure.util.lock;
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;
/**
*  * 分段锁,系统提供一定数量的原始锁,根据传入用户id值获取对应的锁并加锁  * 注意:要锁的用户id值如果发生改变,有可能导致锁无法成功释放!!!
*/
public class ScheduleServeIdLock {
private final static HashMap<Integer, ReentrantLock> lockMap = new HashMap<>();
private Integer segments = 1;// 默认分段数量
private ScheduleServeIdLock() {
init(null, false);
}
private ScheduleServeIdLock(Integer counts, boolean fair) {
init(counts, fair);
}
/*静态内部类实现单例*/
public static final ScheduleServeIdLock getInsatance() {
return SingletonHolder.instance;
}
private void init(Integer counts, boolean fair) {
if (counts != null) {
segments = counts;
}
for (int i = 0; i < segments; i++) {
lockMap.put(i, new ReentrantLock(fair));
}
}
public void lock(int key) {
ReentrantLock lock = lockMap.get(key % segments);
lock.lock();
}
public void unlock(int key) {
ReentrantLock lock = lockMap.get(key % segments);
lock.unlock();
}
@Override
public String toString() {
return "SegmentLock [segments=" + segments + ", lockMap=" + lockMap
+ "]";
}
/*静态内部类实现单例*/
private static class SingletonHolder {
private static final ScheduleServeIdLock instance = new ScheduleServeIdLock(null, true);
}
}
......@@ -22,6 +22,7 @@ import com.gogirl.infrastructure.common.exception.RRException;
import com.gogirl.infrastructure.mapper.product.purchase.PurchaseSkuMapper;
import com.gogirl.infrastructure.mapper.product.serve.BaseProduceMapper;
import com.gogirl.infrastructure.util.SessionUtils;
import com.gogirl.infrastructure.util.lock.ScheduleServeIdLock;
import com.gogirl.infrastructure.util.lock.ScheduledLock;
import com.gogirl.shared.order.serve.command.schedule.CancelScheduleCommand;
import com.gogirl.shared.order.serve.command.schedule.SubmitScheduleCommand;
......@@ -226,7 +227,13 @@ public class ScheduleManageController {
@RequestParam Integer scheduleServeId,
@RequestParam Integer status,
@RequestParam Integer forceLeisureConfig) {
scheduleManageService.updateScheduledServeStatus(scheduleServeId, status, forceLeisureConfig);
ScheduleServeIdLock lock = ScheduleServeIdLock.getInsatance();
try {
lock.lock(scheduleServeId);
scheduleManageService.updateScheduledServeStatus(scheduleServeId, status, forceLeisureConfig);
} finally {
lock.unlock(scheduleServeId);
}
return JsonResult.success();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment