Commit ea0f8e2cbb0606e1bf2d39d66cbbdfaa4031fe6f

Authored by huanggang
1 parent c17e6990

scan time code optimization

cashier-pipeline/src/main/java/com/diligrp/cashier/pipeline/Constants.java
@@ -15,12 +15,18 @@ public final class Constants { @@ -15,12 +15,18 @@ public final class Constants {
15 // 农商行聚合支付结果通知URI 15 // 农商行聚合支付结果通知URI
16 public static final String RCB_PAYMENT_NOTIFY_URI = "%s/rcb/payment/%s/notify.do"; 16 public static final String RCB_PAYMENT_NOTIFY_URI = "%s/rcb/payment/%s/notify.do";
17 17
18 - public static final long ONE_MINUTE = 60 * 1000; 18 + public static final long FIVE_SECONDS = 5;
19 19
20 - public static final long TEN_MINUTES = 10 * ONE_MINUTE; 20 + public static final long TEN_SECONDS = 10;
  21 +
  22 + public static final long FIFTEEN_SECONDS = 15;
  23 +
  24 + public static final long ONE_MINUTE = 60; // 单位秒
21 25
22 - public static final long ONE_SECOND = 1000; 26 + public static final long FIVE_MINUTES = 5 * ONE_MINUTE;
  27 +
  28 + public static final long TEN_MINUTES = 10 * ONE_MINUTE;
23 29
24 - // 支付通道最小超时时间(单位秒), 一分钟 30 + // 支付订单最小超时时间(单位秒), 一分钟
25 public static final long MIN_PIPELINE_TIMEOUT = 60; 31 public static final long MIN_PIPELINE_TIMEOUT = 60;
26 } 32 }
cashier-pipeline/src/main/java/com/diligrp/cashier/pipeline/core/DefaultTimeStrategy.java
1 package com.diligrp.cashier.pipeline.core; 1 package com.diligrp.cashier.pipeline.core;
2 2
  3 +import java.time.Duration;
  4 +
3 public class DefaultTimeStrategy implements ScanTimeStrategy { 5 public class DefaultTimeStrategy implements ScanTimeStrategy {
4 6
5 // 预支付订单扫描时间策略 7 // 预支付订单扫描时间策略
@@ -24,26 +26,26 @@ public class DefaultTimeStrategy implements ScanTimeStrategy { @@ -24,26 +26,26 @@ public class DefaultTimeStrategy implements ScanTimeStrategy {
24 } 26 }
25 27
26 @Override 28 @Override
27 - public long nextPrepayScanTime(int times) { 29 + public Duration nextPrepayScanTime(int times) {
28 if (times - 1 < prepayTimeArray.length) { 30 if (times - 1 < prepayTimeArray.length) {
29 - return prepayTimeArray[times - 1]; 31 + return Duration.ofSeconds(prepayTimeArray[times - 1]);
30 } 32 }
31 - return -1; 33 + return null;
32 } 34 }
33 35
34 @Override 36 @Override
35 - public long nextPaymentScanTime(int times) { 37 + public Duration nextPaymentScanTime(int times) {
36 if (times - 1 < paymentTimeArray.length) { 38 if (times - 1 < paymentTimeArray.length) {
37 - return paymentTimeArray[times - 1]; 39 + return Duration.ofSeconds(paymentTimeArray[times - 1]);
38 } 40 }
39 - return -1; 41 + return null;
40 } 42 }
41 43
42 @Override 44 @Override
43 - public long nextRefundScanTime(int times) { 45 + public Duration nextRefundScanTime(int times) {
44 if (times - 1 < refundTimeArray.length) { 46 if (times - 1 < refundTimeArray.length) {
45 - return refundTimeArray[times - 1]; 47 + return Duration.ofSeconds(refundTimeArray[times - 1]);
46 } 48 }
47 - return -1; 49 + return null;
48 } 50 }
49 } 51 }
cashier-pipeline/src/main/java/com/diligrp/cashier/pipeline/core/ScanTimeStrategy.java
1 package com.diligrp.cashier.pipeline.core; 1 package com.diligrp.cashier.pipeline.core;
2 2
3 -import static com.diligrp.cashier.pipeline.Constants.ONE_MINUTE;  
4 -import static com.diligrp.cashier.pipeline.Constants.ONE_SECOND; 3 +import java.time.Duration;
  4 +
  5 +import static com.diligrp.cashier.pipeline.Constants.*;
5 6
6 public interface ScanTimeStrategy { 7 public interface ScanTimeStrategy {
7 - long[] DEFAULT_PREPAY_TIME_ARRAY = { 10 * ONE_MINUTE }; 8 + long[] DEFAULT_PREPAY_TIME_ARRAY = { TEN_MINUTES };
8 9
9 - long[] DEFAULT_PAYMENT_TIME_ARRAY = { 5 * ONE_SECOND, 5 * ONE_SECOND, 5 * ONE_SECOND,  
10 - 5 * ONE_SECOND, 5 * ONE_SECOND, 5 * ONE_SECOND, 5 * ONE_SECOND, 5 * ONE_SECOND, 10 * ONE_SECOND, 15 * ONE_SECOND }; 10 + long[] DEFAULT_PAYMENT_TIME_ARRAY = { FIVE_SECONDS, FIVE_SECONDS, FIVE_SECONDS, FIVE_SECONDS, FIVE_SECONDS,
  11 + FIVE_SECONDS, FIVE_SECONDS, FIVE_SECONDS, TEN_SECONDS, FIFTEEN_SECONDS };
11 12
12 - long[] DEFAULT_REFUND_TIME_ARRAY = { ONE_MINUTE, 2 * ONE_MINUTE, 2 * ONE_MINUTE, 5 * ONE_MINUTE }; 13 + long[] DEFAULT_REFUND_TIME_ARRAY = { ONE_MINUTE, 2 * ONE_MINUTE, 2 * ONE_MINUTE, FIVE_MINUTES };
13 14
14 // 预支付订单延迟扫描时间 15 // 预支付订单延迟扫描时间
15 - long nextPrepayScanTime(int times); 16 + Duration nextPrepayScanTime(int times);
16 17
17 // 支付订单延迟扫描时间 18 // 支付订单延迟扫描时间
18 - long nextPaymentScanTime(int times); 19 + Duration nextPaymentScanTime(int times);
19 20
20 // 退款订单延迟扫描时间 21 // 退款订单延迟扫描时间
21 - long nextRefundScanTime(int times); 22 + Duration nextRefundScanTime(int times);
22 } 23 }
cashier-trade/src/main/java/com/diligrp/cashier/trade/manager/TaskMessageSender.java
@@ -13,6 +13,8 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; @@ -13,6 +13,8 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate;
13 import org.springframework.stereotype.Service; 13 import org.springframework.stereotype.Service;
14 14
15 import java.nio.charset.StandardCharsets; 15 import java.nio.charset.StandardCharsets;
  16 +import java.time.Duration;
  17 +import java.util.Objects;
