Skip to content

Commit

Permalink
Document the Mutiny Vert.x context-aware scheduler helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jponge committed Sep 15, 2023
1 parent fd18895 commit 3c018b1
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/src/main/asciidoc/vertx-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,52 @@ On macOS Sierra and above you can enable the following socket options:
quarkus.http.so-reuse-port=true
----

== Use a Vert.x context-aware scheduler

Some Mutiny operators need to schedule work on an executor thread pool.

Check warning on line 1013 in docs/src/main/asciidoc/vertx-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/vertx-reference.adoc", "range": {"start": {"line": 1013, "column": 1}}}, "severity": "INFO"}
A good example is `.onItem().delayIt().by(Duration.ofMillis(10)` as it needs such an executor to delay emissions.

The default executor is returned by `io.smallrye.mutiny.infrastructure.Infrastructure` and it is already configured and managed by Quarkus.

That being said, there are cases where you need to make sure that an operation is run on a Vert.x (duplicated) context and not just on any random thread.

Check warning on line 1018 in docs/src/main/asciidoc/vertx-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/vertx-reference.adoc", "range": {"start": {"line": 1018, "column": 44}}}, "severity": "INFO"}

Check warning on line 1018 in docs/src/main/asciidoc/vertx-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'verify' rather than 'make sure' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'verify' rather than 'make sure' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/vertx-reference.adoc", "range": {"start": {"line": 1018, "column": 52}}}, "severity": "WARNING"}

The `io.smallrye.mutiny.vertx.core.ContextAwareScheduler` interface offers an API to obtain context-aware schedulers.
Such a scheduler is configured with:

1. a delegate `ScheduledExecutorService` of your choice (hint: you can reuse `Infrastructure.getDefaultWorkerPool()`), and
2. a context fetching strategy among:
- an explicit `Context`, or
- calling `Vertx::getOrCreateContext()` either on the current thread or later when the scheduling request happens, or
- calling `Vertx::currentContext()`, which fails if the current thread is not a Vert.x thread.

Here is a sample where `ContextAwareScheduler` is used:

[source,java]
----
class MyVerticle extends AbstractVerticle {
@Override
public Uni<Void> asyncStart() {
vertx.getOrCreateContext().put("foo", "bar");
var delegate = Infrastructure.getDefaultWorkerPool();
var scheduler = ContextAwareScheduler.delegatingTo(delegate)
.withCurrentContext();
return Uni.createFrom().voidItem()
.onItem().delayIt().onExecutor(scheduler).by(Duration.ofMillis(10))
.onItem().invoke(() -> {
// Prints "bar"
var ctx = vertx.getOrCreateContext();
System.out.println(ctx.get("foo"));
});
}
}
----

In this example a scheduler is created by capturing the context of the Vert.x event-loop that calls `asyncStart()`.
The `delayIt` operator uses that scheduler, and we can check that the context that we get in `invoke` is a Vert.x duplicated context where the data for key `"foo"` has been propagated.

Check warning on line 1055 in docs/src/main/asciidoc/vertx-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.CaseSensitiveTerms] Use 'UNIX' rather than 'Unix'. Raw Output: {"message": "[Quarkus.CaseSensitiveTerms] Use 'UNIX' rather than 'Unix'.", "location": {"path": "docs/src/main/asciidoc/vertx-reference.adoc", "range": {"start": {"line": 1055, "column": 174}}}, "severity": "INFO"}

== Use a Unix domain socket

Check warning on line 1057 in docs/src/main/asciidoc/vertx-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.CaseSensitiveTerms] Use 'UNIX' rather than 'Unix'. Raw Output: {"message": "[Quarkus.CaseSensitiveTerms] Use 'UNIX' rather than 'Unix'.", "location": {"path": "docs/src/main/asciidoc/vertx-reference.adoc", "range": {"start": {"line": 1057, "column": 23}}}, "severity": "INFO"}

Listening on a Unix domain socket allows us to dispense with the overhead of TCP
Expand Down

0 comments on commit 3c018b1

Please sign in to comment.