Commit 2292a0ea by pye52

增加心跳定时检查离线订单的机制(同时避免离线订单频繁上传的限制)

parent ffceadd6
......@@ -41,6 +41,7 @@ public class PayOfflineViewModel extends ViewModel {
// 在线支付延迟150ms执行,留出时间给扫码反馈
private static final long REQUEST_DELAY = 150;
private static final long DEFAULT_DELAY = 3 * 1000;
private static final long PAY_INTERVAL = 60 * 60 * 1000;
private Gson gson;
private String deviceSN;
private PayDataRepository payDataRepository;
......@@ -53,6 +54,8 @@ public class PayOfflineViewModel extends ViewModel {
}
private PayRequest payRequest;
private long lastExecTime = -1;
private ScheduledFuture<?> payOfflineFuture;
private ScheduledFuture<?> timeoutFuture;
public PayOfflineViewModel(PayDataRepository payDataRepository,
......@@ -75,11 +78,25 @@ public class PayOfflineViewModel extends ViewModel {
}
private void traversalPayOfflineData() {
// 若与上次执行相隔太近,则跳过
long currentTime = System.currentTimeMillis();
if ((currentTime - lastExecTime) < PAY_INTERVAL) {
LogUtils.d(TAG, "离线检测过于频繁");
return;
}
lastExecTime = currentTime;
cancelPayOffline();
cancelTimeout();
TimeoutRunnable timeoutRunnable = new TimeoutRunnable();
timeoutFuture = SCTaskExecutor.getInstance().schedule(timeoutRunnable, TIMEOUT, TimeUnit.MILLISECONDS);
RequestRunnable runnable = new RequestRunnable();
SCTaskExecutor.getInstance().executeOnDiskIO(runnable);
payOfflineFuture = SCTaskExecutor.getInstance().schedule(runnable, 0, TimeUnit.MILLISECONDS);
}
private void cancelPayOffline() {
if (payOfflineFuture == null) return;
payOfflineFuture.cancel(true);
payOfflineFuture = null;
}
private void cancelTimeout() {
......@@ -92,13 +109,27 @@ public class PayOfflineViewModel extends ViewModel {
private static final String RESPONSE_MESSAGE = "message";
private static final String RESPONSE_OFFLINE_RESULT = "操作完成";
private static final String RESPONSE_PAY_OFFLINE_RESULT = "PAY_OFFLINE_RESULT";
// 每心跳多少次后进行一次离线订单检测
private static final int PAY_OFFLINE_CHECK = 30;
private int heartbeatCount = 0;
@Override
public void onOpen(ServerHandshake data) {
heartbeatCount = 0;
traversalPayOfflineData();
}
@Override
public void onHeartbeat() {
// 在一定心跳次数后开始检查离线订单是否已处理完毕
heartbeatCount++;
if (heartbeatCount >= PAY_OFFLINE_CHECK) {
heartbeatCount = 0;
traversalPayOfflineData();
}
}
@Override
public void onMessage(String action, JsonObject obj, String original) {
if (action.equals(RESPONSE_PAY_OFFLINE_RESULT)) {
// 离线订单已写入到服务器数据库,可以标记为支付成功
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment