Skip to content

Commit

Permalink
BAEL-7932: CompletableFuture vs Mono (eugenp#16574)
Browse files Browse the repository at this point in the history
  • Loading branch information
harry9656 authored May 20, 2024
1 parent 5a9705e commit 1e48586
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@
<module>quarkus-modules</module>
<module>reactive-systems</module>
<module>reactor-core</module>
<module>reactor-core-2</module>
<module>rsocket</module>
<module>rule-engines-modules</module>
<module>rxjava-modules</module>
Expand Down Expand Up @@ -1033,6 +1034,7 @@
<module>quarkus-modules</module>
<module>reactive-systems</module>
<module>reactor-core</module>
<module>reactor-core-2</module>
<module>rsocket</module>
<module>rule-engines-modules</module>
<module>rxjava-modules</module>
Expand Down
3 changes: 3 additions & 0 deletions reactor-core-2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Reactor Core (2)

### Relevant articles
47 changes: 47 additions & 0 deletions reactor-core-2/pom.xml
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>
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;
}
}
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();
}
}
}
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);
}
}
13 changes: 13 additions & 0 deletions reactor-core-2/src/main/resources/logback.xml
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>
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));
}
}
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();
}
}

0 comments on commit 1e48586

Please sign in to comment.