I am running over a slow connection and I want to set up a Speedment Stream with a callback whenever a new entity is available from the database. How do I do that?
Asked
Active
Viewed 53 times
1 Answers
1
Here is one way of doing it:
public class Main {
public static void main(String[] args) {
DemoApplication app = new DemoApplicationBuilder()
.withPassword("mySecretPw")
.build();
final Manager<Country> countries =
app.getOrThrow(CountryManager.class);
ForkJoinPool.commonPool().submit(
() -> countries.stream().forEach(Main::callback)
);
}
private static void callback(Country c) {
System.out.format("Thread %s consumed %s%n",
Thread.currentThread().getName(), c);
}
}
Of course you can provide any stream to the ForkJoinPool. For example, if you only want to receive the first ten countries with names that that starts with "A" in your callback, then your could write:
ForkJoinPool.commonPool().submit(
() -> countries.stream()
.filter(Country.NAME.startsWith("A"))
.limit(10)
.forEach(Main::callback)
);

Per-Åke Minborg
- 290
- 2
- 7