-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Add dedicated thread pool for RestTemplate in alert notificati… #3481
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
43a26ac
a52364d
58b6674
956a073
ee52c90
cfb7f2e
d54e188
475d177
b95c588
0134dee
570b61b
a90301f
8e58f00
9f5db16
afe66b1
432f234
3b2ad36
bc08ee8
276324c
4a7f522
840683e
ceb194a
078db96
88f71b3
01ac2bb
14e51b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.ResourceBundle; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.Executor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.hertzbeat.alert.AlerterProperties; | ||
| import org.apache.hertzbeat.common.entity.alerter.GroupAlert; | ||
|
|
@@ -34,6 +36,7 @@ | |
| import org.apache.hertzbeat.common.util.ResourceBundleUtil; | ||
| import org.apache.hertzbeat.alert.notice.AlertNotifyHandler; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; | ||
| import org.springframework.web.client.RestTemplate; | ||
|
|
@@ -50,6 +53,9 @@ abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler { | |
| protected RestTemplate restTemplate; | ||
| @Autowired | ||
| protected AlerterProperties alerterProperties; | ||
| @Autowired | ||
| @Qualifier("restTemplateThreadPool") | ||
| protected Executor restTemplateThreadPool; | ||
|
|
||
|
|
||
| protected String renderContent(NoticeTemplate noticeTemplate, GroupAlert alert) throws TemplateException, IOException { | ||
|
|
@@ -113,6 +119,19 @@ protected String escapeJsonStr(String jsonStr){ | |
| return sb.toString(); | ||
| } | ||
|
|
||
| protected CompletableFuture<Void> sendAsync(org.apache.hertzbeat.common.entity.alerter.NoticeReceiver receiver, | ||
| org.apache.hertzbeat.common.entity.alerter.NoticeTemplate noticeTemplate, | ||
| GroupAlert alert) { | ||
| return CompletableFuture.runAsync(() -> { | ||
| try { | ||
| send(receiver, noticeTemplate, alert); | ||
| } catch (Exception e) { | ||
| log.error("Async alert notification failed", e); | ||
| throw new RuntimeException(e); | ||
| } | ||
| }, restTemplateThreadPool); | ||
| } | ||
|
||
|
|
||
| @EventListener(SystemConfigChangeEvent.class) | ||
| public void onEvent(SystemConfigChangeEvent event) { | ||
| log.info("{} receive system config change event: {}.", this.getClass().getName(), event.getSource()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
| package org.apache.hertzbeat.manager.config; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.ThreadPoolExecutor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import okhttp3.ConnectionPool; | ||
| import okhttp3.OkHttpClient; | ||
|
|
@@ -26,11 +28,11 @@ | |
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.client.ClientHttpRequestFactory; | ||
| import org.springframework.http.client.OkHttp3ClientHttpRequestFactory; | ||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
| import org.springframework.web.client.RestTemplate; | ||
|
|
||
| /** | ||
| * restTemplate config | ||
| * todo thread pool | ||
| */ | ||
| @Configuration | ||
| public class RestTemplateConfig { | ||
|
|
@@ -58,4 +60,16 @@ public ClientHttpRequestFactory simpleClientHttpRequestFactory() { | |
| ); | ||
| } | ||
|
|
||
| @Bean("restTemplateThreadPool") | ||
| public Executor restTemplateThreadPool() { | ||
| ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
| executor.setCorePoolSize(10); | ||
| executor.setMaxPoolSize(50); | ||
| executor.setQueueCapacity(200); | ||
|
||
| executor.setThreadNamePrefix("RestTemplate-"); | ||
| executor.setKeepAliveSeconds(60); | ||
| executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); | ||
| executor.initialize(); | ||
| return executor; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi can you describe why use the
CompleableFuturehere,sendNoticeMsgdoes not need to return a result; it just needs to handle exceptions.