java – How to implement a request queue?


Initial data :

There is a project that uses methods from the SDK of a third-party project (asynchronous methods for working with their server with a callback parameter) and contains Retrofit2 + OkHttp + Rx for working with the server directly. For convenience, lambdas are used.

Task :

It is necessary that all requests (both from the SDK and via Retrofit) are executed no more than 5 times per second, and if the limit is exceeded, they are executed with a delay.

Question :

How to implement it? The first thing that comes to mind is Service + BroadcastReceiver. But you will have to listen to the receiver in each activity/fragment, plus you will get a not so convenient implementation of callbacks… Maybe you can somehow implement this using Rx (including wrapping SDK methods) to leave lambdas for convenience?


Maybe this will help?

Subscription subscription = Observable.interval(1000, 5000, 
        .subscribe(new Action1<Long>() {
            public void call(Long aLong) {
                // here is the task that should repeat

Scroll to Top