How to make RxJava interval to perform action instantly

Hello I am making observable to ask my server about its online/offline status every 15 seconds:

public Observable<Response> repeatCheckServerStatus(int intervalSec, final String path) {
        return Observable.interval(intervalSec, TimeUnit.SECONDS)
                .flatMap(new Func1<Long, Observable<Response>>() {
                    @Override
                    public Observable<Response> call(Long aLong) {
                        return Observable.create(new Observable.OnSubscribe<Response>() {
                            @Override
                            public void call(Subscriber<? super Response> subscriber) {
                                try {
                                    Response response = client.newCall(new Request.Builder()
                                            .url(path + API_ACTION_CHECK_ONLINE_STATUS)
                                            .header("Content-Type", "application/x-www-form-urlencoded")
                                            .get()
                                            .build()).execute();

                                    subscriber.onNext(response);
                                    subscriber.onCompleted();
                                    if (!response.isSuccessful())
                                        subscriber.onError(new Exception());
                                } catch (Exception e) {
                                    subscriber.onError(e);
                                }
                            }
                        })
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread());
                    }
                });

    }

After I call this method, first execution of code will be after intervalSec time (15sec in my case). Looking at rxJava docummentation of interval method:

http://reactivex.io/documentation/operators/interval.html

This is how it should be.

Question: is there any way to execute code instantly and then repeat in intervals?

Answers


What you are looking for is startWith

Observable.interval(15, SECONDS).startWith(1);

This will get the updates from the interval, but emit one item immediately after subscribing.


You can execute it immediately also like this:

Observable.interval(0, 1000, TimeUnit.MILLISECONDS).subscribe();

you can use `

Observable.interval(1, TimeUnit.SECONDS).startWith(0)

` It is duplicate value "0" in subscribe.


Need Your Help

Java client/server application with sockets?

java sockets client-server

I'm writing a java package that will be called by another language (matlab). If my matlab process ends, I want the Java process to keep running. Whenever matlab starts again, it should be able to

Ensuring py.test includes the application directory in sys.path

python unit-testing pytest

I have a project directory structure as follows (which I think is pretty standard):