日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java中怎么實(shí)現(xiàn)商城訂單超時(shí)取消功能

這篇文章將為大家詳細(xì)講解有關(guān)Java中怎么實(shí)現(xiàn)商城訂單超時(shí)取消功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、富寧網(wǎng)站維護(hù)、網(wǎng)站推廣。

實(shí)現(xiàn)原理:

利用 jdk 的 DelayQueue的阻塞隊(duì)列的特性實(shí)現(xiàn)。在項(xiàng)目啟動(dòng)時(shí)開啟一個(gè)線程處理 DelayQueue 隊(duì)列里彈出的超時(shí)訂單對(duì)象,訂單未超時(shí)該線程處于等待中。

DelayQueue的簡(jiǎn)單介紹:

DelayQueue類的主要作用:是一個(gè)無界的BlockingQueue,用于放置實(shí)現(xiàn)了Delayed接口的對(duì)象,其中的對(duì)象只能在其到期時(shí)才能從隊(duì)列中取走。這種隊(duì)列是有序的,即隊(duì)頭對(duì)象的延遲到期時(shí)間最長(zhǎng)。注意:不能將null元素放置到這種隊(duì)列中。

實(shí)現(xiàn)方式 :

1.創(chuàng)建一個(gè)實(shí)現(xiàn)Delayed接口的 order 類并重寫compareTo和 getDelay方法

2.創(chuàng)建一個(gè)幫助類OrderCollection(訂單的增刪查)

3. 創(chuàng)建CancellOrder類

在生成訂單時(shí)將訂單號(hào)創(chuàng)建時(shí)間和過期時(shí)間封裝成一個(gè)實(shí)現(xiàn)Delayed接口的對(duì)象存入DelayQueue隊(duì)列中,當(dāng)該訂單支付完成后將該對(duì)象從隊(duì)列中移除,(為了保證不丟失訂單建議在項(xiàng)目啟動(dòng)時(shí)將數(shù)據(jù)庫(kù)中的符合條件的訂單初始化到DelayQueue隊(duì)列中 )

實(shí)現(xiàn)代碼如下:

/** * 類說明 * * @author grl * @date 2019年12月16日 新建 */public class Order implements Delayed {  private String orderShopNum;  /**  * 1-普通活動(dòng) 2-限時(shí)活動(dòng) 3-拼購(gòu)活動(dòng)  */  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);  } } /** * 類說明 * * @author grl * @date 2019年12月16日 新建 */public class OrderCollection {  /**  * 訂單管理集合  */  private static DelayQueue orderList = new DelayQueue();  private OrderCollection() {  }  /**  * 獲取訂單集合  * @author grl  * @return  */  protected static DelayQueue getOrderCollection() {   return orderList;  }   /**  * 向集合中添加訂單  *   * @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;  }  /**  * 從集合中刪除訂單  *   * @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;  }  /**  * 獲取訂單的過期剩余時(shí)間  *   * @author grl  * @param orderShopNum  * @param unit  * @return -1 已經(jīng)過期  */  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;  }} /** * 類說明 * * @author grl * @date 2019年12月16日 新建 */@Componentpublic 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();      // 業(yè)務(wù)邏輯操作     } catch (InterruptedException e) {      e.printStackTrace();      log.error("CancellOrder DelayQueue 錯(cuò)誤 {}", e.getMessage());     }   }  }}

關(guān)于Java中怎么實(shí)現(xiàn)商城訂單超時(shí)取消功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。


當(dāng)前標(biāo)題:Java中怎么實(shí)現(xiàn)商城訂單超時(shí)取消功能
轉(zhuǎn)載來源:http://www.dlmjj.cn/article/ijepdi.html