Created
September 12, 2015 21:31
-
-
Save davidwatkins73/e463a531cca3310a675c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.davidwatkins73.rx.playpen; | |
import rx.Observable; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class RxBatchDemo { | |
public static void main(String[] args) { | |
List<Integer> batch = new ArrayList<>(); | |
Observable.range(0, 1000) | |
.map(x -> { | |
batch.add(x); | |
return 1; | |
}) | |
.scan(1, (acc, x) -> acc + 1) | |
.filter(x -> x % 300 == 0) | |
.subscribe( | |
x -> { | |
System.out.println("commit " + x + " == " + batch); | |
batch.clear(); | |
}, | |
e -> System.err.println(e.getMessage()), | |
() -> System.out.println("Done") | |
); | |
Observable.range(0, 10) | |
.map(x -> 1) | |
.scan((acc, x) -> acc + x) | |
.map(x -> x / 3) | |
.distinct() | |
.subscribe(System.out::println); | |
} | |
public static void withSize() { | |
List<Integer> batch = new ArrayList<>(); | |
Observable.range(0, 10) | |
.map(x -> { | |
batch.add(x); | |
System.out.println("batch now: "+batch.size()); | |
return batch.size(); | |
}) | |
.filter(x -> x >= 3) | |
.subscribe( | |
x -> { | |
System.out.println("commit " + x + " == " + batch); | |
batch.clear(); | |
}, | |
e -> System.err.println(e.getMessage()), | |
() -> System.out.println("Done") | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Playing with ideas for driving jdbc batch via rx.