當前位置: 首頁>>代碼示例>>Java>>正文


Java Observable.subscribe方法代碼示例

本文整理匯總了Java中io.reactivex.Observable.subscribe方法的典型用法代碼示例。如果您正苦於以下問題:Java Observable.subscribe方法的具體用法?Java Observable.subscribe怎麽用?Java Observable.subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.reactivex.Observable的用法示例。


在下文中一共展示了Observable.subscribe方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: SignallerExample

import io.reactivex.Observable; //導入方法依賴的package包/類
/**
   * For example, if one needs a signaller-like source,
   * a shared enum can be defined and its solo instance onNext'd:
   */
  @Test
  public void SignallerExample() {
    Observable<Object> source = Observable.create((ObservableEmitter<Object> emitter) -> {
      System.out.println("Side-effect 1");
      emitter.onNext(Irrelevant.INSTANCE);

      System.out.println("Side-effect 2");
      emitter.onNext(Irrelevant.INSTANCE);

      System.out.println("Side-effect 3");
//            emitter.onNext(Irrelevant.INSTANCE);
    });

    source.subscribe(e -> { /* Ignored. */ }, Throwable::printStackTrace);
  }
 
開發者ID:jimandreas,項目名稱:android-rxmvp-sandbox,代碼行數:20,代碼來源:RxJava.java

示例2: test_just_new

import io.reactivex.Observable; //導入方法依賴的package包/類
@Test
public void test_just_new() {
	Observable<Integer> observable = Observable.just(12, 34, 6);
	TestObserver<Integer> testObserver = new TestObserver<>();

	observable.subscribe(testObserver);

	List<Integer> list = new ArrayList();
	testObserver.assertComplete();
	testObserver.assertResult(12, 34, 6);
	testObserver.assertValueCount(3);
	testObserver.assertNoErrors();
	testObserver.assertValueAt(2, (value) -> {
		// TODO Auto-generated method stub
		return value == 34;
	});

}
 
開發者ID:PacktPublishing,項目名稱:Reactive-Programming-With-Java-9,代碼行數:19,代碼來源:Modern_Testing.java

示例3: persistObservableToJobFile

import io.reactivex.Observable; //導入方法依賴的package包/類
private Disposable persistObservableToJobFile(JobId jobId, String jobFilename, Observable<byte[]> o) {
    if (!resolveJobDir(jobId).isPresent())
        throw new RuntimeException(jobId + ": cannot persist stdout: job dir does not exist");

    final Path stdoutPath = resolveJobDir(jobId).get().resolve(jobFilename);

    final AtomicReference<FileOutputStream> outputStream =
            new AtomicReference<>(null);
    return o.subscribe(
            bytes -> {
                if (outputStream.get() == null)
                    outputStream.set(new FileOutputStream(stdoutPath.toFile()));
                outputStream.get().write(bytes);
            },
            error -> {
                if (outputStream.get() != null)
                    outputStream.get().close();
            },
            () -> {
                if (outputStream.get() != null)
                    outputStream.get().close();
            });
}
 
開發者ID:adamkewley,項目名稱:jobson,代碼行數:24,代碼來源:FilesystemJobsDAO.java

示例4: main

import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {
    Observable<String> source = Observable.create(emitter -> {
        try {
            emitter.onNext("Alpha");
            emitter.onNext("Beta");
            emitter.onNext("Gamma");
            emitter.onNext("Delta");
            emitter.onNext("Epsilon");
            emitter.onComplete();
        } catch (Throwable e) {
            emitter.onError(e);
        }
    });
    source.subscribe(s -> System.out.println("RECEIVED: " + s),
            Throwable::printStackTrace);
}
 
開發者ID:PacktPublishing,項目名稱:Learning-RxJava,代碼行數:17,代碼來源:Ch2_2.java

示例5: test3

import io.reactivex.Observable; //導入方法依賴的package包/類
@Override
public void test3() {
    Log.i(TAG, "test3() Create simple demo, onNext() twice");
    Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
            for (int i = 0; i < 3; i++) {
                e.onNext(String.valueOf(i));
            }
        }
    });
    for (int time = 0; time < 2; time++) {
        observable.subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.d(TAG, "Consumer<String> accept() s: " + s);
            }
        });
    }
}
 
開發者ID:afunx,項目名稱:RxJava4AndroidDemos,代碼行數:21,代碼來源:Create.java

示例6: querySubscribedToDuringTransactionThrows

import io.reactivex.Observable; //導入方法依賴的package包/類
@Test public void querySubscribedToDuringTransactionThrows() {
  Observable<Query> query = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES);

  db.newTransaction();
  query.subscribe(o);
  o.assertErrorContains("Cannot subscribe to observable query in a transaction.");
}
 
開發者ID:jiechic,項目名稱:sqlbrite-sqlcipher,代碼行數:8,代碼來源:BriteDatabaseTest.java

示例7: main

import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {

    List<String> data = Arrays.asList("foo", "bar", "baz");
    Random random = new Random();
    Observable<String> source = Observable.create(subscriber -> {
      for (String s : data) {
        if (random.nextInt(6) == 0) {
          subscriber.onError(new RuntimeException("Bad luck for you..."));
        }
        subscriber.onNext(s);
      }
      subscriber.onComplete();
    });

    for (int i = 0; i < 10; i++) {
      logger.info("=======================================");
      source.subscribe(next -> logger.info("Next: {}", next),
        error -> logger.error("Whoops"),
        () -> logger.info("Done"));
    }

    logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    source
      .retry(5)
      .subscribe(next -> logger.info("Next: {}", next),
        error -> logger.error("Whoops"),
        () -> logger.info("Done"));
  }
 
