创建订单使用多线程处理
创建订单使用多线程异步处理,快速响应创单请求
订单创建结果放入Redis里面就结束,获取订单结果走新接口从Redis里面取
/**
* 订单创建
*/
@Component
@ConfigurationProperties(prefix = "spring.create-order.thread-pool")
public class CreateOrder implements Serializable, InitializingBean {
@Autowired
private RedisQueue redisQueue;
/**
* 线程池
*/
private ThreadPoolTaskExecutor taskExecutor;
/**
* 队列容量
*/
private Integer queueCapacity = 100;
/**
* 最大线程数
*/
private Integer maxPoolSize = 100;
/**
* 核心线程数
*/
private Integer corePoolSize = 5;
public Integer getQueueCapacity() {
return queueCapacity;
}
public void setQueueCapacity(Integer queueCapacity) {
this.queueCapacity = queueCapacity;
}
public Integer getMaxPoolSize() {
return maxPoolSize;
}
public void setMaxPoolSize(Integer maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public Integer getCorePoolSize() {
return corePoolSize;
}
public void setCorePoolSize(Integer corePoolSize) {
this.corePoolSize = corePoolSize;
}
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private MerchantGoodsMappingRepository merchantGoodsMappingRepository;
@Override
public void afterPropertiesSet() throws Exception {
taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(corePoolSize); //核心线程数
taskExecutor.setMaxPoolSize(maxPoolSize); //最大线程数
taskExecutor.setThreadNamePrefix("createOrder"); //线程名称
taskExecutor.setQueueCapacity(queueCapacity); //队列长度
taskExecutor.initialize();
logger.error("初始化线程池corePoolSize=" + corePoolSize + ":maxPoolSize" + maxPoolSize + ":queueCapacity" + queueCapacity);
taskExecutor.setRejectedExecutionHandler((r, executor) -> {
});
}
/**
* 添加任务
*
* @return
*/
public Result addTask(PreOrderBusinessOrderRequest request,
Function<CreateOrderReq, Result> doCreateOrder) {
//不要重复排队
//生成请求时间戳
long currentNonoTime = System.nanoTime();
Result result = Result.error(TxErrorCode.PENDING.getMsg());
try {
taskExecutor.execute(() -> {
// 调用下单接口
MerchantGoodsMapping mapping = merchantGoodsMappingRepository.getByGoodsType(request.getMerchantGoodsType());
Result<Object> createOrderResult = null;
String idStr = "" + currentNonoTime + SPLIT + request.getUserId();
try {
createOrderResult = doCreateOrder.apply(new CreateOrderReq(request, mapping));
} catch (Exception e) {
logger.error("订单创建异常:doCreateOrder", e);
}
redisQueue.putCreateOrderResult(createOrderResult, request.getGoodsNo(), idStr);
});
} catch (RejectedExecutionException e) {
logger.error("入队失败--队列已满:RejectedExecutionException", e);
// 超过容量,直接拒绝
Result error = Result.error(TxErrorCode.REJECT.getMsg());
error.setCode(TxErrorCode.REJECT.getCode());
return error;
}
result.setCode(TxErrorCode.PENDING.getCode());
QueuePositionInfo queuePositionInfo = new QueuePositionInfo(0, currentNonoTime);
result.setResponse(queuePositionInfo);
return result;
}
/**
* 订单创建请求
*/
public static class CreateOrderReq {
PreOrderBusinessOrderRequest request;
MerchantGoodsMapping mapping;
public PreOrderBusinessOrderRequest getRequest() {
return request;
}
public MerchantGoodsMapping getMapping() {
return mapping;
}
public CreateOrderReq(PreOrderBusinessOrderRequest request, MerchantGoodsMapping mapping) {
this.request = request;
this.mapping = mapping;
}
}
}
作者:隔壁老郭
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
Java入门到入坟
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!