Java implementation of mall order timeout cancellation function

Time:2021-7-28

Most B2C mall projects have time limited activities. When users place an order, there will be a payment timeout. When the order times out, the status of the order will automatically become cancelled. There are many ways to realize this function. The implementation method in this paper is suitable for most small malls.

Implementation principle:

It is implemented by using the blocking queue characteristics of delayqueue of JDK. When the project is started, a thread is started to process the timeout order object popped up in the delayqueue queue. The order does not timeout, and the thread is waiting.

A brief introduction to delayqueue:

The main function of delayqueue class: it is an unbounded BlockingQueue, which is used to place objects that implement the delayed interface. The objects can only be removed from the queue when they expire. This queue is ordered, that is, the delay expiration time of the queue head object is the longest. Note: null elements cannot be placed in this queue.

Implementation method:

1. Create an order class that implements the delayed interface and override the CompareTo and getdelay methods

2. Create a help class ordercollection (add / delete query of orders)

3. Create cancelorder class

When generating an order, the creation time and expiration time of the order number are encapsulated into an object implementing the delayed interface and stored in the delayqueue queue. After the payment of the order is completed, the object is removed from the queue. (in order to ensure that the order is not lost, it is recommended to initialize the qualified orders in the database to the delayqueue queue when the project is started.)

The implementation code is as follows:

/**
 *Class description
 *
 * @author grl
 *@ date new on December 16, 2019
 */
public class Order implements Delayed {
  private String orderShopNum;
  /**
  *1 - ordinary activities 2 - time limited activities 3 - shopping activities
  */
  private int orderType;
  private long orderCreateTime;
  private long expTime;
  public Order(String orderShopNum, int orderType, Date createTime) {
   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();
   }
  }
  public String getOrderShopNum() {
   return orderShopNum;
  }
  public long getOrderCreateTime() {
   return orderCreateTime;
  }
  public long getExpTime() {
   return expTime;
  }
  public int getOrderType() {
   return orderType;
  }
  @Override
  public int compareTo(Delayed o) {
   return Long.valueOf(this.expTime).compareTo(Long.valueOf(((Order) o).expTime));
  }
  @Override
  public long getDelay(TimeUnit unit) {
   return unit.convert(this.expTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
  }
 
}
 
/**
 *Class description
 *
 * @author grl
 *@ date new on December 16, 2019
 */
public class OrderCollection {
  /**
  *Order management set
  */
  private static DelayQueue<Order> orderList = new DelayQueue<Order>();
  private OrderCollection() {
  }
  /**
  *Get order collection
  * @author grl
  * @return
  */
  protected static DelayQueue getOrderCollection() {
   return orderList;
  } 
  /**
  *Add order to collection
  * 
  * @author grl
  * @param order
  * @return
  */
  public static boolean add(Order order) {
   boolean flag = false;
   if (order != null && StringUtils.isNotBlank(order.getOrderShopNum())) {
     flag = orderList.offer(order);
   }
   return flag;
  }
  /**
  *Delete order from collection
  * 
  * @author grl
  * @param orderShopNum
  * @return
  */
  public static boolean remove(String orderShopNum) {
   boolean flag = false;
   Order thisOrder = null;
   if (StringUtils.isNotBlank(orderShopNum)) {
     orderShopNum = orderShopNum.trim();
     for (Order order : orderList) {
      String orderNum = order.getOrderShopNum();
      if (orderNum.equals(orderShopNum)) {
        thisOrder = order;
      }
     }
     if (thisOrder != null) {
      flag = orderList.remove(thisOrder);
     }
   }
   return flag;
  }
  /**
  *Get the remaining expiration time of the order
  * 
  * @author grl
  * @param orderShopNum
  * @param unit
  *@ return - 1 has expired
  */
  public static long getDelay(String orderShopNum) {
   long time = -1;
   if (StringUtils.isNotBlank(orderShopNum)) {
     orderShopNum = orderShopNum.trim();
     for (Order order : orderList) {
      String orderNum = order.getOrderShopNum();
      if (orderNum.equals(orderShopNum)) {
        time = order.getDelay(TimeUnit.MILLISECONDS);
        if(time<5000) {
         time = -1;
        }
      }
     }
   }
   return time;
  }
}
 

/**
 *Class description
 *
 * @author grl
 *@ date new on December 16, 2019
 */
@Component
public class CancellOrder implements Runnable {
  private static final Logger log = LoggerFactory.getLogger(CancellOrder.class);
  @Override
  public void run() {
   while (true) {
     try {
      Order take = OrderCollection.getOrderCollection().take();
      String orderShopNum = take.getOrderShopNum();
      int orderType = take.getOrderType();
      //Business logic operation
     } catch (InterruptedException e) {
      e.printStackTrace();
      Log. Error ("cancelorder delayqueue error {}", e.getmessage());
     }
   }
  }
}

summary

The above is the Java implementation of the mall order timeout cancellation function introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]