開發者ID:jponge,項目名稱:oracle-javamag-vertx-rxjava,代碼行數:29,代碼來源:RxCreateObservable.java

示例8: main

import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Observable<Long> observable=Observable.timer(1,TimeUnit.SECONDS,Schedulers.computation()).flatMap(i->Observable.just(12l,13l,300l,400l));
	observable.subscribe(new Observer<Long>() {

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

		@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 arg0) {
			// TODO Auto-generated method stub
			System.out.println("subscribe");
			
		}
	});

	try {
		Thread.sleep(80000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
開發者ID:PacktPublishing,項目名稱:Reactive-Programming-With-Java-9,代碼行數:41,代碼來源:DemoTimer.java

示例9: shouldGetPreviousShifts

import io.reactivex.Observable; //導入方法依賴的package包/類
@Test
public void shouldGetPreviousShifts() throws Exception {
    shiftService.startAShift(shift);
    shiftService.endAShift(shift);

    Observable<List<FinishedShift>> observable = shiftService.getPreviousShifts();
    TestObserver<List<FinishedShift>> testObserver = new TestObserver<>();
    observable.subscribe(testObserver);
    testObserver.assertNoErrors();
}
 
開發者ID:Ericliu001,項目名稱:RosterManager,代碼行數:11,代碼來源:ItemListActivityTest.java

示例10: queryOnlyNotifiedAfterSubscribe

import io.reactivex.Observable; //導入方法依賴的package包/類
@Test public void queryOnlyNotifiedAfterSubscribe() {
  Observable<Query> query = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES);
  o.assertNoMoreEvents();

  db.insert(TABLE_EMPLOYEE, employee("john", "John Johnson"));
  o.assertNoMoreEvents();

  query.subscribe(o);
  o.assertCursor()
      .hasRow("alice", "Alice Allison")
      .hasRow("bob", "Bob Bobberson")
      .hasRow("eve", "Eve Evenson")
      .hasRow("john", "John Johnson")
      .isExhausted();
}
 
開發者ID:jiechic,項目名稱:sqlbrite-sqlcipher,代碼行數:16,代碼來源:BriteDatabaseTest.java

示例11: returnAValue

import io.reactivex.Observable; //導入方法依賴的package包/類
@Test
public void returnAValue(){
    result = "";
    Observable<String> observer = Observable.just("Hello"); // provides datea
    observer.subscribe(s -> result=s); // Callable as subscriber
    assertTrue(result.equals("Hello"));
}
 
開發者ID:vogellacompany,項目名稱:code-examples-android-expert,代碼行數:8,代碼來源:RxJavaUnitTest.java

示例12: main

import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {
        Observable<String> source =
                Observable.just("Alpha", "Beta", "Gamma",
                        "Delta", "Epsilon")
                        .replay(1)
                        .autoConnect();
//Observer 1
        source.subscribe(l -> System.out.println("Observer 1: " +
                l));
//Observer 2
        source.subscribe(l -> System.out.println("Observer 2: " +
                l));
    }
 
開發者ID:PacktPublishing,項目名稱:Learning-RxJava,代碼行數:14,代碼來源:Ch5_17.java

示例13: main

import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {
	// TODO Auto-generated method stub
	
	Observable<Long>observable=Observable.rangeLong(1l,3l);
	Observer< Long> observer=new Observer<Long>() {

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

		@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(disposable.isDisposed());
			disposable.dispose();
			
		}
	};
	observable.subscribe(observer);
	
}
 
開發者ID:PacktPublishing,項目名稱:Reactive-Programming-With-Java-9,代碼行數:39,代碼來源:MyObserver.java

示例14: test_just

import io.reactivex.Observable; //導入方法依賴的package包/類
@Test
public void test_just() {
	Observable<Integer> observable = Observable.just(12, 34, 6);
	ArrayList<Integer> list = new ArrayList();
	observable.subscribe(item -> list.add(item));

	assertNotNull(list);
	assertEquals(3, list.size());
	assertEquals(6, list.get(2).intValue());
}
 
開發者ID:PacktPublishing,項目名稱:Reactive-Programming-With-Java-9,代碼行數:11,代碼來源:Traditional_Testing.java

示例15: test4

import io.reactivex.Observable; //導入方法依賴的package包/類
@Override
public void test4() {
    Log.i(TAG, "test4() Create simple demo, onNext() and onComplete() twice");
    Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
            for (int i = 0; i < 3; i++) {
                e.onNext(String.valueOf(i));
            }
            e.onComplete();
        }
    });
    for (int time = 0; time < 2; time++) {
        observable.subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.d(TAG, "Consumer<String> accept() s: " + s);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Log.d(TAG, "Consumer<Throwable> accept() throwable: " + throwable);
            }
        }, new Action() {
            @Override
            public void run() throws Exception {
                Log.d(TAG, "Action run() for onComplete()");
            }
        });
    }
}
 
開發者ID:afunx,項目名稱:RxJava4AndroidDemos,代碼行數:32,代碼來源:Create.java


注:本文中的io.reactivex.Observable.subscribe方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。