先来抛出需求:商家接收的客户发出的订单肯定是越快越好,大家普遍认为推送是最好的选择,然而真正现实场景中,在消费高峰期(晚上聚餐去人最多的餐厅吃饭),推送的延时性是让商家最不可忍受的,当然,可能跟我们接入的免费版的SDK有关,收费版的我们公司没有使用过,如果你们使用过收费版的,麻烦评论区告知我们收费版的会不会存在延时,丢推送这些无法忍受的问题。
所以我们后来用轮训和推送同时使用,然后做一个过滤,谁先到就优先处理,其余的过滤掉,刚开始接入轮训的时候肯定是要确定一个让商家能忍受的时间来获取订单,于是就有了每10秒一次请求一次订单,在商用一段时间后,抱怨比之前少了很多。但是还是有些商家不满意,消费人群特别多的商家,如果每个人等10秒,总会有些不满意的声音。
于是,新的需求就如期而来了。。。闲时每10秒轮训一次,高峰期每5秒轮训一次,过了高峰期又恢复到10秒轮训一次。
怎么感觉在写小说呢?我都看不下去了,密密麻麻的。。。
针对:闲时--每10秒轮训一次,忙时--每5秒轮训一次
我们用一个效果来模拟这个场景: 0—30秒:每10秒一次 31—60秒:每5秒一次
轮训用Rxjava的interval()来实现,效果:
代码如下:
package com.ishangbin.shop.ui;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import com.ishangbin.shop.ui.act.util.LogUtil;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
/**
* Created by zhouhui on 2017/12/28.
*/
public class TestActivity extends AppCompatActivity {
/**
* 5秒轮训
*/
private ObserverShort mShortObserver;
private Observable<Long> mObservableOrderPollingShort;
private Subscription mSubscriptionOrderPollingShort;
/**
* 10秒轮训
*/
private ObserverLong mObserverLong;
private Observable<Long> mObservableOrderPollingLong;
private Subscription mSubscriptionOrderPollingLong;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
// 参数说明:
// 参数1 = 第1次延迟时间;
// 参数2 = 间隔时间数字;
// 参数3 = 时间单位;
// Observable.interval(5, 5, TimeUnit.SECONDS)
mShortObserver = new ObserverShort();
mObservableOrderPollingShort = Observable.interval(5, 5, TimeUnit.SECONDS);
mObserverLong = new ObserverLong();
mObservableOrderPollingLong = Observable.interval(10, 10, TimeUnit.SECONDS);
initLongPolling();
}
@Override
public void onDestroy() {
if (mSubscriptionOrderPollingShort != null && mSubscriptionOrderPollingShort.isUnsubscribed()) {
mSubscriptionOrderPollingShort.unsubscribe();
}
if (mSubscriptionOrderPollingLong != null && mSubscriptionOrderPollingLong.isUnsubscribed()) {
mSubscriptionOrderPollingLong.unsubscribe();
}
super.onDestroy();
}
/**
* 短轮训
*/
private void initShortPolling() {
mSubscriptionOrderPollingShort = mObservableOrderPollingShort.subscribe(mShortObserver);
}
/**
* 长轮训
*/
private void initLongPolling() {
mSubscriptionOrderPollingLong = mObservableOrderPollingLong.subscribe(mObserverLong);
}
/**
* 短轮训的回调
*/
public class ObserverShort implements Observer<Long> {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onNext(Long aLong) {
Calendar curDateTime = Calendar.getInstance();
curDateTime.setTimeInMillis(System.currentTimeMillis());
int year = curDateTime.get(Calendar.YEAR);
int month = curDateTime.get(Calendar.MONTH);
int day = curDateTime.get(Calendar.DAY_OF_MONTH);
int hour = curDateTime.get(Calendar.HOUR_OF_DAY);
int minute = curDateTime.get(Calendar.MINUTE);
int second = curDateTime.get(Calendar.SECOND);
LogUtil.i("OrderPollingService---未打印订单的轮训(短)---" + second);
if (second < 30) {
mSubscriptionOrderPollingShort.unsubscribe();
initLongPolling();
}
}
}
/**
* 长轮训的回调
*/
public class ObserverLong implements Observer<Long> {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onNext(Long aLong) {
Calendar curDateTime = Calendar.getInstance();
curDateTime.setTimeInMillis(System.currentTimeMillis());
int second = curDateTime.get(Calendar.SECOND);
LogUtil.i("OrderPollingService---未打印订单的轮训(长)---" + second);
if (second >= 30) {
mSubscriptionOrderPollingLong.unsubscribe();
initShortPolling();
}
}
}
}
复制代码