线程池回滚

This commit is contained in:
yovinchen 2024-05-10 01:30:10 +08:00
parent 423ae62e51
commit afed333294

View File

@ -28,7 +28,6 @@ import com.yovinchen.xlcs.order.service.OrderItemService;
import com.yovinchen.xlcs.vo.order.*;
import com.yovinchen.xlcs.vo.product.SkuStockLockVo;
import com.yovinchen.xlcs.vo.user.LeaderAddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
@ -40,7 +39,7 @@ import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@ -52,7 +51,6 @@ import java.util.stream.Collectors;
* @since 2023-10-12
*/
@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
@Autowired
private UserFeignClient userFeignClient;
@ -85,66 +83,29 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
*/
@Override
public OrderConfirmVo confirmOrder() {
// 创建一个线程池
ExecutorService executorService = Executors.newFixedThreadPool(4);
//获取用户id
Long userId = AuthContextHolder.getUserId();
// 创建一个ScheduledExecutorService用于处理超时
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
//获取用户对应配送员信息
LeaderAddressVo leaderAddressVo = userFeignClient.getUserAddressByUserId(userId);
// 使用CompletableFuture进行多线程异步处理并设置超时时间为1秒
CompletableFuture<LeaderAddressVo> leaderAddressFuture = CompletableFuture.supplyAsync(() -> {
// 获取用户id
Long userId = AuthContextHolder.getUserId();
// 获取用户对应配送员信息
return userFeignClient.getUserAddressByUserId(userId);
}, executorService);
//获取购物车里面选中的商品
List<CartInfo> cartInfoList = cartFeignClient.getCartCheckedList(userId);
CompletableFuture<List<CartInfo>> cartInfoFuture = CompletableFuture.supplyAsync(() -> {
// 获取用户id
Long userId = AuthContextHolder.getUserId();
// 获取购物车里面选中的商品
return cartFeignClient.getCartCheckedList(userId);
}, executorService);
// 使用ScheduledExecutorService设置超时
scheduler.schedule(() -> {
leaderAddressFuture.complete(null);
log.info("获取地址信息任务超时");
}, 1, TimeUnit.SECONDS);
scheduler.schedule(() -> {
cartInfoFuture.complete(null);
log.info("获取购物车选中商品任务超时");
}, 1, TimeUnit.SECONDS);
// 等待上述所有异步任务完成
CompletableFuture<Void> allFutures = CompletableFuture.allOf(leaderAddressFuture, cartInfoFuture);
try {
allFutures.get(); // 等待所有任务完成
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
executorService.shutdown(); // 关闭线程池
scheduler.shutdown(); // 关闭调度器
}
// 唯一标识订单
//唯一标识订单
String orderNo = System.currentTimeMillis() + "";
redisTemplate.opsForValue()
.set(RedisConst.ORDER_REPEAT + orderNo, orderNo, 24, TimeUnit.HOURS);
// 获取购物车满足条件活动和优惠卷信息
OrderConfirmVo orderConfirmVo = activityFeignClient.findCartActivityAndCoupon(cartInfoFuture.join(), AuthContextHolder.getUserId());
// 封装其他值
orderConfirmVo.setLeaderAddressVo(leaderAddressFuture.join());
//获取购物车满足条件活动和优惠卷信息
OrderConfirmVo orderConfirmVo = activityFeignClient.findCartActivityAndCoupon(cartInfoList, userId);
//封装其他值
orderConfirmVo.setLeaderAddressVo(leaderAddressVo);
orderConfirmVo.setOrderNo(orderNo);
return orderConfirmVo;
}
/**
* 生成订单
*
@ -410,14 +371,16 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
*/
@Override
public IPage<OrderInfo> getOrderInfoByUserIdPage(Page<OrderInfo> pageParam, OrderUserQueryVo orderUserQueryVo) {
IPage<OrderInfo> pageModel = baseMapper.selectPage(pageParam, new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getUserId, orderUserQueryVo.getUserId())
IPage<OrderInfo> pageModel = baseMapper.selectPage(pageParam, new LambdaQueryWrapper<OrderInfo>()
.eq(OrderInfo::getUserId, orderUserQueryVo.getUserId())
.eq(OrderInfo::getOrderStatus, orderUserQueryVo.getOrderStatus()));
//获取每个订单把每个订单里面订单项查询封装
List<OrderInfo> orderInfoList = pageModel.getRecords();
for (OrderInfo orderInfo : orderInfoList) {
//根据订单id查询里面所有订单项列表
List<OrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId, orderInfo.getId()));
List<OrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
.eq(OrderItem::getOrderId, orderInfo.getId()));
//把订单项集合封装到每个订单里面
orderInfo.setOrderItemList(orderItemList);
//封装订单状态名称
@ -464,7 +427,8 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
List<OrderInfo> orderInfoList = pageModel.getRecords();
for (OrderInfo orderInfo : orderInfoList) {
//根据订单id查询里面所有订单项列表
List<OrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId, orderInfo.getId()));
List<OrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
.eq(OrderItem::getOrderId, orderInfo.getId()));
//把订单项集合封装到每个订单里面
orderInfo.setOrderItemList(orderItemList);
//封装订单状态名称