rx.Observable.OnSubscribe Java Examples

The following examples show how to use rx.Observable.OnSubscribe. 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: FileObservable.java    From newts with Apache License 2.0 6 votes vote down vote up
public static Observable<String> unzipLines(final Path path) {
    return Observable.create(new OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> s) {
            try (BufferedReader in = zippedFileReader(path))
            {
                String line;
                while ((line = in.readLine()) != null) {
                    if (s.isUnsubscribed()) return;
                    s.onNext(line);
                }
                s.onCompleted();
                
            } catch (Exception e) {
                if (!s.isUnsubscribed()) s.onError(e);
            }
        }
        
    });
    
}
 
Example #2
Source File: HttpRemotingTransport.java    From astrix with Apache License 2.0 6 votes vote down vote up
@Override
public Observable<AstrixServiceInvocationResponse> submitRoutedRequest(
		final AstrixServiceInvocationRequest request, final RoutingKey routingKey) {
	ClusterMember clusterMember = getTargetMember(routingKey);
	final HttpPost postRequest = new HttpPost(clusterMember.getRemoteEndpointUri());
	postRequest.setEntity(new SerializableEntity(request));
	return Observable.create(new OnSubscribe<AstrixServiceInvocationResponse>() {
		@Override
		public void call(final Subscriber<? super AstrixServiceInvocationResponse> t1) {
			try {
				httpclient.execute(postRequest, serviceResponseCallback(t1));
			} catch (Exception e) {
				t1.onError(e);
			}
		}

	});
}
 
Example #3
Source File: Http.java    From trading-backtest with MIT License 6 votes vote down vote up
public static Observable<HttpResponse> get(String url, Consumer<HttpGet> configureRequest) {
    HttpGet request = new HttpGet(url);
    configureRequest.accept(request);

    return Observable.create(new OnSubscribe<HttpResponse>() {
        @Override public void call(Subscriber<? super HttpResponse> s) {
            try {
                log.debug("GET {}", url);
                s.onNext(getDefaultHttpClient().execute(request));
                s.onCompleted();
            } catch (IOException e) {
                s.onError(e);
            }
        }
    }).subscribeOn(Schedulers.io());
}
 
Example #4
Source File: HttpRemotingTransport.java    From astrix with Apache License 2.0 6 votes vote down vote up
@Override
public Observable<List<AstrixServiceInvocationResponse>> submitBroadcastRequest(
		AstrixServiceInvocationRequest request) {
	Observable<AstrixServiceInvocationResponse> result = Observable.empty();
	for (ClusterMember clusterMember : getAllClusterMembers()) {
		final HttpPost postRequest = new HttpPost(clusterMember.getRemoteEndpointUri());
		postRequest.setEntity(new SerializableEntity(request));
		result = result.mergeWith(Observable.create(new OnSubscribe<AstrixServiceInvocationResponse>() {
			@Override
			public void call(final Subscriber<? super AstrixServiceInvocationResponse> t1) {
				try {
					httpclient.execute(postRequest, serviceResponseCallback(t1));
				} catch (Exception e) {
					t1.onError(e);
				}
			}

		}));
	}
	return result.toList();
}
 
Example #5
Source File: HystrixObservableCommandFacadeTest.java    From astrix with Apache License 2.0 6 votes vote down vote up
@Test
public void throwsServiceUnavailableOnTimeouts() throws Exception {
	pingServer.setResult(Observable.create(new OnSubscribe<String>() {
		@Override
		public void call(Subscriber<? super String> t1) {
			// Simulate timeout by not invoking subscriber
		}
	}));
	try {
		ping.ping().toBlocking().first();
		fail("All ServiceUnavailableException should be thrown on timeout");
	} catch (ServiceUnavailableException e) {
		// Expcected
	}
	eventually(() -> {
		assertEquals(0, getEventCountForCommand(HystrixRollingNumberEvent.SUCCESS));
		assertEquals(1, getEventCountForCommand(HystrixRollingNumberEvent.TIMEOUT));
		assertEquals(0, getEventCountForCommand(HystrixRollingNumberEvent.SEMAPHORE_REJECTED));
	});
}
 
