.forEach(new Action1<HashMap<String, ByteIterator>>() { @Override public void call(HashMap<String, ByteIterator> tuple) {
/** * If the RequestContext does not get transferred across threads correctly this blows up. * No specific assertions are necessary. */ @Test public void testRequestContextPropagatesAcrossObserveOnPool() { new SimpleCommand().execute(); new SimpleCommand().observe().map(new Func1<String, String>() { @Override public String call(String s) { System.out.println("Map => Commands: " + HystrixRequestLog.getCurrentRequest().getAllExecutedCommands()); return s; } }).toBlocking().forEach(new Action1<String>() { @Override public void call(String s) { System.out.println("Result [" + s + "] => Commands: " + HystrixRequestLog.getCurrentRequest().getAllExecutedCommands()); } }); }
private static void flatMapBufferedExampleAsync() { Observable.range(0, 5000).buffer(500).flatMap(i -> { return Observable.from(i).subscribeOn(Schedulers.computation()).map(item -> { // simulate computational work try { Thread.sleep(1); } catch (Exception e) { } return item + " processed " + Thread.currentThread(); }); }).toBlocking().forEach(System.out::println); }
private static void flatMapWindowedExampleAsync() { Observable.range(0, 5000).window(500).flatMap(work -> { return work.observeOn(Schedulers.computation()).map(item -> { // simulate computational work try { Thread.sleep(1); } catch (Exception e) { } return item + " processed " + Thread.currentThread(); }); }).toBlocking().forEach(System.out::println); }
private static void flatMapExampleAsync() { Observable.range(0, 5).flatMap(i -> { return getDataAsync(i); }).toBlocking().forEach(System.out::println); }
private static void mergingSyncMadeAsync() { // if you have something synchronous and want to make it async, you can schedule it like this // so here we see both executed concurrently Observable.merge(getDataSync(1).subscribeOn(Schedulers.io()), getDataSync(2).subscribeOn(Schedulers.io())).toBlocking().forEach(System.out::println); }
public static void main(String... args) { Observable<String> data = Observable.just("one", "two", "three", "four", "five"); Observable.zip(data, Observable.interval(1, TimeUnit.SECONDS), (d, t) -> { return d + " " + t; }).toBlocking().forEach(System.out::println); } }
private static void flatMapExampleSync() { Observable.range(0, 5).flatMap(i -> { return getDataSync(i); }).toBlocking().forEach(System.out::println); }
public static void main(String[] args) { getData(1).observeOn(Schedulers.computation()).toBlocking().forEach(System.out::println); }
public static void main(String args[]) { hotStream().sample(500, TimeUnit.MILLISECONDS).toBlocking().forEach(System.out::println); }
public static void main(String args[]) { // first item emitted in each time window hotStream().throttleFirst(500, TimeUnit.MILLISECONDS).take(10).toBlocking().forEach(System.out::println); // last item emitted in each time window hotStream().throttleLast(500, TimeUnit.MILLISECONDS).take(10).toBlocking().forEach(System.out::println); }
public static void main(String args[]) { // buffer every 500ms (using 999999999 to mark start of output) hotStream().window(500, TimeUnit.MILLISECONDS).take(10).flatMap(w -> w.startWith(999999999)).toBlocking().forEach(System.out::println); // buffer 10 items at a time (using 999999999 to mark start of output) hotStream().window(10).take(2).flatMap(w -> w.startWith(999999999)).toBlocking().forEach(System.out::println); System.out.println("Done"); }
private static void mergingSync() { // here you'll see the delay as each is executed synchronously Observable.merge(getDataSync(1), getDataSync(2)).toBlocking().forEach(System.out::println); }
private static void mergingAsync() { Observable.merge(getDataAsync(1), getDataAsync(2)).toBlocking().forEach(System.out::println); }
public static void main(String... args) { /* * retry(n) can be used to immediately retry n times */ Observable.create(s -> { System.out.println("1) subscribing"); s.onError(new RuntimeException("1) always fails")); }).retry(3).subscribe(System.out::println, t -> System.out.println("1) Error: " + t)); System.out.println(""); /* * retryWhen allows custom behavior on when and if a retry should be done */ Observable.create(s -> { System.out.println("2) subscribing"); s.onError(new RuntimeException("2) always fails")); }).retryWhen(attempts -> { return attempts.zipWith(Observable.range(1, 3), (n, i) -> i).flatMap(i -> { System.out.println("2) delay retry by " + i + " second(s)"); return Observable.timer(i, TimeUnit.SECONDS); }).concatWith(Observable.error(new RuntimeException("Failed after 3 retries"))); }).toBlocking().forEach(System.out::println); } }
public static void main(String args[]) { // debounce to the last value in each burst //intermittentBursts().debounce(10, TimeUnit.MILLISECONDS).toBlocking().forEach(System.out::println); /* The following will emit a buffered list as it is debounced */ // first we multicast the stream ... using refCount so it handles the subscribe/unsubscribe Observable<Integer> burstStream = intermittentBursts().take(20).publish().refCount(); // then we get the debounced version Observable<Integer> debounced = burstStream.debounce(10, TimeUnit.MILLISECONDS); // then the buffered one that uses the debounced stream to demark window start/stop Observable<List<Integer>> buffered = burstStream.buffer(debounced); // then we subscribe to the buffered stream so it does what we want buffered.toBlocking().forEach(System.out::println); }
public static void main(String[] args) { Observable.just(100, 2000, 3000, 6000, 7000, 10000) .flatMap(time -> Observable.timer(time, TimeUnit.MILLISECONDS).map(v -> time)) .throttleFirst(4000, TimeUnit.MILLISECONDS) .toBlocking() .forEach(System.out::println); } }
/** * Delete all records */ public void delete(RxTx tx) { // non-blocking needed? scan(tx).toBlocking().forEach(keyValues -> { Observable<byte[]> keys = keyValues.stream() .map(kv -> Observable.just(kv.key())) .reduce(Observable.empty(), (o1, o2) -> o1.mergeWith(o2)); delete(tx, keys); }); }
public static void main(final String[] args) throws Exception { System.out.println("Your message Sir: " + SERVICE.single().get()); System.out.println("Your messages Sir: " + SERVICE.multi(2).get()); final Observable<String> messagesStream = SERVICE.stream(10); messagesStream.toBlocking().forEach(message -> System.out.println("Your message from stream: " + message)); System.out.println("Done."); Clients.close(SERVICE); } }