一张思维导图带你学会使用SpringBoot异步任务实现下单校验库存

一张思维导图带你学会使用SpringBoot异步任务实现下单校验库存

🧑‍💻作者名称:DaenCode
🎤作者简介:啥技术都喜欢捣鼓捣鼓,喜欢分享技术、经验、生活。
😎人生感悟:尝尽人生百味,方知世间冷暖。
📖所属专栏:SpringBoot实战

系列文章目录

标题
一文带你学会使用SpringBoot+Avue实现短信通知功能(含重要文件代码)
一张思维导图带你学会Springboot创建全局异常、自定义异常
一张思维导图带你打通SpringBoot自定义拦截器的思路
28个SpringBoot项目中常用注解,日常开发、求职面试不再懵圈
一张思维导图带你学会SpringBoot、Vue前后端分离项目线上部署
一张思维导图带你学会使用SpringBoot中的Schedule定时发送邮件


🌟前言

在开发中,异步任务应用的场景非常的广泛,本文章以下单时校验库存为例来看看SpringBoot中异步任务的使用。使用异步任务可以提高系统的响应性能,提高系统的并发能力,改善用户体验,减少资源的浪费,提高系统的可扩展性。

🌟思维导图

🌟流程图

🌟必不可少的注解

@EnableAsync:用于标识启动类,开启异步任务。
@***ponent:标识异步任务类。
@Async:标识定时任务方法。

🌟获取异步结果的API

方法 描述
boolean cancel(boolean mayInterruptIfRunning) 取消异步任务的执行。
boolean isCancelled() 判断异步任务是否被取消。
boolean isDone() 判断异步任务是否已经完成。
V get() throws InterruptedException, ExecutionException 获取异步任务的结果。
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 在指定的时间内获取异步任务的结果。如果超时,会抛出TimeoutException

🌟实现步骤

数据准备

创建数据库表,并插入示例数据

CREATE TABLE product (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  price DECIMAL(10,2),
  stock INT
);
INSERT INTO product (id, name, price, stock) VALUES
  (1, '苹果13', 10.99, 50),
  (2, '小米10', 19.99, 100),
  (3, '华为mate20', 5.99, 20);

实体类

java">public class Product {
    private Integer id;
    private String name;
    private BigDecimal price;
    private Integer stock;
}

启动类开启异步任务

添加@EnableAsync开启异步任务。

@SpringBootApplication
@MapperScan("***.shoanjen.redis.mapper")
@EnableCaching
@EnableScheduling
@EnableAsync
public class RedisApplication {

	public static void main(String[] args) {
		SpringApplication.run(RedisApplication.class, args);
	}
}

异步任务方法

在异步任务方法上添加@Async注解。
方法逻辑

  • 根据商品id查询数据库是否存在当前商品
  • 若商品不存在、或者商品当前库存小于购买量quantity,返回false。
  • 若商品存在,进行模拟下单操作。更新库存的数量。
  • 当然还有查询余额的逻辑,我这里没有写,跟查询库存类似。

优点:通过异步任务实现下单时查询库存以及余额,可以很大的提高性能。

@***ponent
public class ValidateTask {
    @Autowired
    private ProductMapper productMapper;
    //校验库存功能
    @Async
    public Future<Boolean> validateStock(int productId, int quantity){
        //查询数据库中的商品
        Product product=productMapper.selectProductById(productId);
        //商品库存校验
        if (product==null || product.getStock()<quantity){
            return new AsyncResult<>(false);
        }else {
            //模拟下单操作
            product.setStock(product.getStock()-quantity);
            //这里就演示订单表保存数据了,直接更新库存
            productMapper.updateProduct(product);
            return new AsyncResult<>(true);
        }
    }
}

Controller层

方法逻辑

  • 调用异步任务。
  • 设置flag标记。
  • 判断异步任务是否完成。
  • 如果flag为true,则下单成功,否则失败。
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {
    @Autowired
    private ValidateTask validateTask;
	//productId-商品id,quantity-购买数量
    @RequestMapping("order")
    public JsonData order(@RequestParam int productId,@RequestParam int quantity) throws ExecutionException, InterruptedException {
    	//调用异步任务
        Future<Boolean> validateResult=validateTask.validateStock(productId,quantity);
        Boolean flag=false;
        //判断异步任务是否完成
        if (validateResult.isDone()){
            try {
                flag=validateResult.get();
            } catch (Exception e) {
                flag=false;
            }
        }
        if (flag){
            return JsonData.buildSu***ess("下单成功");
        }else {
            return JsonData.buildError("下单失败,库存不足");
        }
    }

ProductMapper.xml

<mapper namespace="***.shoanjen.redis.mapper.ProductMapper">


   <select id="selectProductById" resultType="***.shoanjen.redis.model.Product">
       select * from product where id=#{productId}
   </select>

    <update id="updateProduct" parameterType="***.shoanjen.redis.model.Product">
        update product set name=#{product.name},price=#{product.price},stock=#{product.stock} where id=#{product.id}
    </update>
</mapper>

效果测试

下单成功测试

库存不足测试

🌟写在最后

有关于SpringBoot异步任务实现下单校验库存的实战到此就结束了,希望此文能够帮助正在学习的你们。如有错误或者补充请在评论区留言,谢谢大家!


转载请说明出处内容投诉
CSS教程_站长资源网 » 一张思维导图带你学会使用SpringBoot异步任务实现下单校验库存

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买