16 18
17 @Service("taskMessageSender") 19 @Service("taskMessageSender")
18 public class TaskMessageSender { 20 public class TaskMessageSender {
@@ -26,8 +28,8 @@ public class TaskMessageSender { @@ -26,8 +28,8 @@ public class TaskMessageSender {
26 /** 28 /**
27 * 发送延时处理消息 29 * 发送延时处理消息
28 */ 30 */
29 - public void sendDelayTaskMessage(TaskMessage task, long delayInMillis) {  
30 - if (delayInMillis < 0) { 31 + public void sendDelayTaskMessage(TaskMessage task, Duration duration) {
  32 + if (Objects.isNull(duration)) {
31 LOG.debug("No need send scan order message: {}", task); 33 LOG.debug("No need send scan order message: {}", task);
32 return; 34 return;
33 } 35 }
@@ -39,7 +41,7 @@ public class TaskMessageSender { @@ -39,7 +41,7 @@ public class TaskMessageSender {
39 properties.setContentType(MessageProperties.CONTENT_TYPE_BYTES); 41 properties.setContentType(MessageProperties.CONTENT_TYPE_BYTES);
40 // properties.setExpiration(String.valueOf(expiredTime)); 42 // properties.setExpiration(String.valueOf(expiredTime));
41 // RabbitMQ延时插件必须设置x-delay的header才能生效 43 // RabbitMQ延时插件必须设置x-delay的header才能生效
42 - properties.setHeader("x-delay", String.valueOf(delayInMillis)); 44 + properties.setHeader("x-delay", String.valueOf(duration.toMillis()));
43 String payload = JsonUtils.toJsonString(task); 45 String payload = JsonUtils.toJsonString(task);
44 Message message = new Message(payload.getBytes(StandardCharsets.UTF_8), properties); 46 Message message = new Message(payload.getBytes(StandardCharsets.UTF_8), properties);
45 LOG.debug("Sending async delay task message: {}", task.getPayload()); 47 LOG.debug("Sending async delay task message: {}", task.getPayload());
cashier-trade/src/main/java/com/diligrp/cashier/trade/service/impl/CashierAssistantServiceImpl.java
@@ -26,6 +26,7 @@ import org.slf4j.Logger; @@ -26,6 +26,7 @@ import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory; 26 import org.slf4j.LoggerFactory;
27 import org.springframework.stereotype.Service; 27 import org.springframework.stereotype.Service;
28 28
  29 +import java.time.Duration;
29 import java.time.LocalDateTime; 30 import java.time.LocalDateTime;
30 import java.util.List; 31 import java.util.List;
31 32
@@ -114,9 +115,9 @@ public class CashierAssistantServiceImpl implements ICashierAssistantService { @@ -114,9 +115,9 @@ public class CashierAssistantServiceImpl implements ICashierAssistantService {
114 OnlineRefundResponse response = pipeline.queryRefundResponse(order); 115 OnlineRefundResponse response = pipeline.queryRefundResponse(order);
115 cashierPaymentService.notifyRefundResult(refund, response); 116 cashierPaymentService.notifyRefundResult(refund, response);
116 if (!PaymentState.isFinished(response.getState().getCode())) { 117 if (!PaymentState.isFinished(response.getState().getCode())) {
117 - long delay = pipeline.getTimeStrategy().nextRefundScanTime(times + 1); 118 + Duration duration = pipeline.getTimeStrategy().nextRefundScanTime(times + 1);
118 TaskMessage message = new TaskMessage(TaskMessage.TYPE_CASHIER_REFUND_SCAN, refundId, String.valueOf(times + 1)); 119 TaskMessage message = new TaskMessage(TaskMessage.TYPE_CASHIER_REFUND_SCAN, refundId, String.valueOf(times + 1));
119 - taskMessageSender.sendDelayTaskMessage(message, delay); 120 + taskMessageSender.sendDelayTaskMessage(message, duration);
120 } 121 }
121 } 122 }
122 } 123 }
cashier-trade/src/main/java/com/diligrp/cashier/trade/service/impl/CashierPaymentServiceImpl.java
@@ -40,6 +40,7 @@ import org.slf4j.LoggerFactory; @@ -40,6 +40,7 @@ import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service; 40 import org.springframework.stereotype.Service;
41 import org.springframework.transaction.annotation.Transactional; 41 import org.springframework.transaction.annotation.Transactional;
42 42
  43 +import java.time.Duration;
43 import java.time.LocalDateTime; 44 import java.time.LocalDateTime;
44 import java.util.List; 45 import java.util.List;
45 import java.util.Objects; 46 import java.util.Objects;
@@ -98,7 +99,7 @@ public class CashierPaymentServiceImpl implements ICashierPaymentService { @@ -98,7 +99,7 @@ public class CashierPaymentServiceImpl implements ICashierPaymentService {
98 99
99 // 兜底处理交易订单,根据支付结果选择关闭或完成交易订单 100 // 兜底处理交易订单,根据支付结果选择关闭或完成交易订单
100 TaskMessage message = new TaskMessage(TaskMessage.TYPE_CASHIER_ORDER_SCAN, tradeId, "1"); 101 TaskMessage message = new TaskMessage(TaskMessage.TYPE_CASHIER_ORDER_SCAN, tradeId, "1");
101 - taskMessageSender.sendDelayTaskMessage(message, timeout * 1000); // 转换成毫秒 102 + taskMessageSender.sendDelayTaskMessage(message, Duration.ofSeconds(timeout));
102 return tradeId; 103 return tradeId;
103 } 104 }
104 105
@@ -365,9 +366,9 @@ public class CashierPaymentServiceImpl implements ICashierPaymentService { @@ -365,9 +366,9 @@ public class CashierPaymentServiceImpl implements ICashierPaymentService {
365 } else if (!PaymentState.isFinished(response.getState().getCode())) { 366 } else if (!PaymentState.isFinished(response.getState().getCode())) {
366 // 固定周期后,查询退款状态,根据状态完成退款订单 367 // 固定周期后,查询退款状态,根据状态完成退款订单
367 if (pipeline instanceof OnlinePipeline<?> onlinePipeline) { // 只有在线支付通道允许查询退款状态 368 if (pipeline instanceof OnlinePipeline<?> onlinePipeline) { // 只有在线支付通道允许查询退款状态
368 - long delay = onlinePipeline.getTimeStrategy().nextRefundScanTime(1); 369 + Duration duration = onlinePipeline.getTimeStrategy().nextRefundScanTime(1);
369 TaskMessage message = new TaskMessage(TaskMessage.TYPE_CASHIER_REFUND_SCAN, refundId, "1"); 370 TaskMessage message = new TaskMessage(TaskMessage.TYPE_CASHIER_REFUND_SCAN, refundId, "1");
370 - taskMessageSender.sendDelayTaskMessage(message, delay); 371 + taskMessageSender.sendDelayTaskMessage(message, duration);
371 } 372 }
372 } 373 }
373 374