rx.observables.ConnectableObservable Java Examples

The following examples show how to use rx.observables.ConnectableObservable. 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: MultipleSubscribersHotObs.java    From tutorials with MIT License 6 votes vote down vote up
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 #2
Source File: PassWordInputLayout.java    From AndroidProjects with MIT License 6 votes vote down vote up
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 #3
Source File: ScanReplay.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: ScanReplay.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: ScanReplay.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: ConnectableObservableIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 #7
Source File: ConnectableObservableImpl.java    From tutorials with MIT License 5 votes vote down vote up
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 #8
Source File: MultipleSubscribersHotObs.java    From tutorials with MIT License 5 votes vote down vote up
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: MultipleSubscribersColdObs.java    From tutorials with MIT License 5 votes vote down vote up
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 #10
Source File: Application.java    From chrome-native-messaging-java with MIT License 5 votes vote down vote up
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 #11
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay() {
    return OperatorReplay.create(this);
}
 
Example #12
Source File: ConnectableObservableTestRunner.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
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));
}
 
Example #13
Source File: ConnectableObservableTestRunner.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
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 #14
Source File: RxHolder.java    From mv2m with Apache License 2.0 4 votes vote down vote up
public ObservableWithObserver(ConnectableObservable<T> observable) {
    this.observable = observable;
}
 
Example #15
Source File: StoriesFragment.java    From PhilHackerNews with MIT License 4 votes vote down vote up
@Override
protected ConnectableObservable<List<Item>> makeConnectableRepositoryObservable() {
    return mStoryRepository.loadTopStories().publish();
}
 
Example #16
Source File: CommentsFragment.java    From PhilHackerNews with MIT License 4 votes vote down vote up
@Override
protected ConnectableObservable<List<Item>> makeConnectableRepositoryObservable() {
    return mCommentRepository.getCommentsForStoryObservable(mStory).publish();
}
 
Example #17
Source File: JobScenarioBuilder.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
public JobScenarioBuilder(EmbeddedTitusOperations titusOperations,
                          JobsScenarioBuilder jobsScenarioBuilder,
                          String jobId,
                          SchedulingService<? extends TaskRequest> schedulingService,
                          DiagnosticReporter diagnosticReporter) {
    this.client = titusOperations.getV3GrpcClient();
    this.titusOperations = titusOperations;
    this.jobsScenarioBuilder = jobsScenarioBuilder;
    this.jobId = jobId;
    this.schedulingService = schedulingService;
    this.diagnosticReporter = diagnosticReporter;

    // FIXME Job is not made immediately visible after it is accepted by reconciliation framework
    rethrow(() -> Thread.sleep(1000));

    TestStreamObserver<JobChangeNotification> jobEvents = new TestStreamObserver<>();
    ConnectableObservable<JobChangeNotification> connectableEventStream = jobEvents.toObservable()
            .doOnNext(event -> logger.info("Received job change notification: {}", event))
            .replay();

    connectableEventStream.filter(e -> e.getNotificationCase() == NotificationCase.JOBUPDATE)
            .map(n -> toCoreJob(n.getJobUpdate().getJob()))
            .subscribe(jobEventStream);

    connectableEventStream.filter(e -> e.getNotificationCase() == NotificationCase.TASKUPDATE)
            .map(event -> event.getTaskUpdate().getTask())
            .subscribe(
                    grpcTask -> {
                        Task coreTask = GrpcJobManagementModelConverters.toCoreTask(getJob(), grpcTask);
                        String taskId = coreTask.getId();
                        TaskHolder taskHolder = taskHolders.get(taskId);
                        if (taskHolder == null) {
                            Optional<String> resubmitOfOpt = coreTask.getResubmitOf();
                            Integer slot = null;
                            if (resubmitOfOpt.isPresent()) {
                                String resubmitOf = resubmitOfOpt.get();
                                slot = taskToSlot.get(resubmitOf);
                            }
                            if (slot == null) {
                                slot = nextIndex++;
                            }
                            taskSlotIndexes.put(slot, taskId);
                            taskToSlot.put(taskId, slot);
                            taskHolders.put(taskId, taskHolder = new TaskHolder());
                        }
                        taskHolder.onNext(coreTask);
                    },
                    e -> logger.error("Task event stream in job {} terminated with an error", jobId, e),
                    () -> logger.info("Task event stream in job {} completed", jobId)
            );
    Observable<JobChangeNotification> snapshotMarker = connectableEventStream.filter(e -> e.getNotificationCase() == NotificationCase.SNAPSHOTEND);

    this.eventStreamSubscription = connectableEventStream.connect();
    client.observeJob(JobId.newBuilder().setId(jobId).build(), jobEvents);

    snapshotMarker.take(1).timeout(TIMEOUT_MS, TimeUnit.MILLISECONDS).toBlocking().first();
}
 
Example #18
Source File: Sensor.java    From oneops with Apache License 2.0 4 votes vote down vote up
/**
 * Loading sensor statements and seeding heartbeat and perf events.
 * The current logic implemented using Rx Observable is,
 * <lo>
 * <li> Get a stream of all the Ci OpenEvents from cassandra
 * <li> Filter the events with proper sensor instance id.
 * <li> Bifurcate ops event stream into heartbeat and metric.
 * <li> Load all sensor statements and create a stream of fake events curresponding to heartbeat thresholds.
 * <li> Start processing all three streams in parallel (with a timeout of max 30 mins)
 * <li> Once the stream processing is complete, start seeding esper to restore the state for heart beat
 * (MHB) and open hb thresholds (RMHB)
 * </lo>
 * <p>
 * <p>
 * <pre>
 * <--Perf Events-->  <-- 5m (a) -->     <---- 15m (b) ---->       <---- 15m (b) ---->
 * o-o-o-o-o-o-o-o-o-o|.............|MHB|....................|RMHB|....................|RMHB|
 * </pre>
 * <p>
 * MHB -  Missing heartbeat
 * RMHB - Retrigger MHB
 * <p>
 */
