Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava in Microservices World

Piotr Kafel
January 27, 2016

RxJava in Microservices World

Piotr Kafel

January 27, 2016
Tweet

More Decks by Piotr Kafel

Other Decks in Programming

Transcript

  1. 26-27/05/2016 CompletableFuture CompletableFuture<String> result = getUuid().thenApply(this::getString); public CompletableFuture<UUID> getUuid() {

    // incredibly smart code goes here } public String getString(UUID uuid) { // incredibly smart code goes here }
  2. 26-27/05/2016 CompletableFuture CompletableFuture<List<CompletableFuture<String>>> ohMyGod = getUuids() .thenApply(uuids -> uuids.stream() .map(this::getString)

    .collect(Collectors.toList())); public CompletableFuture<List<UUID>> getUuids() { // incredibly smart code goes here } public CompletableFuture<String> getString(UUID uuid) { // incredibly smart code goes here }
  3. 26-27/05/2016 Observable Observable<String> result = getUuids().flatMap(this::getString); public Observable<UUID> getUuids() {

    // incredibly smart code goes here } public Observable<String> getString(UUID uuid) { // incredibly smart code goes here }
  4. 26-27/05/2016 Observable Observable.create(subscriber -> { try { subscriber.onNext(doSomething()); subscriber.onCompleted(); }

    catch(Exception e) { subscriber.onError(e); } }).forEach( i -> System.out.print(i), e -> e.printStackTrace() );
  5. 26-27/05/2016 Example public Observable<DealDivision> retrieveDivisions (List <UUID> divisions) { Locale

    locale = locale(); return Observable.from(divisions) .flatMap(divisionId -> geoPlacesClient.getDivisionsById(divisionId)) .filter(resp -> resp.status == SUCCESS) .flatMapIterable(resp -> resp.data) .filter(div -> div.status == Status.ACTIVE) .map(div -> new DealDivision( div.uuid.toString(), div.getNameOrDefault(locale))); }
  6. 26-27/05/2016 Pattern doHttpCall().retryWhen(errorObservable -> errorObservable .zipWith(Observable.range(1, numberOfRetries), Pair::of) .flatMap(pair ->

    { if(pair.getRight() == numberOfRetries) { return Observable.error(pair.getLeft()); } else { return Observable.timer(wait, TimeUnit.MILLISECONDS); } }));
  7. 26-27/05/2016 Testing TestSubscriber<String> subscriber = new TestSubscriber<>(); Observable.interval(100, TimeUnit.MILLISECONDS, Schedulers.computation())

    .take(5) .map(i -> i + " value") .subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertNoErrors(); subscriber.assertValueCount(5);
  8. 26-27/05/2016 Retrofit public interface InventoryUnitClient { @GET("/inventory/v2/{entity}/search/redemption") Observable<JsonHolder> getInventoryUnitRedemption( @Path("uuid")

    UUID uuid); @POST("/inventory/v2/{entity}/search/redemption") Observable<JsonHolder> redeemUnit( @Path("uuid") UUID uuid, @Body JsonHolder redemption); }
  9. 26-27/05/2016 Hystrix public class CommandHelloWorld extends HystrixCommand<String> { private final

    String name; public CommandHelloWorld(String name) { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")); this.name = name; } @Override protected String run() { return "Hello " + name + "!"; } }
  10. 26-27/05/2016 Spring @RequestMapping(value = PATH_ACCOUNT_MANAGERS, method = RequestMethod.GET) public DeferredResult<AccountManagersResponse>

    getAccountManagers() { DeferredResult<AccountManagersResponse> result = new DeferredResult<>(); getAccountManagersObservable().subscribe( item -> result.setResult(item), exception -> result.setErrorResult(exception) ); return result; }