Example #6
Source File: HystrixObservableCommandFacadeTest.java    From astrix with Apache License 2.0 6 votes vote down vote up
@Test
public void semaphoreRejectedCountsAsFailure() throws Exception {
	pingServer.setResult(Observable.create(new OnSubscribe<String>() {
		@Override
		public void call(Subscriber<? super String> t1) {
			// Simulate timeout by not invoking subscriber
		}
	}));
	Observable<String> ftObservable1 = ping.ping();
	Observable<String> ftObservable2 = ping.ping();

	// Subscribe to observables, ignore emitted items/errors
	ftObservable1.subscribe((item) -> {}, (exception) -> {});
	ftObservable2.subscribe((item) -> {}, (exception) -> {});

	eventually(() -> {
		assertEquals(0, getEventCountForCommand(HystrixRollingNumberEvent.SUCCESS));
		assertEquals(1, getEventCountForCommand(HystrixRollingNumberEvent.SEMAPHORE_REJECTED));
	});
}
 
Example #7
Source File: HystrixObservableCommandFacadeTest.java    From astrix with Apache License 2.0 6 votes vote down vote up
@Test
public void doesNotInvokeSupplierWhenBulkHeadIsFull() throws Exception {
	final AtomicInteger supplierInvocationCount = new AtomicInteger();
	Supplier<Observable<String>> timeoutCommandSupplier = new Supplier<Observable<String>>() {
		@Override
		public Observable<String> get() {
			supplierInvocationCount.incrementAndGet();
			return Observable.create(new OnSubscribe<String>() {
				@Override
				public void call(Subscriber<? super String> t1) {
					// Simulate timeout by not invoking subscriber
				}
			});
		}
	};
	Observable<String> ftObservable1 = HystrixObservableCommandFacade.observe(timeoutCommandSupplier, commandSettings);
	final Observable<String> ftObservable2 = HystrixObservableCommandFacade.observe(timeoutCommandSupplier, commandSettings);
	
	ftObservable1.subscribe(); // Ignore
	
	assertEquals(1, supplierInvocationCount.get());
	AstrixTestUtil.serviceInvocationException(() -> ftObservable2.toBlocking().first(), AstrixTestUtil.isExceptionOfType(ServiceUnavailableException.class));
	assertEquals(1, supplierInvocationCount.get());
}
 
Example #8
Source File: GsUtil.java    From astrix with Apache License 2.0 6 votes vote down vote up
public static <T> Func1<List<AsyncResult<T>>, Observable<T>> asyncResultListToObservable() {
	return new Func1<List<AsyncResult<T>>, Observable<T>>() {
		@Override
		public Observable<T> call(final List<AsyncResult<T>> asyncRresults) {
			return Observable.create(new OnSubscribe<T>() {
				@Override
				public void call(Subscriber<? super T> subscriber) {
					for (AsyncResult<T> asyncInvocationResponse : asyncRresults) {
						if (asyncInvocationResponse.getException() != null) {
							subscriber.onError(asyncInvocationResponse.getException());
							return;
						}
						subscriber.onNext(asyncInvocationResponse.getResult());
					}
					subscriber.onCompleted();
				}
			});
		}};
}
 
Example #9
Source File: OperatorGroupByTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
Observable<Event> SYNC_INFINITE_OBSERVABLE_OF_EVENT(final int numGroups, final AtomicInteger subscribeCounter, final AtomicInteger sentEventCounter) {
    return Observable.create(new OnSubscribe<Event>() {

        @Override
        public void call(final Subscriber<? super Event> op) {
            subscribeCounter.incrementAndGet();
            int i = 0;
            while (!op.isUnsubscribed()) {
                i++;
                Event e = new Event();
                e.source = i % numGroups;
                e.message = "Event-" + i;
                op.onNext(e);
                sentEventCounter.incrementAndGet();
            }
            op.onCompleted();
        }

    });
}
 
Example #10
Source File: FileObservable.java    From newts with Apache License 2.0 6 votes vote down vote up
public static Observable<String> lines(final Path path) {
    return Observable.create(new OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> s) {
            try (BufferedReader in = fileReader(path))
            {
                String line;
                while ((line = in.readLine()) != null) {
                    if (s.isUnsubscribed()) return;
                    s.onNext(line);
                }
                s.onCompleted();
                
            } catch (Exception e) {
                if (!s.isUnsubscribed()) s.onError(e);
            }
        }
        
    });
}
 
