Search in sources :

Example 1 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SynchronizedLockStrategyTest method executeNumberGt0.

@Test
public void executeNumberGt0() {
    SeckillMockRequestDTO requestDto = new SeckillMockRequestDTO();
    long seckillId = 1L;
    requestDto.setSeckillId(seckillId);
    requestDto.setCount(1);
    requestDto.setPhoneNumber("1");
    Seckill seckill = new Seckill();
    seckill.setNumber(1);
    when(seckillMapper.selectById(seckillId)).thenReturn(seckill);
    synchronizedLockStrategy.execute(requestDto);
    Seckill sendTopicResult = new Seckill();
    sendTopicResult.setSeckillId(seckillId);
    sendTopicResult.setStatus(SeckillStatusConstant.END);
    verify(seckillMapper, never()).updateById(sendTopicResult);
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) Seckill(com.goodskill.entity.Seckill) Test(org.junit.jupiter.api.Test)

Example 2 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SeckillMockController method doWithZookeeperLock.

/**
 */
@ApiOperation(value = "秒杀场景七(zookeeper分布式锁)")
@RequestMapping(value = "/zookeeperLock", method = POST, produces = { "application/json;charset=UTF-8" })
public Result doWithZookeeperLock(@RequestBody @Valid SeckillWebMockRequestDTO dto) {
    long seckillId = dto.getSeckillId();
    int seckillCount = dto.getSeckillCount();
    int requestCount = dto.getRequestCount();
    prepareSeckill(seckillId, seckillCount, ZOOKEEPER_LOCK.getName());
    log.info(ZOOKEEPER_LOCK.getName() + "开始时间:{},秒杀id:{}", new Date(), seckillId);
    AtomicInteger atomicInteger = new AtomicInteger(0);
    for (int i = 0; i < requestCount; i++) {
        taskExecutor.execute(() -> seckillService.execute(new SeckillMockRequestDTO(seckillId, 1, String.valueOf(atomicInteger.addAndGet(1))), ZOOKEEPER_LOCK.getCode()));
    }
    return Result.ok();
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SeckillMockController method redisReactiveMongo.

/**
 * 场景八:使用redis缓存执行库存-1操作,最后通过发送MQ完成数据落地(存入mongoDB)
 */
@ApiOperation(value = "秒杀场景八(秒杀商品存放redis减库存,异步发送秒杀成功MQ,mongoDb数据落地)")
@RequestMapping(value = "/redisReactiveMongo", method = POST, produces = { "application/json;charset=UTF-8" })
public Result redisReactiveMongo(@RequestBody @Valid SeckillWebMockRequestDTO dto) {
    long seckillId = dto.getSeckillId();
    int seckillCount = dto.getSeckillCount();
    int requestCount = dto.getRequestCount();
    prepareSeckill(seckillId, seckillCount, REDIS_MONGO_REACTIVE.getName());
    log.info(REDIS_MONGO_REACTIVE.getName() + "开始时间:{},秒杀id:{}", new Date(), seckillId);
    Seckill seckill = new Seckill();
    seckill.setSeckillId(seckillId);
    AtomicInteger atomicInteger = new AtomicInteger(0);
    for (int i = 0; i < requestCount; i++) {
        taskExecutor.execute(() -> seckillService.execute(new SeckillMockRequestDTO(seckillId, requestCount, String.valueOf(atomicInteger.addAndGet(1))), REDIS_MONGO_REACTIVE.getCode()));
    }
    return Result.ok();
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Seckill(com.goodskill.entity.Seckill) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Example 4 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SeckillMockController method doWithProcedure.

/**
 * 通过同步锁控制秒杀并发,秒杀过程使用存储过程(秒杀未完成阻塞主线程)
 * 场景五:初始化当前库存为1000,通过线程池调度,模拟总共有2000人参与秒杀,期望值为最后成功笔数为1000
 * 结果:多次运行,最终的结果为1000
 * 总结:速度快
 */
@ApiOperation(value = "秒杀场景五(数据库原子性更新update set num = num -1)")
@PostMapping("/procedure")
public Result doWithProcedure(@RequestBody @Valid SeckillWebMockRequestDTO dto) {
    long seckillId = dto.getSeckillId();
    int seckillCount = dto.getSeckillCount();
    int requestCount = dto.getRequestCount();
    prepareSeckill(seckillId, seckillCount, ATOMIC_UPDATE.getName());
    log.info(ATOMIC_UPDATE.getName() + "开始时间:{},秒杀id:{}", new Date(), seckillId);
    AtomicInteger atomicInteger = new AtomicInteger(0);
    for (int i = 0; i < requestCount; i++) {
        taskExecutor.execute(() -> seckillService.execute(new SeckillMockRequestDTO(seckillId, 1, String.valueOf(atomicInteger.addAndGet(1))), ATOMIC_UPDATE.getCode()));
    }
    return Result.ok();
// 待mq监听器处理完成打印日志,不在此处打印日志
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with SeckillMockRequestDTO

use of com.goodskill.api.dto.SeckillMockRequestDTO in project goodsKill by techa03.

the class SeckillMockController method doWithRabbitmq.

@ApiOperation(value = "秒杀场景九(rabbitmq)")
@PostMapping("/rabbitmq")
public Result doWithRabbitmq(@RequestBody @Valid SeckillWebMockRequestDTO dto) {
    long seckillId = dto.getSeckillId();
    int seckillCount = dto.getSeckillCount();
    int requestCount = dto.getRequestCount();
    // 初始化库存数量
    prepareSeckill(seckillId, seckillCount, RABBIT_MQ.getName());
    log.info(RABBIT_MQ.getName() + "开始时间:{},秒杀id:{}", new Date(), seckillId);
    AtomicInteger atomicInteger = new AtomicInteger(0);
    for (int i = 0; i < requestCount; i++) {
        taskExecutor.execute(() -> {
            SeckillMockRequestDTO payload = new SeckillMockRequestDTO(seckillId, 1, String.valueOf(atomicInteger.addAndGet(1)));
            streamBridge.send("seckill-out-0", payload);
        });
    }
    return Result.ok();
// 待mq监听器处理完成打印日志,不在此处打印日志
}
Also used : SeckillMockRequestDTO(com.goodskill.api.dto.SeckillMockRequestDTO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Date(java.util.Date) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SeckillMockRequestDTO (com.goodskill.api.dto.SeckillMockRequestDTO)9 ApiOperation (io.swagger.annotations.ApiOperation)7 Date (java.util.Date)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Seckill (com.goodskill.entity.Seckill)3 Test (org.junit.jupiter.api.Test)2 Disabled (org.junit.jupiter.api.Disabled)1