From abf3ba36a7693ec36679fb95c4cf42555b72ffd2 Mon Sep 17 00:00:00 2001 From: Julien Ponge Date: Fri, 15 Sep 2023 18:10:39 +0200 Subject: [PATCH] docs: add a workshop example for Multi::split Fixes #1368 --- .../_01_Multi_Split.java | 80 +++++++++++++++++++ .../_01_Multi_Custom_Operator.java | 2 +- .../{_07_misc => _08_misc}/_02_Logging.java | 2 +- .../{_07_misc => _08_misc}/_03_Context.java | 2 +- 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100755 workshop-examples/src/main/java/_07_advanced_streaming/_01_Multi_Split.java rename workshop-examples/src/main/java/{_07_misc => _08_misc}/_01_Multi_Custom_Operator.java (98%) rename workshop-examples/src/main/java/{_07_misc => _08_misc}/_02_Logging.java (97%) rename workshop-examples/src/main/java/{_07_misc => _08_misc}/_03_Context.java (98%) diff --git a/workshop-examples/src/main/java/_07_advanced_streaming/_01_Multi_Split.java b/workshop-examples/src/main/java/_07_advanced_streaming/_01_Multi_Split.java new file mode 100755 index 000000000..6cf80293c --- /dev/null +++ b/workshop-examples/src/main/java/_07_advanced_streaming/_01_Multi_Split.java @@ -0,0 +1,80 @@ +///usr/bin/env jbang "$0" "$@" ; exit $? +//DEPS io.smallrye.reactive:mutiny:2.4.0 +package _07_advanced_streaming; + +import static _07_advanced_streaming._01_Multi_Split.Country.*; + +import java.util.List; + +import io.smallrye.mutiny.Multi; + +public class _01_Multi_Split { + + static class TemperatureRecord { + final Country country; + final String city; + final long timestamp; + final double value; + + TemperatureRecord(Country country, String city, long timestamp, double value) { + this.country = country; + this.city = city; + this.timestamp = timestamp; + this.value = value; + } + + @Override + public String toString() { + return "TemperatureRecord{" + + "country=" + country + + ", city='" + city + '\'' + + ", timestamp=" + timestamp + + ", value=" + value + + '}'; + } + } + + enum Country { + FRANCE, + UK, + AUSTRALIA + } + + public static void main(String[] args) { + System.out.println("⚑️ Multi split operator"); + + var data = List.of( + new TemperatureRecord(FRANCE, "Tassin-La-Demi-Lune", System.nanoTime(), 28.0), + new TemperatureRecord(FRANCE, "Clermont-Ferrand", System.nanoTime(), 27.0), + new TemperatureRecord(FRANCE, "Nevers", System.nanoTime(), 27.0), + new TemperatureRecord(FRANCE, "AubiΓ¨re", System.nanoTime(), 28.0), + new TemperatureRecord(AUSTRALIA, "Sydney", System.nanoTime(), 16.0), + new TemperatureRecord(FRANCE, "Lyon", System.nanoTime(), 29.0), + new TemperatureRecord(AUSTRALIA, "Kensington", System.nanoTime(), 16.0), + new TemperatureRecord(UK, "Newcastle", System.nanoTime(), 13.0), + new TemperatureRecord(AUSTRALIA, "Coogee", System.nanoTime(), 16.0), + new TemperatureRecord(UK, "Bexhill", System.nanoTime(), 22.0)); + + var splitter = Multi.createFrom().iterable(data) + .split(Country.class, record -> record.country); + + splitter.get(FRANCE) + .subscribe().with( + record -> System.out.println("πŸ‡«πŸ‡· => " + record), + Throwable::printStackTrace, + () -> System.out.println("βœ… Done with France")); + + splitter.get(AUSTRALIA) + .subscribe().with( + record -> System.out.println("πŸ‡¦πŸ‡ΊπŸ¦˜ => " + record), + Throwable::printStackTrace, + () -> System.out.println("βœ… Done with Australia")); + + splitter.get(UK) + .subscribe().with( + record -> System.out.println("πŸ‡¬πŸ‡§ => " + record), + Throwable::printStackTrace, + () -> System.out.println("βœ… Done with the UK")); + + } +} diff --git a/workshop-examples/src/main/java/_07_misc/_01_Multi_Custom_Operator.java b/workshop-examples/src/main/java/_08_misc/_01_Multi_Custom_Operator.java similarity index 98% rename from workshop-examples/src/main/java/_07_misc/_01_Multi_Custom_Operator.java rename to workshop-examples/src/main/java/_08_misc/_01_Multi_Custom_Operator.java index 5fd5efc0f..13e144a7e 100755 --- a/workshop-examples/src/main/java/_07_misc/_01_Multi_Custom_Operator.java +++ b/workshop-examples/src/main/java/_08_misc/_01_Multi_Custom_Operator.java @@ -1,6 +1,6 @@ ///usr/bin/env jbang "$0" "$@" ; exit $? //DEPS io.smallrye.reactive:mutiny:2.4.0 -package _07_misc; +package _08_misc; import java.util.concurrent.ThreadLocalRandom; diff --git a/workshop-examples/src/main/java/_07_misc/_02_Logging.java b/workshop-examples/src/main/java/_08_misc/_02_Logging.java similarity index 97% rename from workshop-examples/src/main/java/_07_misc/_02_Logging.java rename to workshop-examples/src/main/java/_08_misc/_02_Logging.java index be49e4650..175d296e9 100755 --- a/workshop-examples/src/main/java/_07_misc/_02_Logging.java +++ b/workshop-examples/src/main/java/_08_misc/_02_Logging.java @@ -1,6 +1,6 @@ ///usr/bin/env jbang "$0" "$@" ; exit $? //DEPS io.smallrye.reactive:mutiny:2.4.0 -package _07_misc; +package _08_misc; import io.smallrye.mutiny.Multi; diff --git a/workshop-examples/src/main/java/_07_misc/_03_Context.java b/workshop-examples/src/main/java/_08_misc/_03_Context.java similarity index 98% rename from workshop-examples/src/main/java/_07_misc/_03_Context.java rename to workshop-examples/src/main/java/_08_misc/_03_Context.java index 8ad80250a..d5f016de8 100755 --- a/workshop-examples/src/main/java/_07_misc/_03_Context.java +++ b/workshop-examples/src/main/java/_08_misc/_03_Context.java @@ -1,6 +1,6 @@ ///usr/bin/env jbang "$0" "$@" ; exit $? //DEPS io.smallrye.reactive:mutiny:2.4.0 -package _07_misc; +package _08_misc; import java.util.List;