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();
}
}
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));
}
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));
}
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));
}
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)));
}
Aggregations