Example #11
Source File: RequestProcessor.java    From mantis with Apache License 2.0 6 votes vote down vote up
public Observable<Void> sendInfiniteStream(final HttpServerResponse<ByteBuf> response) {
    response.getHeaders().add(HttpHeaders.Names.CONTENT_TYPE, "text/event-stream");
    response.getHeaders().add(HttpHeaders.Names.TRANSFER_ENCODING, "chunked");

    return Observable.create(new OnSubscribe<Void>() {
        final AtomicLong counter = new AtomicLong();
        Worker worker = Schedulers.computation().createWorker();

        public void call(Subscriber<? super Void> subscriber) {
            worker.schedulePeriodically(
                    new Action0() {
                        @Override
                        public void call() {
                            System.out.println("In infinte stream");
                            byte[] contentBytes = ("data:" + "line " + counter.getAndIncrement() + "\n\n").getBytes();
                            response.writeBytes(contentBytes);
                            response.flush();
                        }
                    },
                    0,
                    100,
                    TimeUnit.MILLISECONDS
            );
        }
    });
}
 
Example #12
Source File: OperatorResumeOnErrorTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
private Observable<Integer> createIntegerStreamThatFailsOnThresholdValue(final int threshold) {
    return Observable.create(new OnSubscribe<Integer>() {
        @Override
        public void call(final Subscriber<? super Integer> subscriber) {
            Observable
                    .just(1, 2, 3, 4, 5, 6)
                    .doOnNext(new Action1<Integer>() {
                        @Override
                        public void call(Integer value) {
                            if (value == threshold) {
                                subscriber.onError(new TestException("Failed on value " + value));
                            } else {
                                subscriber.onNext(value);
                            }
                        }
                    }).subscribe();

        }
    });
}
 
Example #13
Source File: LocalJobExecutorNetworked.java    From mantis with Apache License 2.0 6 votes vote down vote up
private static Observable<Set<Endpoint>> staticEndpoints(final int[] ports, final int stageNum, final int workerIndex,
                                                         final int numPartitions) {
    return Observable.create(new OnSubscribe<Set<Endpoint>>() {
        @Override
        public void call(Subscriber<? super Set<Endpoint>> subscriber) {
            Set<Endpoint> endpoints = new HashSet<Endpoint>();
            for (int i = 0; i < ports.length; i++) {
                int port = ports[i];
                for (int j = 1; j <= numPartitions; j++) {
                    Endpoint endpoint = new Endpoint("localhost", port,
                            "stage_" + stageNum + "_index_" + workerIndex + "_partition_" + j);
                    logger.info("adding static endpoint:" + endpoint);
                    endpoints.add(endpoint);
                }
            }
            subscriber.onNext(endpoints);
            subscriber.onCompleted();
        }
    });
}
 
Example #14
Source File: RemoteObservable.java    From mantis with Apache License 2.0 6 votes vote down vote up
public static <T> RemoteRxConnection<T> connect(final ConnectToObservable<T> params) {
    final RxMetrics metrics = new RxMetrics();

    return new RemoteRxConnection<T>(Observable.create(new OnSubscribe<T>() {
        @Override
        public void call(Subscriber<? super T> subscriber) {
            RemoteUnsubscribe remoteUnsubscribe = new RemoteUnsubscribe(params.getName());
            // wrapped in Observable.create() to inject unsubscribe callback
            subscriber.add(remoteUnsubscribe); // unsubscribed callback
            // create connection
            createTcpConnectionToServer(params, remoteUnsubscribe, metrics,
                    params.getConnectionDisconnectCallback(),
                    params.getCloseTrigger())
                    .subscribe(subscriber);
        }
    }), metrics, params.getCloseTrigger());
}
 
Example #15
Source File: RemoteObservable.java    From mantis with Apache License 2.0 6 votes vote down vote up
public static <K, V> RemoteRxConnection<GroupedObservable<K, V>> connect(
        final ConnectToGroupedObservable<K, V> config) {
    final RxMetrics metrics = new RxMetrics();
    return new RemoteRxConnection<GroupedObservable<K, V>>(Observable.create(
            new OnSubscribe<GroupedObservable<K, V>>() {
                @Override
                public void call(Subscriber<? super GroupedObservable<K, V>> subscriber) {
                    RemoteUnsubscribe remoteUnsubscribe = new RemoteUnsubscribe(config.getName());
                    // wrapped in Observable.create() to inject unsubscribe callback
                    subscriber.add(remoteUnsubscribe); // unsubscribed callback
                    // create connection
                    createTcpConnectionToServer(config, remoteUnsubscribe, metrics,
                            config.getConnectionDisconnectCallback(),
                            config.getCloseTrigger())
                            .retryWhen(retryLogic(config))
                            .subscribe(subscriber);
                }
            }), metrics, config.getCloseTrigger());
}
 
