Skip to content

Commit

Permalink
Merge pull request #14894 from cescoffier/avoid-await-in-mailer
Browse files Browse the repository at this point in the history
Avoid retrieving the rendered content using `await` when building a Mail from a Qute template
  • Loading branch information
gsmet authored Feb 8, 2021
2 parents f7f846c + 98e054c commit 63603c1
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public CompletionStage<? extends String> get() {
if (results.isEmpty()) {
throw new IllegalStateException("No suitable template variant found");
}
List<Uni<String>> unis = results.stream().map(Result::getValue).collect(Collectors.toList());
List<Uni<String>> unis = results.stream().map(Result::resolve).collect(Collectors.toList());
return Uni.combine().all().unis(unis)
.combinedWith(combine(results))
.chain(new Function<Mail, Uni<? extends Void>>() {
Expand All @@ -132,13 +132,14 @@ public Uni<? extends Void> apply(Mail m) {
private Function<List<?>, Mail> combine(List<Result> results) {
return new Function<List<?>, Mail>() {
@Override
public Mail apply(List<?> ignored) {
for (Result res : results) {
// We can safely access the content here: 1. it has been resolved, 2. it's cached.
String content = res.value.await().indefinitely();
if (res.variant.getContentType().equals(Variant.TEXT_HTML)) {
public Mail apply(List<?> resolved) {
for (int i = 0; i < resolved.size(); i++) {
Result result = results.get(i);
// We can safely cast, as we know that the results are Strings.
String content = (String) resolved.get(i);
if (result.variant.getContentType().equals(Variant.TEXT_HTML)) {
mail.setHtml(content);
} else if (res.variant.getContentType().equals(Variant.TEXT_PLAIN)) {
} else if (result.variant.getContentType().equals(Variant.TEXT_PLAIN)) {
mail.setText(content);
}
}
Expand All @@ -154,10 +155,10 @@ static class Result {

public Result(Variant variant, Uni<String> result) {
this.variant = variant;
this.value = result.cache();
this.value = result;
}

Uni<String> getValue() {
Uni<String> resolve() {
return value;
}
}
Expand Down

0 comments on commit 63603c1

Please sign in to comment.