use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.
the class DefaultBucketsAPPService method arrangeStockBuckets.
@Override
public AppSimpleResult arrangeStockBuckets(Long userId, Long itemId, BucketsArrangementCommand arrangementCommand) {
logger.info("arrangeBuckets|编排库存分桶|{},{},{}", userId, itemId, JSON.toJSON(arrangementCommand));
String arrangementKey = getArrangementKey(userId, itemId);
DistributedLock arrangementLock = lockFactoryService.getDistributedLock(arrangementKey);
try {
boolean isLockSuccess = arrangementLock.tryLock(5, 5, TimeUnit.SECONDS);
if (!isLockSuccess) {
return AppSimpleResult.failed(FREQUENTLY_ERROR.getErrCode(), FREQUENTLY_ERROR.getErrDesc());
}
FlashItem flashItem = flashItemDomainService.getFlashItem(itemId);
if (flashItem == null) {
throw new BizException(ITEM_NOT_FOUND.getErrDesc());
}
bucketsArrangementService.arrangeStockBuckets(itemId, arrangementCommand.getTotalStocksAmount(), arrangementCommand.getBucketsQuantity(), arrangementCommand.getArrangementMode());
logger.info("arrangeBuckets|库存编排完成|{}", itemId);
return AppSimpleResult.ok(true);
} catch (AppException e) {
logger.error("arrangeBuckets|库存编排失败|{}", itemId, e);
return AppSimpleResult.failed(BUSINESS_ERROR.getErrCode(), e.getMessage());
} catch (Exception e) {
logger.error("arrangeBuckets|库存编排错误|{}", itemId, e);
return AppSimpleResult.failed(ARRANGE_STOCK_BUCKETS_FAILED);
} finally {
arrangementLock.unlock();
}
}
use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.
the class DefaultFlashItemAppService method onlineFlashItem.
@Override
public AppResult onlineFlashItem(Long userId, Long activityId, Long itemId) {
logger.info("itemOnline|上线秒杀品|{},{},{}", userId, activityId, itemId);
if (userId == null || activityId == null || itemId == null) {
throw new BizException(INVALID_PARAMS);
}
AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_MODIFICATION);
if (!authResult.isSuccess()) {
throw new AuthException(UNAUTHORIZED_ACCESS);
}
DistributedLock itemModificationLock = lockFactoryService.getDistributedLock(getItemModificationLockKey(userId));
try {
boolean isLockSuccess = itemModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
if (!isLockSuccess) {
throw new BizException(LOCK_FAILED_ERROR);
}
flashItemDomainService.onlineFlashItem(itemId);
logger.info("itemOnline|秒杀品已上线");
return AppResult.buildSuccess();
} catch (Exception e) {
logger.error("itemOnline|秒杀品已上线失败|{}", userId, e);
throw new BizException("秒杀品已上线失败");
} finally {
itemModificationLock.unlock();
}
}
use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.
the class DefaultFlashItemAppService method offlineFlashItem.
@Override
public AppResult offlineFlashItem(Long userId, Long activityId, Long itemId) {
logger.info("itemOffline|下线秒杀品|{},{},{}", userId, activityId, itemId);
AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_MODIFICATION);
if (!authResult.isSuccess()) {
throw new AuthException(UNAUTHORIZED_ACCESS);
}
if (userId == null || activityId == null || itemId == null) {
throw new BizException(INVALID_PARAMS);
}
DistributedLock itemModificationLock = lockFactoryService.getDistributedLock(getItemModificationLockKey(userId));
try {
boolean isLockSuccess = itemModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
if (!isLockSuccess) {
throw new BizException(LOCK_FAILED_ERROR);
}
flashItemDomainService.offlineFlashItem(itemId);
logger.info("itemOffline|秒杀品已下线");
return AppResult.buildSuccess();
} catch (Exception e) {
logger.error("itemOffline|秒杀品已下线失败|{}", userId, e);
throw new BizException("秒杀品已下线失败");
} finally {
itemModificationLock.unlock();
}
}
use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.
the class DefaultActivityAppService method publishFlashActivity.
@Override
public AppResult publishFlashActivity(Long userId, FlashActivityPublishCommand flashActivityPublishCommand) {
logger.info("activityPublish|发布秒杀活动|{},{}", userId, JSON.toJSONString(flashActivityPublishCommand));
if (userId == null || flashActivityPublishCommand == null || !flashActivityPublishCommand.validate()) {
throw new BizException(INVALID_PARAMS);
}
AuthResult authResult = authorizationService.auth(userId, FLASH_ACTIVITY_CREATE);
if (!authResult.isSuccess()) {
throw new AuthException(UNAUTHORIZED_ACCESS);
}
DistributedLock activityCreateLock = lockFactoryService.getDistributedLock(getActivityCreateLockKey(userId));
try {
boolean isLockSuccess = activityCreateLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
if (!isLockSuccess) {
throw new BizException(FREQUENTLY_ERROR);
}
flashActivityDomainService.publishActivity(userId, toDomain(flashActivityPublishCommand));
logger.info("activityPublish|活动已发布");
return AppResult.buildSuccess();
} catch (Exception e) {
logger.error("activityPublish|活动发布失败|{}", userId, e);
throw new BizException("活动发布失败");
} finally {
activityCreateLock.unlock();
}
}
use of com.actionworks.flashsale.lock.DistributedLock in project flash-sale by ThoughtsBeta.
the class DefaultActivityAppService method modifyFlashActivity.
@Override
public AppResult modifyFlashActivity(Long userId, Long activityId, FlashActivityPublishCommand flashActivityPublishCommand) {
logger.info("activityModification|秒杀活动修改|{},{},{}", userId, activityId, JSON.toJSONString(flashActivityPublishCommand));
if (userId == null || flashActivityPublishCommand == null || !flashActivityPublishCommand.validate()) {
throw new BizException(INVALID_PARAMS);
}
AuthResult authResult = authorizationService.auth(userId, FLASH_ACTIVITY_MODIFICATION);
if (!authResult.isSuccess()) {
throw new AuthException(UNAUTHORIZED_ACCESS);
}
DistributedLock activityModificationLock = lockFactoryService.getDistributedLock(getActivityModificationLockKey(activityId));
try {
boolean isLockSuccess = activityModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
if (!isLockSuccess) {
throw new BizException(FREQUENTLY_ERROR);
}
FlashActivity flashActivity = toDomain(flashActivityPublishCommand);
flashActivity.setId(activityId);
flashActivityDomainService.modifyActivity(userId, flashActivity);
logger.info("activityModification|活动已修改");
return AppResult.buildSuccess();
} catch (Exception e) {
logger.error("activityModification|活动修改失败|{},{}", userId, activityId, e);
throw new BizException("活动修改失败");
} finally {
activityModificationLock.unlock();
}
}
Aggregations