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

The following examples show how to use io.reactivex.Observable#interval() . 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 check out the related API usage on the sidebar.
Example 1
Source File: Code05.java    From rxjava2-lab with 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
Source File: RxJavaDemoTest.java    From reactive-streams-in-java with Apache License 2.0 6 votes vote down vote up
@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
Source File: ExampleUnitTest.java    From RxAndroid-Sample with Apache License 2.0 5 votes vote down vote up
@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
Source File: DemoCreate_Observables.java    From Reactive-Programming-With-Java-9 with MIT License 5 votes vote down vote up
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 File: RxJava2.java    From RHub with 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
Source File: RxJavaHotObservable2.java    From Spring-5.0-Projects with MIT License 4 votes vote down vote up
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 File: LifeCycleHelperTest.java    From Tangram-Android with 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 File: ResponseQueueTest.java    From ocraft-s2client with 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
Source File: IntervalExampleActivity.java    From RxJava2-Android-Samples with Apache License 2.0 4 votes vote down vote up
private Observable<? extends Long> getObservable() {
    return Observable.interval(0, 2, TimeUnit.SECONDS);
}
 
Example 10
Source File: NativeExamples.java    From vertx-rx with 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
Source File: RxJavaHotObservable1.java    From Spring-5.0-Projects with MIT License 3 votes vote down vote up
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 File: TimerSupport.java    From Tangram-Android with 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 File: TimerSupport.java    From Tangram-Android with 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);
}