最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1.需求

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

2.實現(xiàn)方式

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

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

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

  • delayqueue
  • RabbitMQ
  • reids

3.具體代碼

新建實體類 DelayQueueJob 與 ScoredSortedItem

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

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

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

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

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

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

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

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

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

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

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

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

}

定義常量

/**
 * @Desecription: 延時隊列常量
 * @Author: yangyu
 * @Date: 2021/9/10 14:44
 */
public class DelayQueueConstant {

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

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

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

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

}

zset有序隊列操作類

/**
 * @Desecription: 以時間為維度的有序隊列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ù)桶中獲取延遲時間最小的 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);
    }
}

延時任務(wù)類

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

    @Autowired
    private RedissonClient redissonClient;

    /**
     * @Desecription: 添加延時隊列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: 刪除延時隊列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: 查詢延時隊列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: 修改延時隊列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ù)到延遲隊列
    * @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());
   }

}

隊列事件監(jiān)聽接口

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

    void invoke(T t);

}

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

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


/**
*
* @Desecription: 固定時間隊列監(jiān)聽
* @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("固定時間執(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) {
                // 將待支付的訂單改為已取消(超時未支付)
                crcxOrder.setStatusCode(50);
                int ref = crcxOrderService.orderPaidTimeout(crcxOrder);
                if (ref == 0) throw new CustomException("自動取消訂單失敗");
            }
        }
    }
}

初始化隊列監(jiān)聽

/**
 * @Desecription: 初始化隊列監(jiān)聽
 * @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: 啟動線程獲取隊列
     * @Param: queueName
     * @Param: redisDelayedQueueListenerService 任務(wù)回調(diào)監(jiān)聽
     * @Return:
     * @Author: yangyu
     * @Date: 2021/9/10 11:32
     */
    private <T> void startThread(String queueName, RedisDelayedQueueListenerService redisDelayedQueueListenerService) {
        Thread thread = new Thread(() -> {
            // 有時間誤差,一般在一秒左右
            log.info("啟動監(jiān)聽隊列線程:{}", queueName);
            while (true) {
                try {
                    ScoredSortedItem item = delayBucket.getFromBucket(queueName);
                    // 沒有任務(wù)就堵塞
                    if (item == null) {
                        Thread.sleep(1000);
                        continue;
                    }
                    // 延遲時間沒到
                    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)聽隊列線程錯誤:", 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>

至此整個redis延時隊列就實現(xiàn)完成。

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

相關(guān)文章

  • MyBatisX插件之domain文件生成不了問題

    MyBatisX插件之domain文件生成不了問題

    文章描述了在使用MyBatisX插件生成MyBatis的domain文件時遇到的問題,特別是在使用MyBatisX版本1.6.1和MySQL版本8.0.34的情況下,生成的domain文件不完整,作者通過勾選Model選項解決了這個問題,并分享了這一經(jīng)驗,希望能幫助其他遇到類似問題的用戶
    2025-01-01
  • 老生常談 MyBatis 復(fù)雜查詢

    老生常談 MyBatis 復(fù)雜查詢

    這篇文章主要介紹了 MyBatis 復(fù)雜查詢的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Spring boot中Jackson的操作指南

    Spring boot中Jackson的操作指南

    這篇文章主要給大家介紹了關(guān)于Spring boot中Jackson操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Spring?Boot4實戰(zhàn)指南之Jackson2.x升級到3.x踩坑完全記錄

    Spring?Boot4實戰(zhàn)指南之Jackson2.x升級到3.x踩坑完全記錄

    Spring?Boot升級幾乎是Java后端項目繞不開的話題,尤其是很多老項目還停留在2.x,一旦要升級,就會發(fā)現(xiàn)牽扯的絕不只是改個版本號,這篇文章主要介紹了Spring?Boot4實戰(zhàn)指南之Jackson2.x升級到3.x踩坑完全記錄的相關(guān)資料,需要的朋友可以參考下
    2026-05-05
  • Java開發(fā)中常用的 Websocket 技術(shù)參考

    Java開發(fā)中常用的 Websocket 技術(shù)參考

    WebSocket 使得客戶端和服務(wù)器之間的數(shù)據(jù)交換變得更加簡單,允許服務(wù)端主動向客戶端推送數(shù)據(jù),當(dāng)然也支持客戶端發(fā)送數(shù)據(jù)到服務(wù)端。
    2020-09-09
  • Java String、StringBuffer與StringBuilder的區(qū)別

    Java String、StringBuffer與StringBuilder的區(qū)別

    本文主要介紹Java String、StringBuffer與StringBuilder的區(qū)別的資料,這里整理了相關(guān)資料及詳細(xì)說明其作用和利弊點,有需要的小伙伴可以參考下
    2016-09-09
  • Java實現(xiàn)直接插入排序與折半插入排序的示例詳解

    Java實現(xiàn)直接插入排序與折半插入排序的示例詳解

    這篇文章主要為大家詳細(xì)介紹了插入排序中兩個常見的排序:直接插入排序與折半插入排序。本文用Java語言實現(xiàn)了這兩個排序算法,感興趣的可以學(xué)習(xí)一下
    2022-06-06
  • springboot跨域CORS處理代碼解析

    springboot跨域CORS處理代碼解析

    這篇文章主要介紹了springboot跨域CORS處理代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • 詳解RabbitMq如何做到消息的可靠性投遞

    詳解RabbitMq如何做到消息的可靠性投遞

    這篇文章主要為大家介紹了RabbitMq如何做到消息的可靠性投遞,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Java算法之串的簡單處理

    Java算法之串的簡單處理

    今天小編就為大家分享一篇關(guān)于Java算法之串的簡單處理,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03

最新評論

玉门市| 平江县| 高平市| 东阿县| 美姑县| 广饶县| 阿拉善右旗| 子洲县| 兴安县| 阿拉善左旗| 彭州市| 仙居县| 南乐县| 惠水县| 广丰县| 蒙阴县| 大连市| 冷水江市| 利川市| 阳原县| 嵊州市| 蒙山县| 邓州市| 鹿泉市| 长春市| 九江县| 隆林| 长宁县| 文水县| 壶关县| 怀远县| 信丰县| 中宁县| 和田县| 长乐市| 新乐市| 余干县| 丹巴县| 涟水县| 陆川县| 汾西县|