Java Code Examples for io.reactivex.Observable#interval()

The following examples show how to use io.reactivex.Observable#interval() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: rxjava2-lab   File: Code05.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Observable<String> strings = Observable.just("a", "b", "c", "d");
    Observable<Long> ticks = Observable.interval(500, TimeUnit.MILLISECONDS);
    Observable<String> stream = Observable.zip(ticks, strings, (t, s) -> s);

    TestObserver<String> testObserver = stream.test();
    if (testObserver.awaitTerminalEvent(3, TimeUnit.SECONDS)) {
        testObserver
                .assertNoErrors()
                .assertComplete()
                .assertValues("a", "b", "c", "d");
        System.out.println("Cool!");
    } else {
        System.out.println("It did not finish on time");
    }
}
 
Example 2
@Test
public void testScheduler() {
    TestScheduler scheduler = new TestScheduler(); //1
    Observable<Long> tick = Observable
            .interval(1, TimeUnit.SECONDS, scheduler); //2
    Observable<String> observable =
            Observable.just("foo", "bar", "biz", "baz") //3
            .zipWith(tick, (string, index) -> index + "-" + string);//4
    TestObserver<String> testObserver = observable
            .subscribeOn(scheduler).test();//5

    scheduler.advanceTimeBy(2300, TimeUnit.MILLISECONDS);//6

    testObserver.assertNoErrors(); //7
    testObserver.assertValues("0-foo", "1-bar");
    testObserver.assertNotComplete();
}
 
Example 3
@Test
public void testCombineLatestObservable() throws InterruptedException {

    Observable<Long> observable1 = Observable.interval(100, TimeUnit.MILLISECONDS);
    Observable<Long> observable2 = Observable.interval(100, TimeUnit.MILLISECONDS);

    Observable.combineLatest(observable1, observable2,
            (observable1Times, observable2Times) ->
                    "Observable1: " + observable1Times + " Observable2: " + observable2Times)
            .subscribe(item -> System.out.println(item));

    Thread.sleep(1000);
}
 
