欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java微信延遲支付的實(shí)現(xiàn)示例

 更新時(shí)間:2024年01月18日 09:28:22   作者:mob6454cc7c268c  
最近在面試的過(guò)程中總會(huì)出現(xiàn)一些關(guān)于微信支付延遲返回結(jié)果的處理方式的問(wèn)題,本文主要介紹了java微信延遲支付的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下

1.需求

下單支付后,支付回調(diào)因部分因素不可達(dá),導(dǎo)致訂單狀態(tài)與微信支付狀態(tài)不一致。此時(shí)需要服務(wù)端主動(dòng)查詢訂單支付狀態(tài),進(jìn)行更改訂單狀態(tài)。

2.實(shí)現(xiàn)方式

基于定時(shí)任務(wù)
每隔30秒啟動(dòng)一次,找出最近10分鐘內(nèi)創(chuàng)建并且未支付的訂單,調(diào)用微信查單接口核實(shí)訂單狀態(tài)。系統(tǒng)記錄訂單查詢的次數(shù),在n次查詢之后狀態(tài)還是未支付成功,則停止后續(xù)查詢,并調(diào)用關(guān)單接口關(guān)閉訂單。

基于延時(shí)隊(duì)列
每隔5秒/30秒/1分鐘/3分鐘/5分鐘/10分鐘/30分鐘調(diào)用查單接口,若查詢付款成功,不再執(zhí)行隊(duì)列任務(wù),最后一次查詢?nèi)暨€是未返回支付成功狀態(tài),則停止后續(xù)查詢,并調(diào)用《關(guān)單接口》關(guān)閉訂單。

延時(shí)隊(duì)列實(shí)現(xiàn)方式(這里不做詳細(xì)描述,優(yōu)缺點(diǎn)自行百度,本文采用redis實(shí)現(xiàn))

  • delayqueue
  • RabbitMQ
  • reids

3.具體代碼

新建實(shí)體類 DelayQueueJob 與 ScoredSortedItem

/**
 * @Desecription: 延遲任務(wù)
 * @Author: yangyu
 * @Date: 2021/9/10 14:35
 */
@Data
public class DelayQueueJob implements Serializable {

    /**
     * 延遲任務(wù)的唯一標(biāo)識(shí),用于檢索任務(wù)
     */
    private long jobId;

    /**
     * 任務(wù)的執(zhí)行時(shí)間段
     */
    private List<Long> delayTimeList;

    /**
     * 任務(wù)的執(zhí)行次數(shù)
     */
    private Integer exCount = 0;

    /**
     * 任務(wù)的執(zhí)行時(shí)間單位
     */
    private TimeUnit timeUnit = TimeUnit.SECONDS;

    /**
     * 任務(wù)的執(zhí)行超時(shí)時(shí)間
     */
    private long timeout;

    /**
     * 訂單編號(hào),根據(jù)編號(hào)查詢訂單
     */
    private String orderCode;

    /**
     * 任務(wù)類型(具體業(yè)務(wù)類型)
     */
    private Integer topic;

    /**
     * 任務(wù)狀態(tài) 0:執(zhí)行 1:結(jié)束
     */
    private int jobType = 0;

}
/**
 * @Desecription: 延時(shí)任務(wù) 桶
 * @Author: yangyu
 * @Date: 2021/9/10 14:55
 */
@Data
@AllArgsConstructor
public class ScoredSortedItem implements Serializable {

    /**
     * 延遲任務(wù)的唯一標(biāo)識(shí)
     */
    private long jobId;

    /**
     * 任務(wù)的執(zhí)行時(shí)間
     */
    private long delayTime;

}

定義常量

/**
 * @Desecription: 延時(shí)隊(duì)列常量
 * @Author: yangyu
 * @Date: 2021/9/10 14:44
 */
public class DelayQueueConstant {

    /*
     * 延時(shí)任務(wù)池
     * */
    public static final String DELAY_QUEUE_JOB_POOL = "delayQueue:delayQueueJobPool:";

    /*
     * 延時(shí)桶
     * */
    public static final String DELAY_BUCKET_KEY_PREFIX = "delayQueue:delayBucket:";

    /*
     * 任務(wù)的執(zhí)行時(shí)間段
     * */
    public static final List<Long> SLOT_DELAY_TIME = Arrays.asList(5L, 30L, 60L, 180L, 300L, 600L, 1800L);