Example #16
Source File: RemoteObservable.java    From mantis with Apache License 2.0 6 votes vote down vote up
public static <K, V> RemoteRxConnection<MantisGroup<K, V>> connectToMGO(
        final ConnectToGroupedObservable<K, V> config, final SpscArrayQueue<MantisGroup<?, ?>> inputQueue) {
    final RxMetrics metrics = new RxMetrics();
    return new RemoteRxConnection<MantisGroup<K, V>>(Observable.create(
            new OnSubscribe<MantisGroup<K, V>>() {
                @Override
                public void call(Subscriber<? super MantisGroup<K, V>> subscriber) {
                    RemoteUnsubscribe remoteUnsubscribe = new RemoteUnsubscribe(config.getName());
                    // wrapped in Observable.create() to inject unsubscribe callback
                    subscriber.add(remoteUnsubscribe); // unsubscribed callback
                    // create connection
                    createTcpConnectionToGOServer(config, remoteUnsubscribe, metrics,
                            config.getConnectionDisconnectCallback(),
                            config.getCloseTrigger(),
                            inputQueue)
                            .retryWhen(retryLogic(config))

                            .subscribe(subscriber);
                }
            }), metrics, config.getCloseTrigger());
}
 
Example #17
Source File: SSTableColumnScanner.java    From aegisthus with Apache License 2.0 6 votes vote down vote up
public rx.Observable<AtomWritable> observable() {
    final ExecutorService service = Executors.newSingleThreadExecutor();
    rx.Observable<AtomWritable> ret = rx.Observable.create(new OnSubscribe<AtomWritable>() {
        @Override
        public void call(final Subscriber<? super AtomWritable> subscriber) {
            service.execute(new Runnable() {
                @Override
                public void run() {
                    deserialize(subscriber);
                    subscriber.onCompleted();
                }
            });
        }
    });
    LOG.info("created observable");
    return ret;
}
 
Example #18
Source File: EvCacheProvider.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Observable<T> get(final String key, Map<String, Object> requestProperties) {
    return Observable.create(new OnSubscribe<T>() {
        @Override
        public void call(Subscriber<? super T> subscriber) {
            Future<T> getFuture;
            try {
                if (options.getTranscoder() == null) {
                    getFuture = evCache.getAsynchronous(key);
                } else {
                    getFuture = (Future<T>) evCache.getAsynchronous(key, options.getTranscoder());
                }
                FUTURE_OBSERVER.watchFuture(getFuture, subscriber);
            } catch (EVCacheException e) {
                subscriber.onError(new CacheFaultException("EVCache exception when getting value for key " + key, e));
            }
        }
    });
}
 
Example #19
Source File: RemoteObservableTest.java    From mantis with Apache License 2.0 6 votes vote down vote up
@Test(expected = RuntimeException.class)
public void testError() {
    PortSelectorWithinRange portSelector = new PortSelectorWithinRange(8000, 9000);
    Observable<Integer> os = Observable.create(new OnSubscribe<Integer>() {
        @Override
        public void call(Subscriber<? super Integer> subscriber) {
            subscriber.onNext(1);
            subscriber.onError(new Exception("test-exception"));
        }
    });
    int serverPort = portSelector.acquirePort();
    RemoteObservable.serve(serverPort, os, Codecs.integer())
            .start();
    Observable<Integer> oc = RemoteObservable.connect("localhost", serverPort, Codecs.integer());
    MathObservable.sumInteger(oc).toBlocking().forEach(new Action1<Integer>() {
        @Override
        public void call(Integer t1) {
            Assert.assertEquals(5050, t1.intValue()); // sum of number 0-100
        }
    });
}
 
Example #20
Source File: ListenableFutureObservable.java    From RxJavaGuava with Apache License 2.0 6 votes vote down vote up
/**
 * Converts from {@link ListenableFuture} to {@link rx.Observable}.
 * 
 * @param future  the {@link ListenableFuture} to register a listener on.
 * @param executor  the {@link Executor} where the callback will be executed.  The will be where the {@link Observer#onNext(Object)} call from.
 * @return an {@link Observable} that emits the one value when the future completes.
 */
