forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-7932: CompletableFuture vs Mono (eugenp#16574)
- Loading branch information
Showing
9 changed files
with
200 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,3 @@ | ||
## Reactor Core (2) | ||
|
||
### Relevant articles |
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 @@ | ||
<?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> | ||
<groupId>org.baeldung</groupId> | ||
<artifactId>reactor-core-2</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>reactor-core-2</name> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.projectreactor</groupId> | ||
<artifactId>reactor-core</artifactId> | ||
<version>${reactor.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.projectreactor</groupId> | ||
<artifactId>reactor-test</artifactId> | ||
<version>${reactor.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.projectreactor.addons</groupId> | ||
<artifactId>reactor-extra</artifactId> | ||
<version>${reactor-extra.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>${lombok.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<reactor.version>3.6.0</reactor.version> | ||
<reactor-extra.version>3.5.1</reactor-extra.version> | ||
</properties> | ||
|
||
</project> |
41 changes: 41 additions & 0 deletions
41
reactor-core-2/src/main/java/com/baeldung/reactor/completablefuturevsmono/AsyncApiCall.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,41 @@ | ||
package com.baeldung.reactor.completablefuturevsmono; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.BiConsumer; | ||
|
||
public class AsyncApiCall { | ||
|
||
public static void main(String[] args) { | ||
AsyncApiCall apiCall = new AsyncApiCall(); | ||
CompletableFuture<String> completableFuture = apiCall.nonBlockingApiCall("test parameter"); | ||
|
||
completableFuture.thenAccept(result -> System.out.println("Future completed successfully: " + result)) | ||
.exceptionally(error -> { | ||
System.out.println("Future completed exceptionally: " + error.getMessage()); | ||
return null; | ||
}); | ||
} | ||
|
||
public void myAsyncCall(String param, BiConsumer<String, Throwable> callback) { | ||
new Thread(() -> { | ||
try { | ||
Thread.sleep(1000); | ||
callback.accept("Response from API with param: " + param, null); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
}).start(); | ||
} | ||
|
||
public CompletableFuture<String> nonBlockingApiCall(String param) { | ||
CompletableFuture<String> completableFuture = new CompletableFuture<>(); | ||
myAsyncCall(param, (result, error) -> { | ||
if (error != null) { | ||
completableFuture.completeExceptionally(error); | ||
} else { | ||
completableFuture.complete(result); | ||
} | ||
}); | ||
return completableFuture; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...e-2/src/main/java/com/baeldung/reactor/completablefuturevsmono/CompletableFutureDemo.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 @@ | ||
package com.baeldung.reactor.completablefuturevsmono; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class CompletableFutureDemo { | ||
|
||
public static void main(String[] args) { | ||
|
||
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread() | ||
.interrupt(); | ||
} | ||
return "Finished completableFuture"; | ||
}); | ||
|
||
try { | ||
completableFuture.get(); // This blocks the main thread | ||
} catch (InterruptedException | ExecutionException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
reactor-core-2/src/main/java/com/baeldung/reactor/completablefuturevsmono/MonoDemo.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,17 @@ | ||
package com.baeldung.reactor.completablefuturevsmono; | ||
|
||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
|
||
public class MonoDemo { | ||
|
||
public static void main(String[] args) { | ||
Mono<String> reactiveMono = Mono.fromCallable(() -> { | ||
Thread.sleep(1000); | ||
return "Reactive Data"; | ||
}) | ||
.subscribeOn(Schedulers.boundedElastic()); | ||
|
||
reactiveMono.subscribe(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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||
</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
28 changes: 28 additions & 0 deletions
28
...src/test/java/com/baeldung/reactor/completablefuturevsmono/CompletableFutureUnitTest.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,28 @@ | ||
package com.baeldung.reactor.completablefuturevsmono; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class CompletableFutureUnitTest { | ||
|
||
@Test | ||
void whenGetMethodOnCompletableFutureItBlocksMainThread() { | ||
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { | ||
try { | ||
Thread.sleep(5000); | ||
return "Delayed result"; | ||
} catch (InterruptedException e) { | ||
Thread.currentThread() | ||
.interrupt(); | ||
return null; | ||
} | ||
}); | ||
|
||
assertThrows(TimeoutException.class, () -> future.get(2, TimeUnit.SECONDS)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
reactor-core-2/src/test/java/com/baeldung/reactor/completablefuturevsmono/MonoUnitTest.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,23 @@ | ||
package com.baeldung.reactor.completablefuturevsmono; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import reactor.core.publisher.Mono; | ||
import reactor.core.scheduler.Schedulers; | ||
import reactor.test.StepVerifier; | ||
|
||
class MonoUnitTest { | ||
|
||
@Test | ||
void givenMonoAssureItRunsCorrectly() { | ||
Mono<String> reactiveMono = Mono.fromCallable(() -> { | ||
Thread.sleep(1000); | ||
return "Reactive Data"; | ||
}) | ||
.subscribeOn(Schedulers.boundedElastic()); | ||
|
||
StepVerifier.create(reactiveMono) | ||
.expectNext("Reactive Data") | ||
.verifyComplete(); | ||
} | ||
} |