    /*
     * 任務(wù)的執(zhí)行固定時(shí)間
     * */
    public static final List<Long> FIXED_DELAY_TIME = Arrays.asList(1800L);

}

zset有序隊(duì)列操作類

/**
 * @Desecription: 以時(shí)間為維度的有序隊(duì)列zset 操作類,參考redisson_delay_queue_timeout
 * @Author: yangyu
 * @Date: 2021/9/10 14:50
 */
@Component
public class DelayBucket {

    @Autowired
    private RedissonClient redissonClient;

    /**
     * @Desecription: 添加jobId到延遲任務(wù)桶中
     * @Param: key
     * @Param: jobId
     * @Param: delayTimeList
     * @Return:
     * @Author: yangyu
     * @Date: 2021/9/10 14:52
     */
    public void addToBucket(String key, Long jobId, List<Long> delayTimeList, TimeUnit timeUnit) {
        RScoredSortedSet<ScoredSortedItem> scoredSortedSet = redissonClient.getScoredSortedSet(DelayQueueConstant.DELAY_BUCKET_KEY_PREFIX + key);
        long millis = System.currentTimeMillis();
        for (int i = 0; i < delayTimeList.size(); i++) {
            ScoredSortedItem scoredSortedItem = new ScoredSortedItem(jobId, millis + timeUnit.toMillis(delayTimeList.get(i)));
            scoredSortedSet.add(scoredSortedItem.getDelayTime(), scoredSortedItem);
        }
    }

    /**
     * @Desecription: 從延遲任務(wù)桶中獲取延遲時(shí)間最小的 jodId
     * @Param: jobIdKey
     * @Return: ScoredSortedItem
     * @Author: yangyu
     * @Date: 2021/9/10 15:35
     */
    public ScoredSortedItem getFromBucket(String key) {
        RScoredSortedSet<ScoredSortedItem> scoredSortedSet = redissonClient.getScoredSortedSet(DelayQueueConstant.DELAY_BUCKET_KEY_PREFIX + key);
        if (scoredSortedSet.size() == 0) {
            return null;
        }
        return scoredSortedSet.first();
    }

    /**
     * @Desecription: 從延遲任務(wù)桶中刪除 jod
     * @Param: key
     * @Param scoredSortedItem
     * @Return:
     * @Author: yangyu
     * @Date: 2021/9/10 14:52
     */
    public void deleteFormBucket(String key, ScoredSortedItem scoredSortedItem) {
        RScoredSortedSet<ScoredSortedItem> scoredSortedSet = redissonClient.getScoredSortedSet(DelayQueueConstant.DELAY_BUCKET_KEY_PREFIX + key);
        scoredSortedSet.remove(scoredSortedItem);
    }
}

延時(shí)任務(wù)類

/**
 * @Desecription: 操作延時(shí)任務(wù)
 * @Author: yangyu
 * @Date: 2021/9/10 14:43
 */
@Component
public class DelayQueueJobPool {

    @Autowired
    private RedissonClient redissonClient;

    /**
     * @Desecription: 添加延時(shí)隊(duì)列job
     * @Param: delayQueueJob
     * @Author: yangyu
     * @Date: 2021/9/10 14:43
     */
    public void addDelayQueueJob(DelayQueueJob delayQueueJob, String key) {
        RMap<Long, DelayQueueJob> rMap = redissonClient.getMap(DelayQueueConstant.DELAY_QUEUE_JOB_POOL + key);
        rMap.put(delayQueueJob.getJobId(), delayQueueJob);
    }

    /**
     * @Desecription: 刪除延時(shí)隊(duì)列job
     * @Param: jobId
     * @Author: yangyu
     * @Date: 2021/9/10 14:46
     */
    public void deleteDelayQueueJob(Long jobId, String key) {
        RMap<Long, DelayQueueJob> rMap = redissonClient.getMap(DelayQueueConstant.DELAY_QUEUE_JOB_POOL + key);
        rMap.remove(jobId);
    }

    /**
     * @Desecription: 查詢延時(shí)隊(duì)列job
     * @Param: jobId
     * @Return:
     * @Author: yangyu
     * @Date: 2021/9/10 17:04
     */
    public DelayQueueJob getDelayQueueJob(Long jobId, String key) {
        RMap<Long, DelayQueueJob> rMap = redissonClient.getMap(DelayQueueConstant.DELAY_QUEUE_JOB_POOL + key);
        return rMap.get(jobId);
    }