public static <T> Observable<T> from(final ListenableFuture<T> future, final Executor executor) {
    return Observable.create(new OnSubscribe<T>() {
        @Override
        public void call(final Subscriber<? super T> subscriber) {
            final SingleDelayedProducer<T> sdp = new SingleDelayedProducer<T>(subscriber);
            subscriber.setProducer(sdp);
            
            future.addListener(new Runnable() {
                @Override
                public void run() {
                    try {
                        T t = future.get();
                        sdp.setValue(t);
                    } catch (Exception e) {
                        subscriber.onError(e);
                    }
                }
            }, executor);
            
        }
    });
}
 
Example #21
Source File: Eureka2InterestManager.java    From ocelli with Apache License 2.0 5 votes vote down vote up
public Observable<Instance<SocketAddress>> forInterest(final Interest<InstanceInfo> interest,
                                                       final Func1<InstanceInfo, SocketAddress> instanceInfoToHost) {
    return Observable.create(new OnSubscribe<Instance<SocketAddress>>() {
        @Override
        public void call(Subscriber<? super Instance<SocketAddress>> s) {
            final InstanceManager<SocketAddress> subject = InstanceManager.create();
            s.add(client
                    .forInterest(interest)
                    .subscribe(new Action1<ChangeNotification<InstanceInfo>>() {
                        @Override
                        public void call(ChangeNotification<InstanceInfo> notification) {
                            SocketAddress host = instanceInfoToHost.call(notification.getData());
                            switch (notification.getKind()) {
                                case Add:
                                    subject.add(host);
                                    break;
                                case Delete:
                                    subject.remove(host);
                                    break;
                                case Modify:
                                    subject.remove(host);
                                    subject.add(host);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }));
            
            subject.subscribe(s);
        }
    });
}
 
Example #22
Source File: DynamicConnection.java    From mantis with Apache License 2.0 5 votes vote down vote up
public Observable<T> observable() {
    return Observable.create(new OnSubscribe<T>() {
        @Override
        public void call(final Subscriber<? super T> subscriber) {
            subscriber.add(subject.flatMap(new Func1<Observable<T>, Observable<T>>() {
                @Override
                public Observable<T> call(Observable<T> t1) {
                    return t1;
                }
            }).subscribe(new Observer<T>() {

                @Override
                public void onCompleted() {
                    subscriber.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    subscriber.onError(e);
                }

                @Override
                public void onNext(T t) {
                    subscriber.onNext(t);
                }
            }));
            subscriber.add(changeEndpointObservable.subscribe(new Action1<Endpoint>() {
                @Override
                public void call(Endpoint endpoint) {
                    logger.debug("New endpoint: " + endpoint);
                    subject.onNext(toObservableFunc.call(endpoint));
                }
            }));
        }
    });
}
 
Example #23
Source File: OperatorSampleFirstTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testThrottlingWithCompleted() {
    Observable<String> source = Observable.create(new OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> observer) {
            publishNext(observer, 100, "one"); // publish as it's first
            publishNext(observer, 300, "two"); // skip as it's last within
                                               // the first 400
            publishNext(observer, 900, "three"); // publish
            publishNext(observer, 905, "four"); // skip
            publishCompleted(observer, 1000); // Should be published as soon
                                              // as the timeout expires.
        }
    });

    Observable<String> sampled = source
            .compose(Transformers.<String> sampleFirst(400, TimeUnit.MILLISECONDS, scheduler));
    sampled.subscribe(observer);

    InOrder inOrder = inOrder(observer);

    scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS);
    inOrder.verify(observer, times(1)).onNext("one");
    inOrder.verify(observer, times(0)).onNext("two");
    inOrder.verify(observer, times(1)).onNext("three");
    inOrder.verify(observer, times(0)).onNext("four");
    inOrder.verify(observer, times(1)).onCompleted();
    inOrder.verifyNoMoreInteractions();
}
 
Example #24
Source File: OperatorSampleFirstTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
public void testThrottlingWithError() {
    Observable<String> source = Observable.create(new OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> observer) {
            Exception error = new TestException();
            publishNext(observer, 100, "one"); // Should be published since
                                               // it is first
            publishNext(observer, 200, "two"); // Should be skipped since
                                               // onError will arrive before
                                               // the timeout expires
            publishError(observer, 300, error); // Should be published as
                                                // soon as the timeout
                                                // expires.
        }
    });

    Observable<String> sampled = source
            .compose(Transformers.<String> sampleFirst(400, TimeUnit.MILLISECONDS, scheduler));
    sampled.subscribe(observer);

    InOrder inOrder = inOrder(observer);

    scheduler.advanceTimeTo(400, TimeUnit.MILLISECONDS);
    inOrder.verify(observer).onNext("one");
    inOrder.verify(observer).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
}
 
