本文整理匯總了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);
}
示例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;
});
}
示例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();
});
}
示例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);
}
示例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);
}
});
}
}
示例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.");
}
示例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"));
}
示例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();
}
}
示例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();
}
示例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();
}
示例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"));
}
示例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));
}
示例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);
}
示例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());
}
示例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()");
}
});
}
}