日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

秒杀场景_多线程异步抢单队列分析与实现_02

發(fā)布時(shí)間:2024/9/27 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 秒杀场景_多线程异步抢单队列分析与实现_02 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

            • 1. 實(shí)體
            • 2. Service改造
            • 3. 啟動(dòng)類

1. 實(shí)體
package com.gblfy.entity;import java.io.Serializable;/*** 用戶排隊(duì)搶單信息實(shí)體*/@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 {// 先封裝對(duì)象 并且放入redis 隊(duì)列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("商品已經(jīng)被搶光拉");}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("成功秒殺 剩余庫(kù)存:" + skillGood.getStockCount());}if (skillGood.getStockCount() <= 0) {System.out.println("庫(kù)存已經(jīng)是負(fù)數(shù)了:" + skillGood.getStockCount());redisTemplate.boundHashOps(SkillGoodService.SKILL_GOODS_PHONE).delete(skillGood.getId());productService.update(skillGood);}System.out.println("結(jié)束異步搶單");} }
3. 啟動(dòng)類
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();} }

總結(jié)

以上是生活随笔為你收集整理的秒杀场景_多线程异步抢单队列分析与实现_02的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。