秒杀场景_多线程异步抢单队列分析与实现_02
生活随笔
收集整理的這篇文章主要介紹了
秒杀场景_多线程异步抢单队列分析与实现_02
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 實體
- 2. Service改造
- 3. 啟動類
1. 實體
package com.gblfy.entity;import java.io.Serializable;/*** 用戶排隊搶單信息實體*/@Data public class SkillEntity implements Serializable {private Long productId;private String userId; }2. Service改造
SkillGoodService
package com.gblfy.service;import com.gblfy.dao.SkillOrderRepository; import com.gblfy.entity.SkillEntity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service;import javax.transaction.Transactional;@Service public class SkillGoodService {public static final String SKILL_GOODS_PHONE = "SKILL_GOODS_PHONE";public static final String SKILL_GOODS_LIST = "SKILL_GOODS_LIST";@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate SkillOrderRepository skillOrderRepository;@Autowiredprivate ProductService productService;@Autowiredprivate MutilThreadOrder mutilThreadOrder;@Transactionalpublic void add(Long productId, String userId) throws Exception {// 先封裝對象 并且放入redis 隊列SkillEntity skillEntity=new SkillEntity();skillEntity.setProductId(productId);skillEntity.setUserId(userId);redisTemplate.boundListOps(SKILL_GOODS_LIST).leftPush(skillEntity);mutilThreadOrder.createOrder();} }新增MutilThreadOrder
package com.gblfy.service;import com.gblfy.dao.SkillOrderRepository; import com.gblfy.entity.SkillEntity; import com.gblfy.entity.SkillGood; import com.gblfy.entity.SkillOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component;import java.util.Date; import java.util.concurrent.atomic.AtomicInteger;@Component public class MutilThreadOrder {@Autowiredprivate ProductService productService;@Autowiredprivate SkillOrderRepository skillOrderRepository;@Autowiredprivate RedisTemplate redisTemplate;@Asyncpublic void createOrder() throws Exception {System.out.println("開始異步搶單");SkillEntity skillEntity = (SkillEntity) redisTemplate.boundListOps(SkillGoodService.SKILL_GOODS_LIST).rightPop();if (skillEntity == null) {return;}Long productId = skillEntity.getProductId();String userId = skillEntity.getUserId();Thread.sleep(10000);SkillGood skillGood = productService.getGoodById(productId);if (skillGood == null) {throw new Exception("商品已經被搶光拉");}if (skillGood.getStockCount() > 0) {SkillOrder skillOrder = new SkillOrder();skillOrder.setMoney(skillGood.getCostPrice());skillOrder.setPayTime(new Date());skillOrder.setStatus("0");skillOrder.setUserId(userId);skillOrder.setCreateTime(new Date());skillOrder.setSkillId(productId);skillOrderRepository.save(skillOrder);skillGood.setStockCount(skillGood.getStockCount() - 1);redisTemplate.boundHashOps(SkillGoodService.SKILL_GOODS_PHONE).put(skillGood.getId(), skillGood);System.out.println("成功秒殺 剩余庫存:" + skillGood.getStockCount());}if (skillGood.getStockCount() <= 0) {System.out.println("庫存已經是負數了:" + skillGood.getStockCount());redisTemplate.boundHashOps(SkillGoodService.SKILL_GOODS_PHONE).delete(skillGood.getId());productService.update(skillGood);}System.out.println("結束異步搶單");} }3. 啟動類
package com.gblfy;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.web.client.RestTemplate;@SpringBootApplication @EnableDiscoveryClient @EnableAsync public class SkillServApplication {public static void main(String[] args) {SpringApplication.run(SkillServApplication.class, args);}@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);//采用普通的key 為 字符串template.setKeySerializer(new StringRedisSerializer());return template;}@Bean@LoadBalancedpublic RestTemplate create() {return new RestTemplate();} }總結
以上是生活随笔為你收集整理的秒杀场景_多线程异步抢单队列分析与实现_02的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Directory /opt/jfrog
- 下一篇: 解决ssh正常登录sftp不能登录的问题