Search in sources :

Example 16 with Seckill

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

the class SynchronizedLockStrategy method execute.

@Override
public void execute(SeckillMockRequestDTO requestDto) {
    int executeTime = requestDto.getCount();
    long seckillId = requestDto.getSeckillId();
    CountDownLatch countDownLatch = new CountDownLatch(executeTime);
    for (int i = 0; i < executeTime; i++) {
        int userId = i;
        taskExecutor.execute(() -> {
            // 此处已优化,锁单个商品而不是整个方法
            Object seckillMonitor = seckillIdList.get(seckillId);
            if (seckillMonitor == null) {
                Object value = new Object();
                seckillIdList.put(seckillId, value);
            }
            synchronized (seckillIdList.get(seckillId)) {
                Seckill seckill = seckillMapper.selectById(seckillId);
                if (seckill.getNumber() > 0) {
                    seckillMapper.reduceNumber(seckillId, new Date());
                    SuccessKilled record = new SuccessKilled();
                    record.setSeckillId(seckillId);
                    record.setUserPhone(String.valueOf(userId));
                    record.setStatus(1);
                    record.setCreateTime(new Date());
                    successKilledMapper.insert(record);
                } else {
                    if (!SeckillStatusConstant.END.equals(seckill.getStatus())) {
                        streamBridge.send(DEFAULT_BINDING_NAME, MessageBuilder.withPayload(SeckillMockResponseDTO.builder().seckillId(seckillId).status(true).note(SYCHRONIZED.getName()).build()).build());
                        Seckill sendTopicResult = new Seckill();
                        sendTopicResult.setSeckillId(seckillId);
                        sendTopicResult.setStatus(SeckillStatusConstant.END);
                        seckillMapper.updateById(sendTopicResult);
                    }
                    if (log.isDebugEnabled()) {
                        log.debug("库存不足,无法继续秒杀!");
                    }
                }
            }
            countDownLatch.countDown();
        });
    }
    // 等待线程执行完毕,阻塞当前进程
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        log.error(e.getMessage(), e);
        Thread.currentThread().interrupt();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Seckill(com.goodskill.entity.Seckill) SuccessKilled(com.goodskill.entity.SuccessKilled) Date(java.util.Date)

Example 17 with Seckill

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

the class RedisServiceTest method putSeckill.

@Test
void putSeckill() {
    long seckillId = 1001L;
    ValueOperations valueOperations = mock(ValueOperations.class);
    when(redisTemplate.opsForValue()).thenReturn(valueOperations);
    Seckill seckill = new Seckill();
    seckill.setSeckillId(seckillId);
    assertDoesNotThrow(() -> redisService.putSeckill(seckill));
}
Also used : Seckill(com.goodskill.entity.Seckill) ValueOperations(org.springframework.data.redis.core.ValueOperations) Test(org.junit.jupiter.api.Test)

Example 18 with Seckill

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

the class RedisServiceTest method init.

@BeforeAll
public static void init() {
    long seckillId = 1001L;
    Seckill seckill = new Seckill();
    seckill.setSeckillId(seckillId);
    bytes = ProtostuffIOUtil.toByteArray(seckill, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
}
Also used : Seckill(com.goodskill.entity.Seckill) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 19 with Seckill

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

the class RedisServiceTest method getSeckillSuccessCache.

@Test
void getSeckillSuccessCache() {
    long seckillId = 1001L;
    String key = "seckill:" + seckillId;
    ValueOperations valueOperations = mock(ValueOperations.class);
    when(redisTemplate.opsForValue()).thenReturn(valueOperations);
    when(valueOperations.get(key.getBytes())).thenReturn(null);
    when(seckillMapper.selectById(seckillId)).thenReturn(new Seckill());
    assertNotNull(redisService.getSeckill(seckillId));
}
Also used : Seckill(com.goodskill.entity.Seckill) ValueOperations(org.springframework.data.redis.core.ValueOperations) Test(org.junit.jupiter.api.Test)

Example 20 with Seckill

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

the class AlipayRunnerTest method tradePrecreate.

@Test
void tradePrecreate() {
    long seckillId = 1001L;
    Seckill seckill = new Seckill();
    seckill.setGoodsId(1);
    when(redisService.getSeckill(seckillId)).thenReturn(seckill);
    alipayRunner.setAlipayPublicKey("1");
    alipayRunner.setNotifyUrl("1");
    alipayRunner.setSignType("RSA2");
    assertTrue(StringUtils.isBlank(alipayRunner.tradePrecreate(seckillId)));
}
Also used : Seckill(com.goodskill.entity.Seckill) Test(org.junit.jupiter.api.Test)

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