Java Code Examples for rx.observables.ConnectableObservable#connect()
The following examples show how to use
rx.observables.ConnectableObservable#connect() .
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: 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 7
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 8
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 9
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 10
Source File: Sensor.java From oneops with Apache License 2.0 | 4 votes |
/** * 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 11
Source File: JobScenarioBuilder.java From titus-control-plane with Apache License 2.0 | 4 votes |
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 12
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 13
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)); }
Example 14
Source File: RxTest.java From usergrid with Apache License 2.0 | 3 votes |
@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 ); }