實現訂單超時取消(多種方案)


1、Redis消息隊列

 因我們業務實現訂單失效時間是可配置的,所有Rdis數據類型使用ZSet;

    ZSet命令參考:https://www.cnblogs.com/yyhhblog/p/15393992.html
 參考:https://www.cnblogs.com/kinglf/p/5972300.html

2、線程處理

  

package com.mw.web.thread;

import com.mw.web.common.utils.IdWorker;
import com.mw.web.common.utils.SpringContextUtil;
import com.mw.web.common.utils.StringUtils;
import com.mw.web.common.utils.constants.RedisKeys;
import com.mw.web.common.utils.redis.RedisUtils;
import com.mw.web.service.WeChat.WechatOrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Set;

/**
 * @description: TODO 訂單待支付倒計時
 * @author yaohui
 * @date 2021/10/11 18:19
 * @version 1.0
 */
public class MyOrder extends HttpServlet{

    private static final long serialVersionUID = 1L;
    private MyThread1 myThread1;
    private MyThread2 myThread2;

    public MyOrder(){
    }

    @Override
    public void init(){
        String str = null;
        if (str == null && myThread1 == null) {
            myThread1 = new MyThread1();
            myThread1.start(); // servlet 上下文初始化時啟動 socket
        }
        if (str == null && myThread2 == null) {
            myThread2 = new MyThread2();
            myThread2.start(); // servlet 上下文初始化時啟動 socket
        }
    }

    @Override
    public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
            throws ServletException, IOException{
    }

    public void destory(){
        if (myThread1 != null && myThread1.isInterrupted()) {
            myThread1.interrupt();
        }

        if (myThread2 != null && myThread2.isInterrupted()) {
            myThread2.interrupt();
        }
    }
}

/**
 * 自定義一個 Class 線程類繼承自線程類,重寫 run() 方法,用於從后台獲取並處理數據
 *
 * @author Champion.Wong
 *
 */
class MyThread1 extends Thread {
    protected final static Logger log = Logger.getLogger(String.class);
    @Override
    public void run() {
        while (!this.isInterrupted()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /* ------------------ 開始執行 --------------------------- */
            System.out.println("線程1");
        }
    }
}

/**
 * 自定義一個 Class 線程類繼承自線程類,重寫 run() 方法,用於從后台獲取並處理數據
 *
 * @author Champion.Wong
 *
 */
class MyThread2 extends Thread {
    @Autowired
    private WechatOrderService wechatOrderService;
    protected final static Logger log = Logger.getLogger(String.class);

    @Override
    public void run() {
        this.wechatOrderService = SpringContextUtil.getBean(WechatOrderService.class);
        while (!this.isInterrupted()) {// 線程未中斷執行循環
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            /* ------------------ 開始執行 --------------------------- */
            System.out.println("線程2");
        }
    }
}

 

 3、redisTemplate

 參考:https://www.cnblogs.com/yyhhblog/p/15420369.html

 

 以上是訂單相關整理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM