-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: import the Mutiny workshop examples into the main repository
- Loading branch information
Showing
69 changed files
with
2,277 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.smallrye.reactive</groupId> | ||
<artifactId>mutiny-project</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
<name>SmallRye Mutiny - Workshop examples</name> | ||
<description>Workshop examples</description> | ||
<artifactId>mutiny-workshop-examples</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.smallrye.reactive</groupId> | ||
<artifactId>mutiny</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<showDeprecation>true</showDeprecation> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _01_Uni { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Hello world"); | ||
|
||
Uni<String> uni = Uni.createFrom().item("Hello, world!"); | ||
|
||
uni.subscribe().with(System.out::println); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
workshop-examples/src/main/java/_01_basics/_02_Uni_UniSubscriber.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.subscription.UniSubscriber; | ||
import io.smallrye.mutiny.subscription.UniSubscription; | ||
|
||
public class _02_Uni_UniSubscriber { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Hello world with UniSubscriber"); | ||
|
||
Uni<String> uni = Uni.createFrom().item("Hello, world!"); | ||
|
||
uni.subscribe().withSubscriber(new UniSubscriber<String>() { | ||
@Override | ||
public void onSubscribe(UniSubscription subscription) { | ||
System.out.println("onSubscribe"); | ||
} | ||
|
||
@Override | ||
public void onItem(String item) { | ||
System.out.println("onItem: " + item); | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable failure) { | ||
System.out.println("onFailure: " + failure.getMessage()); | ||
} | ||
}); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
workshop-examples/src/main/java/_01_basics/_03_Uni_From_Supplier.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.Random; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _03_Uni_From_Supplier { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("️⚡️ Uni from supplier"); | ||
|
||
Random random = new Random(); | ||
|
||
Uni<Integer> uniFromSupplier = Uni.createFrom().item(random::nextInt); | ||
|
||
for (var i = 0; i < 5; i++) { | ||
uniFromSupplier.subscribe().with(System.out::println); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
workshop-examples/src/main/java/_01_basics/_04_Uni_From_Supplier_And_State.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _04_Uni_From_Supplier_And_State { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("️⚡️ Uni from supplier with state"); | ||
|
||
Uni<Integer> uniFromSupplierAndState = Uni.createFrom().item(AtomicInteger::new, i -> i.addAndGet(10)); | ||
|
||
for (var i = 0; i < 5; i++) { | ||
uniFromSupplierAndState.subscribe().with(System.out::println); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
workshop-examples/src/main/java/_01_basics/_05_Uni_Deferred.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _05_Uni_Deferred { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("️⚡️ Deferred Uni"); | ||
|
||
AtomicLong ids = new AtomicLong(); | ||
|
||
Uni<Long> deferredUni = Uni.createFrom().deferred(() -> Uni.createFrom().item(ids::incrementAndGet)); | ||
|
||
for (var i = 0; i < 5; i++) { | ||
deferredUni.subscribe().with(System.out::println); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
workshop-examples/src/main/java/_01_basics/_06_Uni_From_Emitter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ForkJoinPool; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _06_Uni_From_Emitter { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
System.out.println("⚡️ Uni from emitter"); | ||
|
||
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); | ||
CountDownLatch emitterLatch = new CountDownLatch(1); | ||
|
||
Uni<String> uniFromEmitter = Uni.createFrom().emitter(emitter -> { | ||
forkJoinPool.submit(() -> { | ||
emitter.complete("Hello"); | ||
emitterLatch.countDown(); | ||
}); | ||
}); | ||
|
||
uniFromEmitter.subscribe().with(System.out::println); | ||
|
||
emitterLatch.await(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
workshop-examples/src/main/java/_01_basics/_07_Unit_From_Emitter_And_State.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _07_Unit_From_Emitter_And_State { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Uni from emitter and state"); | ||
|
||
Uni<Integer> uniFromEmitterAndState = Uni.createFrom() | ||
.emitter(AtomicInteger::new, (i, e) -> e.complete(i.addAndGet(10))); | ||
|
||
for (var i = 0; i < 5; i++) { | ||
uniFromEmitterAndState.subscribe().with(System.out::println); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
workshop-examples/src/main/java/_01_basics/_08_Uni_From_Failure.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.io.IOException; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _08_Uni_From_Failure { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Uni from failure"); | ||
|
||
Uni.createFrom().failure(new IOException("Boom")) | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
|
||
Uni.createFrom().failure(() -> new IOException("Badaboom")) | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
workshop-examples/src/main/java/_01_basics/_09_Uni_From_CompletionStage.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import static java.util.concurrent.CompletableFuture.delayedExecutor; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _09_Uni_From_CompletionStage { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
System.out.println("⚡️ Uni from CompletionStage"); | ||
|
||
var cs = CompletableFuture | ||
.supplyAsync(() -> "Hello!", delayedExecutor(1, TimeUnit.SECONDS)) | ||
.thenApply(String::toUpperCase); | ||
|
||
Uni.createFrom().completionStage(cs) | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
|
||
Thread.sleep(2000); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
workshop-examples/src/main/java/_01_basics/_10_Uni_Misc.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.Optional; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _10_Uni_Misc { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Misc"); | ||
|
||
Uni.createFrom().nothing() | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
|
||
Uni.createFrom().voidItem() | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
|
||
Uni.createFrom().nullItem() | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
|
||
Uni.createFrom().optional(Optional.of("Hello")) | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
|
||
Uni.createFrom().converter(i -> Uni.createFrom().item("[" + i + "]"), 10) | ||
.subscribe().with(System.out::println, failure -> System.out.println(failure.getMessage())); | ||
|
||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
workshop-examples/src/main/java/_01_basics/_11_Uni_Delay.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import static java.util.concurrent.CompletableFuture.delayedExecutor; | ||
import static java.util.concurrent.CompletableFuture.supplyAsync; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _11_Uni_Delay { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Uni delay"); | ||
|
||
Uni.createFrom().item(666) | ||
.onItem().delayIt().by(Duration.ofSeconds(1)) | ||
.subscribe().with(System.out::println); | ||
|
||
System.out.println("⏰"); | ||
|
||
Uni.createFrom().item(666) | ||
.onItem().delayIt() | ||
.until(n -> Uni.createFrom().completionStage( | ||
supplyAsync( | ||
() -> "Ok", | ||
delayedExecutor(5, TimeUnit.SECONDS)))) | ||
.subscribe().with(System.out::println); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
workshop-examples/src/main/java/_01_basics/_12_Uni_Disjoint.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.List; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class _12_Uni_Disjoint { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Uni disjoint"); | ||
|
||
Uni.createFrom().item(List.of(1, 2, 3, 4, 5)) | ||
.onItem().disjoint() | ||
.subscribe().with(System.out::println); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
///usr/bin/env jbang "$0" "$@" ; exit $? | ||
//DEPS io.smallrye.reactive:mutiny:2.4.0 | ||
package _01_basics; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
|
||
public class _13_Multi { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("⚡️ Hello world"); | ||
|
||
// -------------------------------------------------------------------------------------------------- // | ||
|
||
Multi.createFrom().items(1, 2, 3) | ||
.subscribe().with( | ||
subscription -> { | ||
System.out.println("Subscription: " + subscription); | ||
subscription.request(10); | ||
}, | ||
item -> System.out.println("Item: " + item), | ||
failure -> System.out.println("Failure: " + failure.getMessage()), | ||
() -> System.out.println("Completed")); | ||
|
||
// -------------------------------------------------------------------------------------------------- // | ||
|
||
System.out.println("----"); | ||
|
||
Multi.createFrom().range(10, 15) | ||
.subscribe().with(System.out::println); | ||
|
||
var randomNumbers = Stream | ||
.generate(ThreadLocalRandom.current()::nextInt) | ||
.limit(5) | ||
.collect(Collectors.toList()); | ||
|
||
// -------------------------------------------------------------------------------------------------- // | ||
|
||
System.out.println("----"); | ||
|
||
Multi.createFrom().iterable(randomNumbers) | ||
.subscribe().with(System.out::println); | ||
} | ||
} |
Oops, something went wrong.