Example #25
Source File: HttpMetaRequest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
private Observable<RibbonResponse<Observable<T>>> convertToRibbonResponse(
        final HystrixObservableCommandChain<T> commandChain, final Observable<ResultCommandPair<T>> hystrixNotificationObservable) {
    return Observable.create(new OnSubscribe<RibbonResponse<Observable<T>>>() {
        @Override
        public void call(
                final Subscriber<? super RibbonResponse<Observable<T>>> t1) {
            final Subject<T, T> subject = ReplaySubject.create();
            hystrixNotificationObservable.materialize().subscribe(new Action1<Notification<ResultCommandPair<T>>>() {
                AtomicBoolean first = new AtomicBoolean(true);

                @Override
                public void call(Notification<ResultCommandPair<T>> notification) {
                    if (first.compareAndSet(true, false)) {
                        HystrixObservableCommand<T> command = notification.isOnError() ? commandChain.getLastCommand() : notification.getValue().getCommand();
                        t1.onNext(new ResponseWithSubject<T>(subject, command));
                        t1.onCompleted();
                    }
                    if (notification.isOnNext()) {
                        subject.onNext(notification.getValue().getResult());
                    } else if (notification.isOnCompleted()) {
                        subject.onCompleted();
                    } else { // onError
                        subject.onError(notification.getThrowable());
                    }
                }
            });
        }
    });
}
 
Example #26
Source File: LoadBalancer.java    From ocelli with Apache License 2.0 5 votes vote down vote up
public Observable<T> toObservable() {
    return Observable.create(new OnSubscribe<T>() {
        @Override
        public void call(Subscriber<? super T> s) {
            try {
                s.onNext(next());
                s.onCompleted();
            }
            catch (Exception e) {
                s.onError(e);
            }
        }
    });
}
 
Example #27
Source File: RxUtil.java    From ocelli with Apache License 2.0 5 votes vote down vote up
public static <T> Observable<Observable<T>> onSubscribeChooseNext(final Observable<T> ... sources) {
    return Observable.create(new OnSubscribe<Observable<T>>() {
        private AtomicInteger count = new AtomicInteger();
        
        @Override
        public void call(Subscriber<? super Observable<T>> t1) {
            int index = count.getAndIncrement();
            if (index < sources.length) {
                t1.onNext(sources[index]);
            }
            t1.onCompleted();
        }
    });
}
 
Example #28
Source File: HostHolder.java    From ocelli with Apache License 2.0 5 votes vote down vote up
public Observable<Void> shutdown() {
    return Observable.create(new OnSubscribe<Void>() {
        @Override
        public void call(Subscriber<? super Void> subscriber) {
            if (null != streamSubscription) {
                streamSubscription.unsubscribe();
            }

            subscriber.onCompleted();
        }
    });
}
 
Example #29
Source File: LoadBalancerCommand.java    From ribbon with Apache License 2.0 5 votes vote down vote up
/**
 * Return an Observable that either emits only the single requested server
 * or queries the load balancer for the next server on each subscription
 */
private Observable<Server> selectServer() {
    return Observable.create(new OnSubscribe<Server>() {
        @Override
        public void call(Subscriber<? super Server> next) {
            try {
                Server server = loadBalancerContext.getServerFromLoadBalancer(loadBalancerURI, loadBalancerKey);   
                next.onNext(server);
                next.onCompleted();
            } catch (Exception e) {
                next.onError(e);
            }
        }
    });
}
 
Example #30
Source File: BackendResponse.java    From WSPerfLab with Apache License 2.0 5 votes vote down vote up
public static Observable<BackendResponse> fromJsonToObservable(final JsonFactory jsonFactory, final String json, Scheduler scheduler) {
    return Observable.create(new OnSubscribe<BackendResponse>() {

        @Override
        public void call(Subscriber<? super BackendResponse> o) {
            try {
                o.onNext(fromJson(jsonFactory, json));
                o.onCompleted();
            } catch (Exception e) {
                o.onError(e);
            }
        }
    }).subscribeOn(scheduler);
}