Java Code Examples for rx.observables.ConnectableObservable#subscribe()
The following examples show how to use
rx.observables.ConnectableObservable#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: PassWordInputLayout.java From AndroidProjects with MIT License | 6 votes |
private void addTextListeners(final EditText et) { final ConnectableObservable<String> textSource = RxTextView .textChanges(et) .skip(1) .map(CharSequence::toString) .publish(); addSpaceHandler(et); final Subscription suggestionSub = textSource.filter(input -> input.length() > 0) .flatMap(this::getWordSuggestion) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(this::handleWordSuggestion, Throwable::printStackTrace); final Subscription uiSub = textSource .subscribe(this::updateUi, Throwable::printStackTrace); final Subscription connectSub = textSource.connect(); this.subscriptions.addAll(suggestionSub, uiSub, connectSub); }
Example 2
Source File: ScanReplay.java From akarnokd-misc with Apache License 2.0 | 6 votes |
@Test public void testExpectedReplayBehavior() { final TestScheduler scheduler = new TestScheduler(); final TestSubject<Integer> subject = TestSubject.create(scheduler); final TestSubscriber<Integer> subscriber = new TestSubscriber<>(); final ConnectableObservable<Integer> sums = subject.scan((a, b) -> a + b).replay(1); sums.connect(); subject.onNext(1); subject.onNext(2); subject.onNext(3); scheduler.triggerActions(); sums.subscribe(subscriber); subscriber.assertValueCount(1); subscriber.assertValues(6); }
Example 3
Source File: ScanReplay.java From akarnokd-misc with Apache License 2.0 | 6 votes |
@Test public void testFlakyReplayBehavior() { final TestScheduler scheduler = new TestScheduler(); final TestSubject<Integer> subject = TestSubject.create(scheduler); final TestSubscriber<Integer> subscriber = new TestSubscriber<>(); final ConnectableObservable<Integer> sums = subject.scan(1, (a, b) -> a + b).replay(1); sums.connect(); subject.onNext(2); subject.onNext(3); scheduler.triggerActions(); sums.subscribe(subscriber); // subscriber.assertValueCount(1); subscriber.assertValues(6); }
Example 4
Source File: ScanReplay.java From akarnokd-misc with Apache License 2.0 | 6 votes |
@Test public void testFlakyReplayBehavior2() { final PublishSubject<Integer> subject = PublishSubject.create(); final TestSubscriber<Integer> subscriber = new TestSubscriber<>(); final ConnectableObservable<Integer> sums = subject.scan(1, (a, b) -> a + b).replay(1); sums.connect(); subject.onNext(2); subject.onNext(3); sums.subscribe(subscriber); // subscriber.assertValueCount(1); subscriber.assertValues(6); }
Example 5
Source File: MultipleSubscribersHotObs.java From tutorials with MIT License | 6 votes |
public static void subscribeBeforeConnect() throws InterruptedException { ConnectableObservable obs = getObservable().publish(); LOGGER.info("subscribing #1"); Subscription subscription1 = obs.subscribe((i) -> LOGGER.info("subscriber#1 is printing x-coordinate " + i)); Thread.sleep(1000); LOGGER.info("subscribing #2"); Subscription subscription2 = obs.subscribe((i) -> LOGGER.info("subscriber#2 is printing x-coordinate " + i)); Thread.sleep(1000); LOGGER.info("connecting:"); Subscription s = obs.connect(); Thread.sleep(1000); LOGGER.info("unsubscribe connected"); s.unsubscribe(); }
Example 6
Source File: Application.java From chrome-native-messaging-java with MIT License | 5 votes |
private ConnectableObservable<String> getObservable() { ConnectableObservable<String> observable = Observable .create(new Observable.OnSubscribe<String>() { public void call(Subscriber<? super String> subscriber) { subscriber.onStart(); try { while (true) { String _s = readMessage(System.in); subscriber.onNext(_s); } } catch (InterruptedIOException ioe) { log("Blocked communication"); } catch (Exception e) { subscriber.onError(e); } subscriber.onCompleted(); } }).subscribeOn(Schedulers.io()).publish(); observable.subscribe(new Observer<String>() { public void onCompleted() { log("App closed."); interrompe.set(true); } public void onError(Throwable throwable) { log("Unexpected error!"); interrompe.set(true); } public void onNext(String s) { } }); return observable; }
Example 7
Source File: MultipleSubscribersColdObs.java From tutorials with MIT License | 5 votes |
private static void subscribeBeforeConnect() throws InterruptedException { ConnectableObservable obs = getObservable().publish(); LOGGER.info("Subscribing"); obs.subscribe(i -> LOGGER.info("subscriber #1 is printing " + i)); obs.subscribe(i -> LOGGER.info("subscriber #2 is printing " + i)); Thread.sleep(1000); LOGGER.info("Connecting"); Subscription s = obs.connect(); s.unsubscribe(); }
Example 8
Source File: MultipleSubscribersHotObs.java From tutorials with MIT License | 5 votes |
public static void connectBeforeSubscribe() throws InterruptedException { ConnectableObservable obs = getObservable().doOnNext(x -> LOGGER.info("saving " + x)).publish(); LOGGER.info("connecting:"); Subscription s = obs.connect(); Thread.sleep(1000); LOGGER.info("subscribing #1"); obs.subscribe((i) -> LOGGER.info("subscriber#1 is printing x-coordinate " + i)); Thread.sleep(1000); LOGGER.info("subscribing #2"); obs.subscribe((i) -> LOGGER.info("subscriber#2 is printing x-coordinate " + i)); Thread.sleep(1000); s.unsubscribe(); }
Example 9
Source File: ConnectableObservableImpl.java From tutorials with MIT License | 5 votes |
public static void main(String[] args) throws InterruptedException { ConnectableObservable<Long> connectable = Observable.interval(200, TimeUnit.MILLISECONDS).publish(); connectable.subscribe(System.out::println); System.out.println("Connect"); connectable.connect(); Thread.sleep(500); System.out.println("Sleep"); }
Example 10
Source File: ConnectableObservableIntegrationTest.java From tutorials with MIT License | 5 votes |
@Test public void givenConnectableObservable_whenConnect_thenGetMessage() throws InterruptedException { String[] result = {""}; ConnectableObservable<Long> connectable = Observable.interval(500, TimeUnit.MILLISECONDS).publish(); connectable.subscribe(i -> result[0] += i); assertFalse(result[0].equals("01")); connectable.connect(); await() .until(() -> assertTrue(result[0].equals("01"))); }
Example 11
Source File: ConnectableObservableTestRunner.java From pinpoint with Apache License 2.0 | 4 votes |
public void connectableObservable() throws Exception { final int numSubscribers = 2; final List<String> messages = Arrays.asList("Hello", "World"); final List<String> actualMessages = Collections.synchronizedList(new ArrayList<String>()); final CountDownLatch completeLatch = new CountDownLatch(numSubscribers); final Action1<String> onNext = new Action1<String>() { @Override public void call(String s) { actualMessages.add(s); } }; final Action1<Throwable> onError = new Action1<Throwable>() { @Override public void call(Throwable throwable) { completeLatch.countDown(); } }; final Action0 onCompleted = new Action0() { @Override public void call() { completeLatch.countDown(); } }; ConnectableObservable<String> echoes = echoesService.echo(messages) .subscribeOn(Schedulers.computation()) .publish(); for (int i = 0; i < numSubscribers; i++) { echoes.subscribe(onNext, onError, onCompleted); } echoes.connect(); completeLatch.await(500L, TimeUnit.MILLISECONDS); for (int i = 0; i < actualMessages.size(); i++) { String expectedMessage = messages.get(i / numSubscribers); String actualMessage = actualMessages.get(i); Assert.assertEquals(expectedMessage, actualMessage); } TestHelper.awaitForSpanDataFlush(); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); // Skip rx java internal traces as they differ between versions and it's too much work to split the tests. // Instead, we can verify them indirectly by checking if user methods have been traced. verifier.ignoreServiceType("RX_JAVA_INTERNAL"); Method publishMethod = Observable.class.getDeclaredMethod("publish"); verifier.verifyTrace(event("RX_JAVA", publishMethod)); // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) X numSubscribers Method subscribeMethod = Observable.class.getDeclaredMethod("subscribe", Action1.class, Action1.class, Action0.class); for (int i = 0; i < numSubscribers; i++) { verifier.verifyTrace(event("RX_JAVA", subscribeMethod)); } Method connectMethod = ConnectableObservable.class.getDeclaredMethod("connect"); verifier.verifyTrace(event("RX_JAVA", connectMethod)); // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) for scheduling a single connectable observable verifier.verifyTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation")); // event - RX_JAVA_INTERNAL some form of Action0 implementation's call() inside OperatorSubscribeOn that gets scheduled Method echoMethod = EchoRepository.class.getDeclaredMethod("echo", String.class); for (int i = 0; i < numSubscribers; i++) { verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), echoMethod)); } }
Example 12
Source File: ConnectableObservableTestRunner.java From pinpoint with Apache License 2.0 | 4 votes |
public void connectableObservableError() throws Exception { final int numSubscribers = 3; final List<String> messages = Arrays.asList("Hello", "World"); final Exception expected = new RuntimeException("expected"); final CountDownLatch completeLatch = new CountDownLatch(numSubscribers); final List<Exception> actualExceptions = Collections.synchronizedList(new ArrayList<Exception>()); final Action1<String> onNext = new Action1<String>() { @Override public void call(String s) { // ignore } }; final Action1<Throwable> onError = new Action1<Throwable>() { @Override public void call(Throwable throwable) { actualExceptions.add((Exception) throwable); completeLatch.countDown(); } }; final Action0 onCompleted = new Action0() { @Override public void call() { completeLatch.countDown(); } }; ConnectableObservable<String> echoes = echoesService.echo(messages, expected) .subscribeOn(Schedulers.computation()) .publish(); for (int i = 0; i < numSubscribers; i++) { echoes.subscribe(onNext, onError, onCompleted); } echoes.connect(); completeLatch.await(500L, TimeUnit.MILLISECONDS); Assert.assertEquals(numSubscribers, actualExceptions.size()); for (Exception actualException : actualExceptions) { Assert.assertSame(expected, actualException); } TestHelper.awaitForSpanDataFlush(); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); // Skip rx java internal traces as they differ between versions and it's too much work to split the tests. // Instead, we can verify them indirectly by checking if user methods have been traced. verifier.ignoreServiceType("RX_JAVA_INTERNAL"); Method publishMethod = Observable.class.getDeclaredMethod("publish"); verifier.verifyTrace(event("RX_JAVA", publishMethod)); // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) X numSubscribers Method subscribeMethod = Observable.class.getDeclaredMethod("subscribe", Action1.class, Action1.class, Action0.class); for (int i = 0; i < numSubscribers; i++) { verifier.verifyTrace(event("RX_JAVA", subscribeMethod)); } Method connectMethod = ConnectableObservable.class.getDeclaredMethod("connect"); verifier.verifyTrace(event("RX_JAVA", connectMethod)); // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0) for scheduling a single connectable observable verifier.verifyTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation")); // event - RX_JAVA_INTERNAL some form of Action0 implementation's call() inside OperatorSubscribeOn that gets scheduled Method echoMethod = EchoRepository.class.getDeclaredMethod("echo", String.class, Exception.class); verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), echoMethod, expected)); }