    /**
     * @Desecription: 修改延時(shí)隊(duì)列job
     * @Param: jobId
     * @Author: yangyu
     * @Date: 2021/9/10 14:46
     */
    /**
     * @Desecription: 描述方法
     * @Param: jobId
     * @Param: jobId
     * @Return:
     * @Author: yangyu
     * @Date: 2021/9/13 11:09
     */
    public void updateDelayQueueJob(DelayQueueJob delayQueueJob, String key) {
        RMap<Long, DelayQueueJob> rMap = redissonClient.getMap(DelayQueueConstant.DELAY_QUEUE_JOB_POOL + key);
        rMap.replace(delayQueueJob.getJobId(), delayQueueJob);
    }
}

封裝api

@Log4j2
@Component
public class RedisDelayedQueue {

   @Autowired
   private DelayQueueJobPool delayQueueJobPool;

   @Autowired
   private DelayBucket delayBucket;

   /**
    * @Desecription: 添加延遲任務(wù)到延遲隊(duì)列
    * @Param:
    * @Author: yangyu
    * @Date: 2021/9/10 14:41
    */
   public void push(DelayQueueJob delayQueueJob, String simpleName) {
       delayQueueJobPool.addDelayQueueJob(delayQueueJob, simpleName);
       delayBucket.addToBucket(simpleName, delayQueueJob.getJobId(), delayQueueJob.getDelayTimeList(), delayQueueJob.getTimeUnit());
   }

}

隊(duì)列事件監(jiān)聽(tīng)接口

/**
 * @Desecription: 隊(duì)列事件監(jiān)聽(tīng)接口
 * @Author: yangyu
 * @Date: 2021/9/10 10:11
 */
public interface RedisDelayedQueueListenerService<T> {

    void invoke(T t);

}

兩個(gè)接口實(shí)現(xiàn)類,主要分固定時(shí)間和延時(shí)時(shí)間,具體業(yè)務(wù)代碼就不貼出來(lái)了

/**
 *
 * @Desecription: 延時(shí)隊(duì)列監(jiān)聽(tīng)
 * @Author: yangyu
 * @Date: 2021/9/10 14:32
 */
@Log4j2
@Service
public class DelayedQueueSlotTime implements RedisDelayedQueueListenerService<DelayQueueJob> {

    @Autowired
    private WeChatPay chatPay;

    @Autowired
    private CrcxPayDetailService crcxPayDetailService;

    @Autowired
    private CrcxOrderService crcxOrderService;

    @Autowired
    private DelayQueueJobPool delayQueueJobPool;

