Skip to content

Commit 1f5c36f

Browse files
authored
Merge pull request #40620 from Ladicek/cdi-reactive-pitfalls
ArC: initial documentation of CDI pitfalls with reactive programming
2 parents fb25191 + 7a06a23 commit 1f5c36f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

docs/src/main/asciidoc/cdi-reference.adoc

+41
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,47 @@ public class NoopAsyncObserverExceptionHandler implements AsyncObserverException
10691069
}
10701070
----
10711071

1072+
[[reactive_pitfalls]]
1073+
== Pitfalls with Reactive Programming
1074+
1075+
CDI is a purely synchronous framework.
1076+
Its notion of asynchrony is very limited and based solely on thread pools and thread offloading.
1077+
Therefore, there is a number of pitfalls when using CDI together with reactive programming.
1078+
1079+
=== Detecting When Blocking Is Allowed
1080+
1081+
The `io.quarkus.runtime.BlockingOperationControl#isBlockingAllowed()` method can be used to detect whether blocking is allowed on the current thread.
1082+
When it is not, and you need to perform a blocking operation, you have to offload it to another thread.
1083+
The easiest way is to use the `Vertx.executeBlocking()` method:
1084+
1085+
[source,java]
1086+
----
1087+
import io.quarkus.runtime.BlockingOperationControl;
1088+
1089+
@ApplicationScoped
1090+
public class MyBean {
1091+
@Inject
1092+
Vertx vertx;
1093+
1094+
@PostConstruct
1095+
void init() {
1096+
if (BlockingOperationControl.isBlockingAllowed()) {
1097+
somethingThatBlocks();
1098+
} else {
1099+
vertx.executeBlocking(() -> {
1100+
somethingThatBlocks();
1101+
return null;
1102+
});
1103+
}
1104+
}
1105+
1106+
void somethingThatBlocks() {
1107+
// use the file system or JDBC, call a REST service, etc.
1108+
Thread.sleep(5000);
1109+
}
1110+
}
1111+
----
1112+
10721113
[[build_time_apis]]
10731114
== Build Time Extensions
10741115

0 commit comments

Comments
 (0)