Search in sources :

Example 6 with Seckill

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

the class BaseServiceConfigForTest method testSelect.

public Seckill testSelect() throws ExecutionException, InterruptedException {
    Seckill seckill = (Seckill) redisTemplate.opsForValue().get(1000);
    if (seckill == null) {
        Seckill value;
        if (signal.get()) {
            putIntoJvm();
            value = getFromJvm();
        } else {
            value = seckillMapper.selectById(1000);
            updateRedis(value);
        }
        if (atomicBoolean.get()) {
            System.out.println(Thread.currentThread().getName() + "已更新缓存:" + value.getNumber());
        }
        return value;
    } else {
        if (atomicBoolean.get()) {
            System.out.println(Thread.currentThread().getName() + "获取到缓存:" + seckill.getNumber());
        }
        return seckill;
    }
}
Also used : Seckill(com.goodskill.entity.Seckill)

Example 7 with Seckill

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

the class BaseServiceConfigForTest method getFromJvm.

private Seckill getFromJvm() throws InterruptedException, ExecutionException {
    BlockingQueue blockingQueue = taskQueue.get(Thread.currentThread());
    Seckill seckill = null;
    while (!blockingQueue.isEmpty()) {
        Object object = blockingQueue.take();
        reentrantLock.lock();
        try {
            if (object instanceof Callable) {
                FutureTask futureTask = new FutureTask((Callable) object);
                while (signal.get()) {
                    condition.await();
                }
                futureTask.run();
                seckill = (Seckill) futureTask.get();
            } else {
                FutureTask futureTask = new FutureTask((Runnable) object, 1);
                while (signal.get()) {
                    condition.await();
                }
                futureTask.run();
                updateCount.decrementAndGet();
            }
        } finally {
            reentrantLock.unlock();
        }
    }
    return seckill;
}
Also used : Seckill(com.goodskill.entity.Seckill)

Example 8 with Seckill

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

the class AlipayRunner method tradePrecreate.

public String tradePrecreate(long seckillId) {
    Seckill seckill = redisService.getSeckill(seckillId);
    Goods goods = goodsMapper.selectById(seckill.getGoodsId());
    // 需保证商户系统端不能重复,建议通过数据库sequence生成,
    String outTradeNo = "tradeprecreate" + System.currentTimeMillis() + (long) (Math.random() * 10000000L);
    // 1. 设置参数(全局只需设置一次)
    Factory.setOptions(getOptions());
    try {
        // 2. 发起API调用(以创建当面付收款二维码为例)
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate(goods.getName(), outTradeNo, String.valueOf(seckill.getPrice()));
        // 3. 处理响应或异常
        if (ResponseChecker.success(response)) {
            String finalQrcodeImagePath = String.format(qrcodeImagePath + "/qr-%s.png", response.outTradeNo);
            ZxingUtils.getQRCodeImge(response.qrCode, 256, finalQrcodeImagePath);
            log.info("支付宝当面扫调用成功!");
            return finalQrcodeImagePath.split("/")[finalQrcodeImagePath.split("/").length - 1];
        } else {
            log.error("调用失败,原因:" + response.msg + "," + response.subMsg);
        }
    } catch (Exception e) {
        log.error("调用遭遇异常,原因:" + e.getMessage());
    }
    return null;
}
Also used : AlipayTradePrecreateResponse(com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse) Goods(com.goodskill.entity.Goods) Seckill(com.goodskill.entity.Seckill)

Example 9 with Seckill

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

the class SeckillServiceImpl method reduceNumberInner.

@Transactional(rollbackFor = Exception.class)
@Override
public int reduceNumberInner(SuccessKilled successKilled) {
    successKilledMapper.insert(successKilled);
    Seckill wrapper = new Seckill();
    wrapper.setSeckillId(successKilled.getSeckillId());
    UpdateWrapper<Seckill> updateWrapper = new UpdateWrapper(wrapper);
    updateWrapper.gt("end_time", successKilled.getCreateTime());
    updateWrapper.lt("start_time", successKilled.getCreateTime());
    updateWrapper.gt("number", 0);
    updateWrapper.setSql("number = number - 1");
    int update = baseMapper.update(null, updateWrapper);
    if (update <= 0) {
        throw new SeckillCloseException("seckill is closed");
    } else {
        taskExecutor.execute(() -> streamBridge.send(DEFAULT_BINDING_NAME_MONGO_SAVE, MessageBuilder.withPayload(SeckillMockSaveVo.builder().seckillId(successKilled.getSeckillId()).userPhone(successKilled.getUserPhone()).note(REDIS_MONGO_REACTIVE.getName()).build()).build()));
        log.info("已发送");
        return update;
    }
}
Also used : SeckillCloseException(com.goodskill.common.exception.SeckillCloseException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) Seckill(com.goodskill.entity.Seckill) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with Seckill

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

the class SeckillServiceImpl method getInfoById.

@Override
public SeckillInfoDTO getInfoById(Serializable seckillId) {
    SeckillInfoDTO seckillInfoDTO = new SeckillInfoDTO();
    Seckill seckill = seckillService.getById(seckillId);
    Goods goods = goodsService.getById(seckill.getGoodsId());
    BeanUtils.copyProperties(seckill, seckillInfoDTO);
    seckillInfoDTO.setGoodsName(goods.getName());
    return seckillInfoDTO;
}
Also used : Goods(com.goodskill.entity.Goods) 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