Example 4
public static void demoInterval() {

		Observable<Long> myObservable = Observable.interval(2, TimeUnit.SECONDS);
		Observer<Long> myObserver = new Observer<Long>() {

			@Override
			public void onComplete() {
				// TODO Auto-generated method stub
				System.out.println("emission completed");

			}

			@Override
			public void onError(Throwable throwable) {
				// TODO Auto-generated method stub
				System.out.println("sorry you got an error");

			}

			@Override
			public void onNext(Long item) {
				// TODO Auto-generated method stub
				System.out.println("item" + item);

			}

			@Override
			public void onSubscribe(Disposable arg0) {
				// TODO Auto-generated method stub

			}
		};
		myObservable.subscribe(myObserver);
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
 
Example 5
Source Project: RHub   File: RxJava2.java    License: Apache License 2.0 5 votes vote down vote up
private static void generalExample(RxJava2ObsHub rxJava2Hub) {
    Observable src1 = Observable.fromArray(new Integer[]{1, 3, 5, 7, 11, 13});
    Observable src2 = Observable.interval(1, TimeUnit.SECONDS);
    rxJava2Hub.addUpstream("src1", src1);

    rxJava2Hub.getPub("src1").subscribe(o -> {
        System.out.println("consumer1 (src1) got: " + o);
    });

    rxJava2Hub.getPub("src1").subscribe(System.out::println);
    rxJava2Hub.getPub("src1.1").subscribe(o -> {
        System.out.println("consumer1 (src1.1) got: " + o);
    });

    rxJava2Hub.addUpstream("src1.1", src1.repeat(1));
    rxJava2Hub.addUpstream("src2", src2.buffer(Integer.MAX_VALUE));

    rxJava2Hub.getPub("src1").subscribe(o -> {
        System.out.println("consumer2 (src1) got: " + o);
    });

    rxJava2Hub.getPub("src1.1").subscribe(o -> {
        System.out.println("consumer2 (src1.1) got: " + o);
    });
    rxJava2Hub.getPub("src2").subscribe(o -> {
        System.out.println("consumer2 (src2) got: " + o);
    });
    new Thread(() -> {
        try {
            Thread.sleep(5000);
            rxJava2Hub.addUpstream("src1.1", Observable.interval(1, TimeUnit.SECONDS));
            Thread.sleep(5000);
            rxJava2Hub.clearUpstream();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).start();
}
 
Example 6
public static void main(String args[]) {
	
	
	Observable<Long> observableInt = Observable.interval(2, TimeUnit.SECONDS);
	
	ConnectableObservable<Long> connectableIntObservable = observableInt.publish();
	connectableIntObservable.subscribe(i -> System.out.println("Observable #1 : "+i));

	connectableIntObservable.connect();
					
	addDelay(7000);
	connectableIntObservable.
		subscribe(i -> System.out.println("Observable #2 : "+i));

	addDelay(10000);

      
}
 
Example 7
Source Project: Tangram-Android   File: LifeCycleHelperTest.java    License: MIT License 4 votes vote down vote up
@Before
public void setUp() {
    mTestScheduler = new TestScheduler();
    mObservable = Observable.interval(1, TimeUnit.MINUTES, mTestScheduler);
    mObserver = new TestObserver<Long>();
}
 
Example 8
Source Project: ocraft-s2client   File: ResponseQueueTest.java    License: MIT License 4 votes vote down vote up
private Observable<Response> responseStream(TestScheduler scheduler) {
    List<Response> responses = Collections.singletonList(ResponsePing.from(GameServerResponses.ping()));
    Observable<Long> tick = Observable.interval(1, TimeUnit.SECONDS, scheduler);
    return Observable.fromIterable(responses).zipWith(tick, (r, index) -> r);
}
 
Example 9
private Observable<? extends Long> getObservable() {
    return Observable.interval(0, 2, TimeUnit.SECONDS);
}
 
Example 10
Source Project: vertx-rx   File: NativeExamples.java    License: Apache License 2.0 4 votes vote down vote up
public void blockingScheduler(Vertx vertx) {
  Scheduler scheduler = RxHelper.blockingScheduler(vertx);
  Observable<Long> timer = Observable.interval(100, 100, TimeUnit.MILLISECONDS, scheduler);
}
 
Example 11
public static void main(String args[]) {

		Observable<Long> observableInterval = Observable.interval(2, TimeUnit.SECONDS);
		
		PublishSubject<Long> publishSubject = PublishSubject.create();
	    observableInterval.subscribe(publishSubject);

	    publishSubject.subscribe(i -> System.out.println("Observable #1 : "+i));
		addDelay(4000);
		publishSubject.subscribe(i -> System.out.println("Observable #2 : "+i));
		addDelay(10000); 
		
	}
 
Example 12
Source Project: Tangram-Android   File: TimerSupport.java    License: MIT License 2 votes vote down vote up
/**
 * return tick observable for each user, user should handle the observable with cell's lifecycle
 * @param interval timer interval, in TimeUnit.SECOND
 * @param intermediate true, get event immediately
 * @return
 * @since 3.0.0
 */
public Observable<Long> getTickObservable(int interval, boolean intermediate) {
    return Observable.interval(intermediate ? 0 : interval, interval, TimeUnit.SECONDS);
}
 
Example 13
Source Project: Tangram-Android   File: TimerSupport.java    License: MIT License 2 votes vote down vote up
/**
 * return tick observable for each user, user should handle the observable with cell's lifecycle
 * @param interval timer interval, in TimeUnit.SECOND
 * @param intermediate true, get event immediately
 * @return
 * @since 3.0.0
 */
public Observable<Long> getTickObservable(int interval, boolean intermediate) {
    return Observable.interval(intermediate ? 0 : interval, interval, TimeUnit.SECONDS);
}