Search in sources :

Example 11 with Seckill

use of com.goodskill.entity.Seckill in project goodsKill by techa03.

the class SeckillServiceImpl method exportSeckillUrl.

@Override
public ExposerDTO exportSeckillUrl(long seckillId) {
    // 从redis中获取缓存秒杀信息
    Seckill seckill = redisService.getSeckill(seckillId);
    Date startTime = seckill.getStartTime();
    Date endTime = seckill.getEndTime();
    Date nowTime = new Date();
    if (nowTime.getTime() < startTime.getTime() || nowTime.getTime() > endTime.getTime()) {
        return new ExposerDTO(false, seckillId, nowTime.getTime(), startTime.getTime(), endTime.getTime());
    }
    String md5 = MD5Util.getMD5(seckillId);
    return new ExposerDTO(true, md5, seckillId);
}
Also used : Seckill(com.goodskill.entity.Seckill) Date(java.util.Date)

Example 12 with Seckill

use of com.goodskill.entity.Seckill in project goodsKill by techa03.

the class SeckillServiceImpl method prepareSeckill.

@Override
public void prepareSeckill(Long seckillId, int seckillCount) {
    // 初始化库存数量
    Seckill entity = new Seckill();
    entity.setSeckillId(seckillId);
    entity.setNumber(seckillCount);
    entity.setStatus(SeckillStatusConstant.IN_PROGRESS);
    this.updateById(entity);
    // 清理已成功秒杀记录
    this.deleteSuccessKillRecord(seckillId);
    redisService.removeSeckill(seckillId);
    Seckill seckill = redisService.getSeckill(seckillId);
    redisTemplate.delete(seckillId);
    seckill.setStatus(SeckillStatusConstant.IN_PROGRESS);
    redisService.putSeckill(seckill);
    // 清理mongo表数据
    try {
        successKilledMongoService.deleteRecord(seckillId);
    } catch (Exception e) {
        log.error("mongo服务不可用请检查!", e);
        throw e;
    }
}
Also used : Seckill(com.goodskill.entity.Seckill) RepeatKillException(com.goodskill.common.exception.RepeatKillException) IOException(java.io.IOException) SeckillCloseException(com.goodskill.common.exception.SeckillCloseException) SeckillException(com.goodskill.common.exception.SeckillException)

Example 13 with Seckill

use of com.goodskill.entity.Seckill in project goodsKill by techa03.

the class SeckillProcedureExecutor method dealSeckill.

/**
 * 处理用户秒杀请求
 *
 * @param seckillId 秒杀活动id
 * @param userPhone
 * @param note      秒杀备注信息
 */
@Override
public void dealSeckill(long seckillId, String userPhone, String note) {
    try {
        InetAddress localHost = InetAddress.getLocalHost();
        SuccessKilled successKilled = new SuccessKilled();
        successKilled.setSeckillId(seckillId);
        successKilled.setUserPhone(userPhone);
        successKilled.setCreateTime(new Date());
        successKilled.setServerIp(localHost.getHostAddress() + ":" + localHost.getHostName());
        if (seckillService.reduceNumber(successKilled) < 1) {
            Seckill seckill = seckillMapper.selectById(seckillId);
            log.debug("当前库存:{}", seckill.getNumber());
            // 高并发时限制只能发一次秒杀完成通知
            if (!SeckillStatusConstant.END.equals(seckill.getStatus()) && sendTopicTimes.getAndIncrement() == 0) {
                streamBridge.send(DEFAULT_BINDING_NAME, MessageBuilder.withPayload(SeckillMockResponseDTO.builder().seckillId(seckillId).note(note).status(true).build()).build());
                Seckill sendTopicResult = new Seckill();
                sendTopicResult.setSeckillId(seckillId);
                sendTopicResult.setStatus(SeckillStatusConstant.END);
                seckillMapper.updateById(sendTopicResult);
                // 重置发送成功通知次数
                sendTopicTimes.set(0);
            }
            if (seckill.getNumber() <= 0) {
                log.debug("库存不足,无法继续秒杀!");
            }
        }
    } catch (UnknownHostException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) SuccessKilled(com.goodskill.entity.SuccessKilled) Seckill(com.goodskill.entity.Seckill) InetAddress(java.net.InetAddress) Date(java.util.Date)

Example 14 with Seckill

use of com.goodskill.entity.Seckill in project goodsKill by techa03.

the class SeckillServiceImpl method getSeckillList.

@Override
public Page<Seckill> getSeckillList(int pageNum, int pageSize, String goodsName) {
    String key = "seckill:list:" + pageNum + ":" + pageSize + ":" + goodsName;
    ValueOperations valueOperations = redisTemplate.opsForValue();
    Page pageCache = (Page) valueOperations.get(key);
    Page<Seckill> page;
    if (pageCache == null) {
        QueryWrapper<Seckill> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(goodsName)) {
            queryWrapper.lambda().like(Seckill::getName, goodsName);
        }
        page = this.page(new Page(pageNum, pageSize), queryWrapper);
        valueOperations.set(key, page, 5, TimeUnit.MINUTES);
    } else {
        page = pageCache;
    }
    return page;
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) Seckill(com.goodskill.entity.Seckill) ValueOperations(org.springframework.data.redis.core.ValueOperations)

Example 15 with Seckill

use of com.goodskill.entity.Seckill in project goodsKill by techa03.

the class RedisMongoReactiveStrategy method execute.

@Override
public void execute(SeckillMockRequestDTO requestDto) {
    long seckillId = requestDto.getSeckillId();
    Seckill seckill = redisService.getSeckill(seckillId);
    if (redisTemplate.opsForValue().increment(seckillId) <= seckill.getNumber()) {
        taskExecutor.execute(() -> streamBridge.send(DEFAULT_BINDING_NAME_MONGO_SAVE, MessageBuilder.withPayload(SeckillMockSaveVo.builder().seckillId(seckillId).userPhone(requestDto.getPhoneNumber()).note(REDIS_MONGO_REACTIVE.getName()).build()).build()));
        log.debug("已发送");
    } else {
        synchronized (this) {
            seckill = redisService.getSeckill(seckillId);
            if (!SeckillStatusConstant.END.equals(seckill.getStatus())) {
                log.info("秒杀商品暂无库存,发送活动结束消息!");
                streamBridge.send(DEFAULT_BINDING_NAME, MessageBuilder.withPayload(SeckillMockResponseDTO.builder().status(true).seckillId(seckillId).note(REDIS_MONGO_REACTIVE.getName()).build()).build());
                Seckill sendTopicResult = new Seckill();
                sendTopicResult.setSeckillId(seckillId);
                sendTopicResult.setStatus(SeckillStatusConstant.END);
                extSeckillMapper.updateById(sendTopicResult);
                seckill.setStatus(SeckillStatusConstant.END);
                redisService.putSeckill(seckill);
            }
        }
    }
}
Also used : Seckill(com.goodskill.entity.Seckill)

Aggregations

Seckill (com.goodskill.entity.Seckill)26 Test (org.junit.jupiter.api.Test)10 Date (java.util.Date)5 Goods (com.goodskill.entity.Goods)4 SeckillMockRequestDTO (com.goodskill.api.dto.SeckillMockRequestDTO)3 ValueOperations (org.springframework.data.redis.core.ValueOperations)3 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)2 SeckillCloseException (com.goodskill.common.exception.SeckillCloseException)2 SuccessKilled (com.goodskill.entity.SuccessKilled)2 AlipayTradePrecreateResponse (com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse)1 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)1 RepeatKillException (com.goodskill.common.exception.RepeatKillException)1 SeckillException (com.goodskill.common.exception.SeckillException)1 GlobalTransactional (io.seata.spring.annotation.GlobalTransactional)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1