当前位置: 首页>>代码示例>>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;未经允许,请勿转载。