private void loadAllStatements() throws InterruptedException {

    initDefaultStatements();

    // Bifurcate ops events stream into heartbeat and metric.
    ConnectableObservable<OpsEvent> openEvents = getAllOpenEvents().publish();
    Observable<OpsEvent> hbeat = openEvents.filter(e -> HEARTBEAT.equals(e.getType()));
    Observable<OpsEvent> metric = openEvents.filter(e -> METRIC.equals(e.getType()));


    // Subscribe to all three streams.
    final CountDownLatch lock = new CountDownLatch(3);
    final List<FakeEvent> fes = new ArrayList<>();
    final Map<String, OpsEvent> hbOpenEvents = new HashMap<>();

    loadThresholds().subscribeOn(Schedulers.io())
            .subscribe(fes::add, this::handleError,
                    () -> {
                        logger.info("Loading threshold statements completed!");
                        lock.countDown();
                    });

    hbeat.subscribeOn(Schedulers.io())
            .subscribe((he) -> hbOpenEvents.put(he.getCiId() + he.getSource(), he),
                    this::handleError,
                    () -> {
                        logger.info("Loading Heartbeat OpsEvents completed!");
                        lock.countDown();
                    });

    metric.subscribeOn(Schedulers.io())
            .subscribe(this::sendOpsEvent,
                    this::handleError,
                    () -> {
                        logger.info("Loading Metric OpsEvents completed!");
                        lock.countDown();
                    });

    // Starts the pipeline and wait for it to complete processing
    logger.info("Starting stream processing pipeline...");
    openEvents.connect();
    logger.info("Waiting to complete the OpsEvent stream processing.");
    lock.await(getLoadStatementTimeOut(), TimeUnit.MINUTES);

    // Finally insert the fake events to satisfy the open hb conditions.
    fes.stream().forEach(fe -> {
        OpsEvent event = hbOpenEvents.get(fe.ciId + fe.source);
        if (event != null) {
            // If there is an open hb event lets just reinsert it so hb event will gets retriggered if no metrics coming in.
            logger.info("Seeding OpenHbEvent(ciId = " + event.getCiId() + ", manifestId = " + event.getManifestId() + ", name = "
                    + event.getName() + ", state = " + event.getState() + ")");
            this.epService.getEPRuntime().sendEvent(event);
        } else {
            // If there is no open heartbeat event lets insert fake perf event to seed hb threshold.
            logger.info("Seeding PerfEvent(ciId = " + fe.ciId + ", manifestId = " + fe.manifestId + ", source = " + fe.source + ")");
            int delay = random.nextInt(heartbeatRandomDelay);
            insertFakeEventWithDelay(fe.ciId, fe.manifestId, fe.source, delay);
        }

    });

    initChannelDownStatement();
}
 
Example #19
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay(Scheduler scheduler) {
    return OperatorReplay.observeOn(replay(), scheduler);
}
 
Example #20
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay(long time, TimeUnit unit, Scheduler scheduler) {
    return OperatorReplay.create(this, time, unit, scheduler);
}
 
Example #21
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay(long time, TimeUnit unit) {
    return replay(time, unit, Schedulers.computation());
}
 
Example #22
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay(int bufferSize, Scheduler scheduler) {
    return OperatorReplay.observeOn(replay(bufferSize), scheduler);
}
 
Example #23
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay(int bufferSize, long time, TimeUnit unit, Scheduler scheduler) {
    if (bufferSize >= 0) {
        return OperatorReplay.create(this, time, unit, scheduler, bufferSize);
    }
    throw new IllegalArgumentException("bufferSize < 0");
}
 
Example #24
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay(int bufferSize, long time, TimeUnit unit) {
    return replay(bufferSize, time, unit, Schedulers.computation());
}
 
Example #25
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> replay(int bufferSize) {
    return OperatorReplay.create(this, bufferSize);
}
 
Example #26
Source File: Observable.java    From letv with Apache License 2.0 4 votes vote down vote up
public final ConnectableObservable<T> publish() {
    return OperatorPublish.create(this);
}
 
Example #27
Source File: RxTest.java    From usergrid with Apache License 2.0 3 votes vote down vote up
@Test
@Category(ExperimentalTest.class )
public void testConnectableObserver() throws InterruptedException {

    final int count = 10;

    final CountDownLatch latch = new CountDownLatch( count );

    final ConnectableObservable<Integer> connectedObservable = Observable.range( 0, count ).publish();


    //connect to our latch, which should run on it's own subscription
    //start our latch running
    connectedObservable.doOnNext( integer -> latch.countDown() ).subscribeOn( Schedulers.io() ).subscribe();


    final Observable<Integer> countObservable = connectedObservable.subscribeOn( Schedulers.io() ).count();

    //start the sequence
    connectedObservable.connect();


    final boolean completed = latch.await( 5, TimeUnit.SECONDS );

    assertTrue( "publish1 behaves as expected", completed );

    final int returnedCount = countObservable.toBlocking().last();

    assertEquals( "Counts the same", count, returnedCount );
}
 
Example #28
Source File: RefreshableListRepositoryFragment.java    From PhilHackerNews with MIT License votes vote down vote up
protected abstract ConnectableObservable<List<Item>> makeConnectableRepositoryObservable();