Java实现商城订单超时取消功能
大多数的B2C商城项目都会有限时活动,当用户下单后都会有支付超时时间,当订单超时后订单的状态就会自动变成已取消,这个功能的实现有很多种方法,本文的实现方法适合大多数比较小的商城使用。
实现原理:
利用jdk的DelayQueue的阻塞队列的特性实现。在项目启动时开启一个线程处理DelayQueue队列里弹出的超时订单对象,订单未超时该线程处于等待中。
DelayQueue的简单介绍:
DelayQueue类的主要作用:是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。这种队列是有序的,即队头对象的延迟到期时间最长。注意:不能将null元素放置到这种队列中。
实现方式:
1.创建一个实现Delayed接口的order类并重写compareTo和getDelay方法
2.创建一个帮助类OrderCollection(订单的增删查)
3.创建CancellOrder类
在生成订单时将订单号创建时间和过期时间封装成一个实现Delayed接口的对象存入DelayQueue队列中,当该订单支付完成后将该对象从队列中移除,(为了保证不丢失订单建议在项目启动时将数据库中的符合条件的订单初始化到DelayQueue队列中)
实现代码如下:
/**
*类说明
*
*@authorgrl
*@date2019年12月16日新建
*/
publicclassOrderimplementsDelayed{
privateStringorderShopNum;
/**
*1-普通活动2-限时活动3-拼购活动
*/
privateintorderType;
privatelongorderCreateTime;
privatelongexpTime;
publicOrder(StringorderShopNum,intorderType,DatecreateTime){
if(StringUtils.isNotBlank(orderShopNum)){
this.orderShopNum=orderShopNum.trim();
}
if(createTime==null){
this.orderCreateTime=System.currentTimeMillis();
}else{
this.orderCreateTime=createTime.getTime();
}
this.orderType=orderType;
if(orderType==2){
this.expTime=TimeUnit.MILLISECONDS.convert(Const.LIMIT_ACTIVITY_EXPIRATION_TIME,TimeUnit.MINUTES)
+createTime.getTime();
}if(orderType==3){
this.expTime=TimeUnit.MILLISECONDS.convert(Const.LIMIT_GROUP_BUY_EXPIRATION_TIME,TimeUnit.MINUTES)
+createTime.getTime();
}else{
this.expTime=TimeUnit.MILLISECONDS.convert(Const.ORDER_PAYMENT_DEADLINE,TimeUnit.DAYS)
+createTime.getTime();
}
}
publicStringgetOrderShopNum(){
returnorderShopNum;
}
publiclonggetOrderCreateTime(){
returnorderCreateTime;
}
publiclonggetExpTime(){
returnexpTime;
}
publicintgetOrderType(){
returnorderType;
}
@Override
publicintcompareTo(Delayedo){
returnLong.valueOf(this.expTime).compareTo(Long.valueOf(((Order)o).expTime));
}
@Override
publiclonggetDelay(TimeUnitunit){
returnunit.convert(this.expTime-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
}
}
/**
*类说明
*
*@authorgrl
*@date2019年12月16日新建
*/
publicclassOrderCollection{
/**
*订单管理集合
*/
privatestaticDelayQueueorderList=newDelayQueue();
privateOrderCollection(){
}
/**
*获取订单集合
*@authorgrl
*@return
*/
protectedstaticDelayQueuegetOrderCollection(){
returnorderList;
}
/**
*向集合中添加订单
*
*@authorgrl
*@paramorder
*@return
*/
publicstaticbooleanadd(Orderorder){
booleanflag=false;
if(order!=null&&StringUtils.isNotBlank(order.getOrderShopNum())){
flag=orderList.offer(order);
}
returnflag;
}
/**
*从集合中删除订单
*
*@authorgrl
*@paramorderShopNum
*@return
*/
publicstaticbooleanremove(StringorderShopNum){
booleanflag=false;
OrderthisOrder=null;
if(StringUtils.isNotBlank(orderShopNum)){
orderShopNum=orderShopNum.trim();
for(Orderorder:orderList){
StringorderNum=order.getOrderShopNum();
if(orderNum.equals(orderShopNum)){
thisOrder=order;
}
}
if(thisOrder!=null){
flag=orderList.remove(thisOrder);
}
}
returnflag;
}
/**
*获取订单的过期剩余时间
*
*@authorgrl
*@paramorderShopNum
*@paramunit
*@return-1已经过期
*/
publicstaticlonggetDelay(StringorderShopNum){
longtime=-1;
if(StringUtils.isNotBlank(orderShopNum)){
orderShopNum=orderShopNum.trim();
for(Orderorder:orderList){
StringorderNum=order.getOrderShopNum();
if(orderNum.equals(orderShopNum)){
time=order.getDelay(TimeUnit.MILLISECONDS);
if(time<5000){
time=-1;
}
}
}
}
returntime;
}
}
/**
*类说明
*
*@authorgrl
*@date2019年12月16日新建
*/
@Component
publicclassCancellOrderimplementsRunnable{
privatestaticfinalLoggerlog=LoggerFactory.getLogger(CancellOrder.class);
@Override
publicvoidrun(){
while(true){
try{
Ordertake=OrderCollection.getOrderCollection().take();
StringorderShopNum=take.getOrderShopNum();
intorderType=take.getOrderType();
//业务逻辑操作
}catch(InterruptedExceptione){
e.printStackTrace();
log.error("CancellOrderDelayQueue错误{}",e.getMessage());
}
}
}
}
总结
以上所述是小编给大家介绍的Java实现商城订单超时取消功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。