Search in sources :

Example 6 with DistributedLock

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();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) AppException(com.actionworks.flashsale.app.exception.AppException) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) BizException(com.actionworks.flashsale.app.exception.BizException) AppException(com.actionworks.flashsale.app.exception.AppException)

Example 7 with DistributedLock

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();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 8 with DistributedLock

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();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 9 with DistributedLock

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();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 10 with DistributedLock

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();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Aggregations

DistributedLock (com.actionworks.flashsale.lock.DistributedLock)17 BizException (com.actionworks.flashsale.app.exception.BizException)9 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)7 AuthException (com.actionworks.flashsale.controller.exception.AuthException)7 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)4 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)4 Bucket (com.actionworks.flashsale.domain.model.Bucket)2 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AppException (com.actionworks.flashsale.app.exception.AppException)1 StockBucketException (com.actionworks.flashsale.app.exception.StockBucketException)1 PlaceOrderResult (com.actionworks.flashsale.app.model.result.PlaceOrderResult)1 FlashActivitiesCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)1 FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)1 FlashItemCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemCache)1 FlashItemsCache (com.actionworks.flashsale.app.service.item.cache.model.FlashItemsCache)1 ItemStockCache (com.actionworks.flashsale.app.service.stock.model.ItemStockCache)1 TimeUnit (java.util.concurrent.TimeUnit)1 RLock (org.redisson.api.RLock)1 DefaultRedisScript (org.springframework.data.redis.core.script.DefaultRedisScript)1