Skip to content

Commit

Permalink
feat: import the Mutiny workshop examples into the main repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jponge committed Sep 10, 2023
1 parent 7baf62f commit f5705ff
Show file tree
Hide file tree
Showing 69 changed files with 2,277 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<module>kotlin</module>
<module>bom</module>
<module>math</module>
<module>workshop-examples</module>
</modules>

<properties>
Expand Down
36 changes: 36 additions & 0 deletions workshop-examples/pom.xml
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>
16 changes: 16 additions & 0 deletions workshop-examples/src/main/java/_01_basics/_01_Uni.java
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);
}
}
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());
}
});
}
}
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);
}
}
}
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 workshop-examples/src/main/java/_01_basics/_05_Uni_Deferred.java
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);
}
}
}
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();
}
}
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);
}
}
}
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()));
}
}
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 workshop-examples/src/main/java/_01_basics/_10_Uni_Misc.java
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 workshop-examples/src/main/java/_01_basics/_11_Uni_Delay.java
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 workshop-examples/src/main/java/_01_basics/_12_Uni_Disjoint.java
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);
}
}
47 changes: 47 additions & 0 deletions workshop-examples/src/main/java/_01_basics/_13_Multi.java
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);
}
}
Loading

0 comments on commit f5705ff

Please sign in to comment.