Skip to content

Instantly share code, notes, and snippets.

@davidwatkins73
Created September 12, 2015 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwatkins73/e463a531cca3310a675c to your computer and use it in GitHub Desktop.
Save davidwatkins73/e463a531cca3310a675c to your computer and use it in GitHub Desktop.
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")
);
}
}
@davidwatkins73
Copy link
Author

Playing with ideas for driving jdbc batch via rx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment