Search in sources :

Example 6 with FlashActivity

use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method publishFlashItem.

@Override
public AppResult publishFlashItem(Long userId, Long activityId, FlashItemPublishCommand itemPublishCommand) {
    logger.info("itemPublish|发布秒杀品|{},{},{}", userId, activityId, JSON.toJSON(itemPublishCommand));
    if (userId == null || activityId == null || itemPublishCommand == null || !itemPublishCommand.validate()) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_CREATE);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock itemCreateLock = lockFactoryService.getDistributedLock(getItemCreateLockKey(userId));
    try {
        boolean isLockSuccess = itemCreateLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        FlashActivity flashActivity = flashActivityDomainService.getFlashActivity(activityId);
        if (flashActivity == null) {
            throw new BizException(ACTIVITY_NOT_FOUND);
        }
        FlashItem flashItem = toDomain(itemPublishCommand);
        flashItem.setActivityId(activityId);
        flashItem.setStockWarmUp(0);
        flashItemDomainService.publishFlashItem(flashItem);
        logger.info("itemPublish|秒杀品已发布");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("itemPublish|秒杀品发布失败|{}", userId, e);
        throw new BizException("秒杀品发布失败");
    } finally {
        itemCreateLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) BizException(com.actionworks.flashsale.app.exception.BizException) FlashItem(com.actionworks.flashsale.domain.model.entity.FlashItem) 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 7 with FlashActivity

use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.

the class FlashActivitiesCacheService method tryToUpdateActivitiesCacheByLock.

public FlashActivitiesCache tryToUpdateActivitiesCacheByLock(Integer pageNumber) {
    logger.info("activitiesCache|更新远程缓存|{}", pageNumber);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ACTIVITIES_CACHE_LOCK_KEY);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashActivitiesCache().tryLater();
        }
        PagesQueryCondition pagesQueryCondition = new PagesQueryCondition();
        PageResult<FlashActivity> flashActivityPageResult = flashActivityDomainService.getFlashActivities(pagesQueryCondition);
        FlashActivitiesCache flashActivitiesCache;
        if (flashActivityPageResult == null) {
            flashActivitiesCache = new FlashActivitiesCache().notExist();
        } else {
            flashActivitiesCache = new FlashActivitiesCache().setTotal(flashActivityPageResult.getTotal()).setFlashActivities(flashActivityPageResult.getData()).setVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildActivityCacheKey(pageNumber), JSON.toJSONString(flashActivitiesCache), FIVE_MINUTES);
        logger.info("activitiesCache|远程缓存已更新|{}", pageNumber);
        return flashActivitiesCache;
    } catch (InterruptedException e) {
        logger.error("activitiesCache|远程缓存更新失败", e);
        return new FlashActivitiesCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivitiesCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition)

Example 8 with FlashActivity

use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.

the class FlashActivityCacheService method tryToUpdateActivityCacheByLock.

public FlashActivityCache tryToUpdateActivityCacheByLock(Long activityId) {
    logger.info("activityCache|更新远程缓存|{}", activityId);
    DistributedLock lock = distributedLockFactoryService.getDistributedLock(UPDATE_ACTIVITY_CACHE_LOCK_KEY + activityId);
    try {
        boolean isLockSuccess = lock.tryLock(1, 5, TimeUnit.SECONDS);
        if (!isLockSuccess) {
            return new FlashActivityCache().tryLater();
        }
        FlashActivity flashActivity = flashActivityDomainService.getFlashActivity(activityId);
        FlashActivityCache flashActivityCache;
        if (flashActivity == null) {
            flashActivityCache = new FlashActivityCache().notExist();
        } else {
            flashActivityCache = new FlashActivityCache().with(flashActivity).withVersion(System.currentTimeMillis());
        }
        distributedCacheService.put(buildActivityCacheKey(activityId), JSON.toJSONString(flashActivityCache), FIVE_MINUTES);
        logger.info("activityCache|远程缓存已更新|{}", activityId);
        return flashActivityCache;
    } catch (InterruptedException e) {
        logger.error("activityCache|远程缓存更新失败|{}", activityId);
        return new FlashActivityCache().tryLater();
    } finally {
        lock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) FlashActivityCache(com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)

Example 9 with FlashActivity

use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.

the class FlashActivityDomainServiceImpl method getFlashActivities.

@Override
public PageResult<FlashActivity> getFlashActivities(PagesQueryCondition pagesQueryCondition) {
    if (pagesQueryCondition == null) {
        pagesQueryCondition = new PagesQueryCondition();
    }
    List<FlashActivity> flashActivities = flashActivityRepository.findFlashActivitiesByCondition(pagesQueryCondition.buildParams());
    Integer total = flashActivityRepository.countFlashActivitiesByCondition(pagesQueryCondition);
    return PageResult.with(flashActivities, total);
}
Also used : FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) PagesQueryCondition(com.actionworks.flashsale.domain.model.PagesQueryCondition)

Example 10 with FlashActivity

use of com.actionworks.flashsale.domain.model.entity.FlashActivity in project flash-sale by ThoughtsBeta.

the class FlashActivityBuilder method toDomainObject.

public static FlashActivity toDomainObject(FlashActivityDO flashActivityDO) {
    FlashActivity flashActivity = new FlashActivity();
    BeanUtils.copyProperties(flashActivityDO, flashActivity);
    return flashActivity;
}
Also used : FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity)

Aggregations

FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)12 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)4 AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)2 BizException (com.actionworks.flashsale.app.exception.BizException)2 FlashActivitiesCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivitiesCache)2 FlashActivityCache (com.actionworks.flashsale.app.service.activity.cache.model.FlashActivityCache)2 AuthException (com.actionworks.flashsale.controller.exception.AuthException)2 FlashActivityEvent (com.actionworks.flashsale.domain.event.FlashActivityEvent)2 DomainException (com.actionworks.flashsale.domain.exception.DomainException)2 PagesQueryCondition (com.actionworks.flashsale.domain.model.PagesQueryCondition)2 FlashActivityDTO (com.actionworks.flashsale.app.model.dto.FlashActivityDTO)1 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)1 FlashActivityDO (com.actionworks.flashsale.persistence.model.FlashActivityDO)1