Java Code Examples for io.reactivex.subjects.ReplaySubject#subscribe()

The following examples show how to use io.reactivex.subjects.ReplaySubject#subscribe() . 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: ObservableTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 6 votes vote down vote up
@Test
void replaySubject_conf_test() {
    //ReplaySubject<Object> replaySubject = ReplaySubject.createWithSize(5);//只缓存订阅前最后5条数据
    //ReplaySubject<Object> replaySubject = ReplaySubject.createWithTime(5,TimeUnit.SECONDS, Schedulers.computation()); //只缓存被订阅前5秒内的数据
    ReplaySubject<Object> replaySubject =ReplaySubject.createWithTimeAndSize(5,
            TimeUnit.SECONDS, Schedulers.computation(),3); // 请结合以上两者注释
    for (Long i=1l;i<10l;i++){
        replaySubject.onNext(i);
        sleep(1,TimeUnit.SECONDS);
    }

    replaySubject.subscribe(x -> log("一郎神: " + x),
            Throwable::printStackTrace,
            () -> System.out.println("Emission completed"),
            disposable -> System.out.println("onSubscribe")
    );
    replaySubject.onNext(10l);
    replaySubject.onComplete();

}
 
Example 2
Source File: ReplaySubjectExampleActivity.java    From RxJava2-Android-Samples with Apache License 2.0 6 votes vote down vote up
private void doSomeWork() {

        ReplaySubject<Integer> source = ReplaySubject.create();

        source.subscribe(getFirstObserver()); // it will get 1, 2, 3, 4

        source.onNext(1);
        source.onNext(2);
        source.onNext(3);
        source.onNext(4);
        source.onComplete();

        /*
         * it will emit 1, 2, 3, 4 for second observer also as we have used replay
         */
        source.subscribe(getSecondObserver());

    }
 
Example 3
Source File: ObservableTest.java    From Java-programming-methodology-Rxjava-articles with Apache License 2.0 5 votes vote down vote up
@Test
void replaySubject_test() {
    ReplaySubject<Object> replaySubject = ReplaySubject.create();
    replaySubject.onNext(1l);
    replaySubject.onNext(2l);
    replaySubject.subscribe(x -> log("一郎神: " + x),
            Throwable::printStackTrace,
            () -> System.out.println("Emission completed"),
            disposable -> System.out.println("onSubscribe")
    );
    replaySubject.onNext(10l);
    replaySubject.onComplete();

}
 
Example 4
Source File: ExampleUnitTest.java    From RxAndroid-Sample with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubjectExample() {

    /*
     * Step 1: Create an observable that emits an integer
     * from 1 to 5
     *  */
    Observable<Integer> observable = Observable.range(1, 5)
            .subscribeOn(Schedulers.io());

    /*
     * Step 2: Create a subject that observes
     * this emission from the observable. In this scenario,
     * the subject acts an an observer since it observes the
     * changes to the Observable.
     *  */
    ReplaySubject<Integer> subject = ReplaySubject.create();
    observable.subscribe(subject);


    /*
     * Step 3: In this scenario, the subject acts an an Observable,
     * since it emits each item from the original Observable.
     *  */
    subject.subscribe(s -> System.out.println("subscriber one: " + s));
    subject.subscribe(s -> System.out.println("subscriber two: " + s));
}
 
Example 5
Source File: ExampleUnitTest.java    From RxAndroid-Sample with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubjectMulticastExample() {

    /*
     * Step 1: Create an observable that emits an integer
     * from 1 to 5. Each item is squared by itself before it is
     * emitted.
     *  */
    Observable<Integer> observable = Observable.range(1, 5)
            .subscribeOn(Schedulers.io())
            .map(integer -> {
                System.out.println(String.format("Squaring %d with itself", integer));
                return integer * integer;
            });

    /*
     * Step 2: Create a subject that observes
     * this emission from the observable.
     *  */
    ReplaySubject<Integer> subject = ReplaySubject.create();
    observable.subscribe(subject);


    /*
     * Step 3: We are subscribing two subscribers to the Subject.
     *  */
    subject.subscribe(s -> System.out.println("subscriber one: " + s));
    subject.subscribe(s -> System.out.println("subscriber two: " + s));
}
 
Example 6
Source File: Demo_ReplaySubject.java    From Reactive-Programming-With-Java-9 with MIT License 4 votes vote down vote up
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Observer<Long> observer=new Observer<Long>() {

		@Override
		public void onComplete() {
			// TODO Auto-generated method stub
			System.out.println("It's Done");
			
		}

		@Override
		public void onError(Throwable throwable) {
			// TODO Auto-generated method stub
			throwable.printStackTrace();
			
		}

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

		@Override
		public void onSubscribe(Disposable disposable) {
			// TODO Auto-generated method stub
			System.out.println("onSubscribe");
			
		}
	};
	
	ReplaySubject<Long> replaySubject=ReplaySubject.create();
	replaySubject.onNext(1l);
	replaySubject.onNext(2l);
	replaySubject.subscribe(observer);
	replaySubject.onNext(10l);
	replaySubject.onComplete();
	

}