(1)接口
1 12 public interface DataLock {13 14 /**15 * 从数据库中查询一定数量的OrderTask,并进行lock,lock到指定条数就返回,如果lock不到就返回空集合16 * @param modeSize 总模数17 * @param modeNo 当前锁定模数18 */19 public ListlockDatas(int modeSize, int modeNo);20 /**21 * 根据id锁定一条数据22 * @param id23 * @return true已锁定,false未锁定24 */25 public abstract boolean lockSingleData(long id);26 /**27 * 解锁指定数据28 */29 public int unLock(long id);30 31 /**32 * 解锁全部数据33 */34 public int unLockAll();35 36 }
(2)模板类
2 15 public abstract class DataLockImpl implements DataLock {16 @Resource17 protected OrderTaskDao orderTaskDao;18 @Resource19 protected RouteUtil routeUtil;20 21 protected int maxLockSize;22 23 protected int getSize;24 25 @Override26 public ListlockDatas(int modeSize, int modeNo) {27 List consumeInfoLocks = new ArrayList ();28 List consumeInfos = getData();29 for (OrderTask consumeInfo : consumeInfos) {30 String pin = consumeInfo.getPin();31 if (StringUtils.isEmpty(pin)) {32 continue;33 }34 if (modeNo != routeUtil.getMode(pin, modeSize)) {35 continue;36 }37 if (lockSingleData(consumeInfo.getId())) {38 consumeInfoLocks.add(consumeInfo);39 }40 if (consumeInfoLocks.size() >= maxLockSize) {41 break;42 }43 }44 return consumeInfoLocks;45 }46 @Override47 public abstract boolean lockSingleData(long id);48 49 public abstract List getData();50 51 public void setMaxLockSize(int maxLockSize) {52 this.maxLockSize = maxLockSize;53 }54 55 public void setGetSize(int getSize) {56 this.getSize = getSize;57 }58 59 }
(3)具体类
15 @Service(value = "addConsumeLock")16 public class AddConsumeLock extends DataLockImpl {17 18 @Override19 @Value("${addConsumeLock.getSize}")20 public void setGetSize(int getSize) {21 super.setGetSize(getSize);22 }23 24 @Override25 @Value("${addConsumeLock.maxLockSize}")26 public void setMaxLockSize(int maxLockSize) {27 super.setMaxLockSize(maxLockSize);28 }29 30 @Override31 public boolean lockSingleData(long id) {32 return orderTaskDao.updateStatus(id, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT) == 1;33 }34 35 @Override36 public int unLock(long id) {37 return orderTaskDao.updateStatus(id, OrderTask.SYN_WAIT, OrderTask.SYN_LOCK);38 }39 40 @Override41 public ListgetData() {42 List orderTasks = orderTaskDao.findOrderTasksByConsumeStatus(OrderTask.SYN_WAIT, getSize, false);43 return orderTasks;44 }45 46 @Override47 public int unLockAll() {48 return orderTaskDao.updateStatus(OrderTask.SYN_WAIT, OrderTask.SYN_LOCK);49 }50 }
1 package com.jd.consume.service.lock.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.stereotype.Service; 7 8 import com.jd.consume.domain.OrderTask; 9 10 /**11 * 计算等级数据锁定12 * @author guanpanpan13 *14 */15 @Service(value = "gradeLock")16 public class GradeLock extends DataLockImpl {17 @Override18 @Value("${gradeLock.getSize}")19 public void setGetSize(int getSize) {20 super.setGetSize(getSize);21 }22 23 @Override24 @Value("${gradeLock.maxLockSize}")25 public void setMaxLockSize(int maxLockSize) {26 super.setMaxLockSize(maxLockSize);27 }28 29 @Override30 public boolean lockSingleData(long id) {31 return orderTaskDao.updateGradeStatus(id, OrderTask.LEV_LOCK, OrderTask.LEV_WAIT) == 1;32 }33 34 @Override35 public int unLock(long id) {36 return orderTaskDao.updateGradeStatus(id, OrderTask.LEV_WAIT, OrderTask.LEV_LOCK);37 }38 39 @Override40 public ListgetData() {41 List consumeInfos = orderTaskDao.findOrderTasksByLevStatus(OrderTask.LEV_WAIT, getSize, false);42 return consumeInfos;43 }44 @Override45 public int unLockAll() {46 return orderTaskDao.updateGradeStatus(OrderTask.LEV_WAIT, OrderTask.LEV_LOCK);47 }48 }
注:本示例来源于consume-grade-sql