Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArC: initial documentation of CDI pitfalls with reactive programming #40620

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/src/main/asciidoc/cdi-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,47 @@
}
----

[[reactive_pitfalls]]
== Pitfalls with Reactive Programming

CDI is a purely synchronous framework.

Check warning on line 1075 in docs/src/main/asciidoc/cdi-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Spelling] Use correct American English spelling. Did you really mean 'asynchrony'? Raw Output: {"message": "[Quarkus.Spelling] Use correct American English spelling. Did you really mean 'asynchrony'?", "location": {"path": "docs/src/main/asciidoc/cdi-reference.adoc", "range": {"start": {"line": 1075, "column": 36}}}, "severity": "WARNING"}
Its notion of asynchrony is very limited and based solely on thread pools and thread offloading.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that the case for async event/observers ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's the only place in CDI where asynchrony is mentioned, and it's based on thread offloading.

Therefore, there is a number of pitfalls when using CDI together with reactive programming.

Check warning on line 1077 in docs/src/main/asciidoc/cdi-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'several' rather than 'a number of' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'several' rather than 'a number of' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/cdi-reference.adoc", "range": {"start": {"line": 1077, "column": 3}}}, "severity": "WARNING"}

Check warning on line 1077 in docs/src/main/asciidoc/cdi-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsSuggestions] Depending on the context, consider using 'by using' or 'that uses' rather than 'using'. Raw Output: {"message": "[Quarkus.TermsSuggestions] Depending on the context, consider using 'by using' or 'that uses' rather than 'using'.", "location": {"path": "docs/src/main/asciidoc/cdi-reference.adoc", "range": {"start": {"line": 1077, "column": 28}}}, "severity": "INFO"}

=== Detecting When Blocking Is Allowed

Check warning on line 1079 in docs/src/main/asciidoc/cdi-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in '5.1. Detecting When Blocking Is Allowed'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in '5.1. Detecting When Blocking Is Allowed'.", "location": {"path": "docs/src/main/asciidoc/cdi-reference.adoc", "range": {"start": {"line": 1079, "column": 1}}}, "severity": "INFO"}

Check warning on line 1079 in docs/src/main/asciidoc/cdi-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.HeadingPunctuation] Do not use end punctuation in headings. Raw Output: {"message": "[Quarkus.HeadingPunctuation] Do not use end punctuation in headings.", "location": {"path": "docs/src/main/asciidoc/cdi-reference.adoc", "range": {"start": {"line": 1079, "column": 1}}}, "severity": "INFO"}

The `io.quarkus.runtime.BlockingOperationControl#isBlockingAllowed()` method can be used to detect whether blocking is allowed on the current thread.
When it is not, and you need to perform a blocking operation, you have to offload it to another thread.

Check warning on line 1082 in docs/src/main/asciidoc/cdi-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'need to'. Raw Output: {"message": "[Quarkus.Fluff] Depending on the context, consider using 'Rewrite the sentence, or use 'must', instead of' rather than 'need to'.", "location": {"path": "docs/src/main/asciidoc/cdi-reference.adoc", "range": {"start": {"line": 1082, "column": 7}}}, "severity": "INFO"}
mkouba marked this conversation as resolved.
Show resolved Hide resolved
The easiest way is to use the `Vertx.executeBlocking()` method:

[source,java]
----
import io.quarkus.runtime.BlockingOperationControl;

@ApplicationScoped
public class MyBean {
@Inject
Vertx vertx;

@PostConstruct
void init() {
if (BlockingOperationControl.isBlockingAllowed()) {
somethingThatBlocks();
} else {
vertx.executeBlocking(() -> {
somethingThatBlocks();
return null;
});
}
}

void somethingThatBlocks() {
// use the file system or JDBC, call a REST service, etc.
Thread.sleep(5000);
}
}
----

[[build_time_apis]]
== Build Time Extensions

Expand Down
Loading