    @Override
    public void invoke(DelayQueueJob delayQueueJob) {
        log.info("時(shí)間段執(zhí)行:{}", delayQueueJob);
        // topic == 1 查單延時(shí)任務(wù)
        if (delayQueueJob.getTopic() == 1) {
            // 1.通過(guò)訂單編號(hào),調(diào)用微信查單接口,查詢微信訂單支付狀態(tài)。
            try {
                Map<String, String> queryOrder = chatPay.getQueryOrder(delayQueueJob.getOrderCode());
                // 2.若已付款,對(duì)比系統(tǒng)訂單狀態(tài),未付款修改訂單狀態(tài),付款不做處理,
                String tradeState = queryOrder.get("tradeState");
                if (tradeState.equalsIgnoreCase("SUCCESS")) {
                    CrcxOrder crcxOrder = new CrcxOrder();
                    crcxOrder.setOrderCode(delayQueueJob.getOrderCode());
                    crcxOrder = crcxOrderService.getCrcxOrderByCode(crcxOrder);
                    if (crcxOrder.getIsPay() == 0) {
                        // 3.通過(guò)訂單編號(hào) 查 crcx_pay_detail 支付單-明細(xì)
                        CrcxPayDetail crcxPayDetail = new CrcxPayDetail();
                        crcxPayDetail.setTradeCode(delayQueueJob.getOrderCode());
                        crcxPayDetail = crcxPayDetailService.getPayDetail(crcxPayDetail);
                        // 4.進(jìn)行修改訂單狀態(tài)
                        int update = crcxOrderService.updateOrderStatus(crcxPayDetail, queryOrder);
                        if (update >= 1) {
                            log.info("延時(shí)隊(duì)列:修改訂單支付狀態(tài)成功");
                        } else {
                            log.error("延時(shí)隊(duì)列:修改訂單支付狀態(tài)失敗");
                        }
                    }
                    // 5.標(biāo)記任務(wù)不需要再進(jìn)業(yè)務(wù)邏輯。因?yàn)樵?zset 中無(wú)法通過(guò)jobId 或者 訂單編號(hào)獲取對(duì)應(yīng)的List
                    delayQueueJob.setJobType(1);
                    delayQueueJobPool.updateDelayQueueJob(delayQueueJob, this.getClass().getSimpleName());
                }
                // 6.非已付款,不做任何處理,繼續(xù)執(zhí)行延時(shí)任務(wù),直到執(zhí)行完畢,或者訂單已付款。
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


/**
*
* @Desecription: 固定時(shí)間隊(duì)列監(jiān)聽(tīng)
* @Author: yangyu
* @Date: 2021/9/10 13:18
*/
@Log4j2
@Service
public class DelayedQueueFixedTime implements RedisDelayedQueueListenerService<DelayQueueJob> {

    @Autowired
    private CrcxOrderService crcxOrderService;

    @Override
    public void invoke(DelayQueueJob delayQueueJob) {
        log.info("固定時(shí)間執(zhí)行...." + delayQueueJob.getJobId());
        if (delayQueueJob != null) {
            // 查詢訂單狀態(tài)是否未支付狀態(tài)
            CrcxOrder crcxOrder = new CrcxOrder();
            crcxOrder.setOrderCode(delayQueueJob.getOrderCode());
            crcxOrder = crcxOrderService.getCrcxOrderByCode(crcxOrder);
            if (crcxOrder.getIsPay() == 0) {
                // 將待支付的訂單改為已取消(超時(shí)未支付)
                crcxOrder.setStatusCode(50);
                int ref = crcxOrderService.orderPaidTimeout(crcxOrder);
                if (ref == 0) throw new CustomException("自動(dòng)取消訂單失敗");
            }
        }
    }
}

初始化隊(duì)列監(jiān)聽(tīng)

/**
 * @Desecription: 初始化隊(duì)列監(jiān)聽(tīng)
 * @Author: yangyu
 * @Date: 2021/9/10 9:31
 */
@Log4j2
@Component
public class RedisDelayedQueueInit implements ApplicationContextAware {

    @Autowired
    private DelayBucket delayBucket;

    @Autowired
    private DelayQueueJobPool delayQueueJobPool;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, RedisDelayedQueueListenerService> map = applicationContext.getBeansOfType(RedisDelayedQueueListenerService.class);
        for (Map.Entry<String, RedisDelayedQueueListenerService> taskEventListenerEntry : map.entrySet()) {
            String listenerName = taskEventListenerEntry.getValue().getClass().getSimpleName();
            startThread(listenerName, taskEventListenerEntry.getValue());
        }
    }

    /**
     * @Desecription: 啟動(dòng)線程獲取隊(duì)列
     * @Param: queueName
     * @Param: redisDelayedQueueListenerService 任務(wù)回調(diào)監(jiān)聽(tīng)
     * @Return:
     * @Author: yangyu
     * @Date: 2021/9/10 11:32
     */
    private <T> void startThread(String queueName, RedisDelayedQueueListenerService redisDelayedQueueListenerService) {
        Thread thread = new Thread(() -> {
            // 有時(shí)間誤差,一般在一秒左右
            log.info("啟動(dòng)監(jiān)聽(tīng)隊(duì)列線程:{}", queueName);
            while (true) {
                try {
                    ScoredSortedItem item = delayBucket.getFromBucket(queueName);
                    // 沒(méi)有任務(wù)就堵塞
                    if (item == null) {
                        Thread.sleep(1000);
                        continue;
                    }
                    // 延遲時(shí)間沒(méi)到
                    if (item.getDelayTime() > System.currentTimeMillis()) {
                        Thread.sleep(1000);
                        continue;
                    }
                    DelayQueueJob delayQueueJob = delayQueueJobPool.getDelayQueueJob(item.getJobId(), queueName);
                    // 延遲任務(wù)數(shù)據(jù)不存在
                    if (delayQueueJob == null) {
                        log.info("延遲任務(wù)數(shù)據(jù)不存在·····");
                        delayBucket.deleteFormBucket(queueName, item);
                        Thread.sleep(1000);
                        continue;
                    }
                    // 如果后面任務(wù)不需要執(zhí)行,不走業(yè)務(wù)邏輯,刪除正常執(zhí)行
                    if (delayQueueJob.getJobType() == 0) {
                        new Thread(() -> {
                            DelayQueueJob job = new DelayQueueJob();
                            BeanUtils.copyProperties(delayQueueJob, job);
                            redisDelayedQueueListenerService.invoke(job);
                        }).start();
                    }
                    Integer exCount = delayQueueJob.getExCount();
                    if (exCount == 1) {
                        delayQueueJobPool.deleteDelayQueueJob(delayQueueJob.getJobId(), queueName);
                        delayBucket.deleteFormBucket(queueName, item);
                        continue;
                    }
                    delayQueueJob.setExCount(--exCount);
                    delayQueueJobPool.updateDelayQueueJob(delayQueueJob, queueName);
                    delayBucket.deleteFormBucket(queueName, item);
                } catch (Exception e) {
                    log.error("監(jiān)聽(tīng)隊(duì)列線程錯(cuò)誤:", e);
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        });
        thread.setName(queueName);
        thread.start();
    }
}

主要使用到的依賴包

<dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.10.5</version>
</dependency>

至此整個(gè)redis延時(shí)隊(duì)列就實(shí)現(xiàn)完成。

到此這篇關(guān)于java微信延遲支付的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)java微信延遲支付內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java線程中的ThreadLocal原理及源碼解析

    Java線程中的ThreadLocal原理及源碼解析

    這篇文章主要介紹了Java線程中的ThreadLocal原理及源碼解析,ThreadLocal 的作用是為每個(gè)線程保存一份局部變量的引用,實(shí)現(xiàn)多線程之間的數(shù)據(jù)隔離,從而避免了線程不安全情況的發(fā)生,需要的朋友可以參考下
    2023-12-12
  • 基于線程池的工作原理與源碼解讀

    基于線程池的工作原理與源碼解讀

    下面小編就為大家分享一篇基于線程池的工作原理與源碼解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • Java詳細(xì)分析Lambda表達(dá)式與Stream流的使用方法

    Java詳細(xì)分析Lambda表達(dá)式與Stream流的使用方法

    Lambda表達(dá)式,基于Lambda所帶來(lái)的函數(shù)式編程,又引入了一個(gè)全新的Stream概念,用于解決集合類庫(kù)既有的弊端,Lambda 允許把函數(shù)作為一個(gè)方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)。使用 Lambda 表達(dá)式可以使代碼變的更加簡(jiǎn)潔緊湊
    2022-04-04
  • Java獲取文件夾下所有文件名稱的方法示例

    Java獲取文件夾下所有文件名稱的方法示例

    這篇文章主要介紹了Java獲取文件夾下所有文件名稱的方法,涉及java針對(duì)文件與目錄相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • java連接Oracle數(shù)據(jù)庫(kù)的方法解析

    java連接Oracle數(shù)據(jù)庫(kù)的方法解析

    本文主要對(duì)java連接Oracle數(shù)據(jù)庫(kù)方法進(jìn)行步驟解析,具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧
    2016-12-12
  • 詳解Spring注解驅(qū)動(dòng)開(kāi)發(fā)之屬性賦值

    詳解Spring注解驅(qū)動(dòng)開(kāi)發(fā)之屬性賦值

    今天帶大家學(xué)習(xí)Spring注解驅(qū)動(dòng)開(kāi)發(fā)的相關(guān)知識(shí),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Java Comparable和Comparator對(duì)比詳解

    Java Comparable和Comparator對(duì)比詳解

    這篇文章主要介紹了Java Comparable和Comparator對(duì)比詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java基礎(chǔ)泛型詳情

    Java基礎(chǔ)泛型詳情

    這篇文章主要介紹了Java基礎(chǔ)泛型詳情,泛型是JDK5中引入的特性,它提供了編譯時(shí)類型安全檢測(cè)機(jī)制,該機(jī)制允許在編譯時(shí)檢測(cè)到非法的類型,下面文章的詳細(xì)介紹,需要的朋友可以參考一下
    2022-04-04
  • jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理詳解

    jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理詳解

    本篇文章主要介紹了深度剖析java中JDK動(dòng)態(tài)代理機(jī)制 ,動(dòng)態(tài)代理避免了開(kāi)發(fā)人員編寫各個(gè)繁鎖的靜態(tài)代理類,只需簡(jiǎn)單地指定一組接口及目標(biāo)類對(duì)象就能動(dòng)態(tài)的獲得代理對(duì)象
    2021-07-07
  • java.security.egd?作用詳解

    java.security.egd?作用詳解

    這篇文章主要為大家介紹了java.security.egd作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08

最新評(píng)論