tomsun28 commented on code in PR #3481:
URL: https://github.com/apache/hertzbeat/pull/3481#discussion_r2159788508
##########
hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/notice/AlertNoticeDispatch.java:
##########
@@ -121,14 +129,31 @@ public void dispatchAlarm(GroupAlert groupAlert) {
}
private void sendNotify(GroupAlert alert) {
- matchNoticeRulesByAlert(alert).ifPresent(noticeRules ->
noticeRules.forEach(rule -> workerPool.executeNotify(() -> rule.getReceiverId()
- .forEach(receiverId -> {
- try {
- sendNoticeMsg(getOneReceiverById(receiverId),
- getOneTemplateById(rule.getTemplateId()),
alert);
- } catch (AlertNoticeException e) {
- log.warn("DispatchTask sendNoticeMsg error, message:
{}", e.getMessage());
- }
- }))));
+ matchNoticeRulesByAlert(alert).ifPresent(noticeRules ->
noticeRules.forEach(rule -> workerPool.executeNotify(() -> {
+ List<CompletableFuture<Void>> futures =
rule.getReceiverId().stream()
+ .map(receiverId ->
sendNoticeAsync(getOneReceiverById(receiverId),
+ getOneTemplateById(rule.getTemplateId()), alert))
+ .toList();
+
+ CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
+ .whenComplete((result, exception) -> {
+ if (exception != null) {
+ log.warn("Some async notifications failed",
exception);
+ } else {
+ log.debug("All notifications completed for alert:
{}", alert.getGroupLabels());
+ }
+ });
+ })));
Review Comment:
hi can you describe why use the `CompleableFuture` here, `sendNoticeMsg`
does not need to return a result; it just needs to handle exceptions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]