optimize: 非 spring 环境启动自定义线程池时不发送报警消息.

pull/161/head
chen.ma 3 years ago
parent 2d037a48c6
commit bd745ab6c2

@ -68,4 +68,13 @@ public class ApplicationContextHolder implements ApplicationContextAware {
return CONTEXT.findAnnotationOnBean(beanName, annotationType); return CONTEXT.findAnnotationOnBean(beanName, annotationType);
} }
/**
* Get ApplicationContext.
*
* @return
*/
public static ApplicationContext getInstance() {
return CONTEXT;
}
} }

@ -2,11 +2,14 @@ package com.github.dynamic.threadpool.starter.alarm;
import com.github.dynamic.threadpool.common.config.ApplicationContextHolder; import com.github.dynamic.threadpool.common.config.ApplicationContextHolder;
import com.github.dynamic.threadpool.common.model.PoolParameterInfo; import com.github.dynamic.threadpool.common.model.PoolParameterInfo;
import com.github.dynamic.threadpool.starter.config.MessageAlarmConfig;
import com.github.dynamic.threadpool.starter.toolkit.CalculateUtil; import com.github.dynamic.threadpool.starter.toolkit.CalculateUtil;
import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor; import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor;
import com.github.dynamic.threadpool.starter.toolkit.thread.ResizableCapacityLinkedBlockIngQueue; import com.github.dynamic.threadpool.starter.toolkit.thread.ResizableCapacityLinkedBlockIngQueue;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.Optional;
/** /**
* Alarm manage. * Alarm manage.
* *
@ -19,7 +22,9 @@ public class ThreadPoolAlarmManage {
private static final SendMessageService SEND_MESSAGE_SERVICE; private static final SendMessageService SEND_MESSAGE_SERVICE;
static { static {
SEND_MESSAGE_SERVICE = ApplicationContextHolder.getBean("sendMessageService", SendMessageService.class); SEND_MESSAGE_SERVICE = Optional.ofNullable(ApplicationContextHolder.getInstance())
.map(each -> each.getBean(MessageAlarmConfig.SEND_MESSAGE_BEAN_NAME, SendMessageService.class))
.orElse(null);
} }
/** /**
@ -47,7 +52,7 @@ public class ThreadPoolAlarmManage {
* @param threadPoolExecutor * @param threadPoolExecutor
*/ */
public static void checkPoolLivenessAlarm(boolean isCore, CustomThreadPoolExecutor threadPoolExecutor) { public static void checkPoolLivenessAlarm(boolean isCore, CustomThreadPoolExecutor threadPoolExecutor) {
if (isCore) { if (isCore || SEND_MESSAGE_SERVICE == null) {
return; return;
} }
int activeCount = threadPoolExecutor.getActiveCount(); int activeCount = threadPoolExecutor.getActiveCount();
@ -64,8 +69,10 @@ public class ThreadPoolAlarmManage {
* @param threadPoolExecutor * @param threadPoolExecutor
*/ */
public static void checkPoolRejectAlarm(CustomThreadPoolExecutor threadPoolExecutor) { public static void checkPoolRejectAlarm(CustomThreadPoolExecutor threadPoolExecutor) {
if (SEND_MESSAGE_SERVICE != null) {
SEND_MESSAGE_SERVICE.sendAlarmMessage(threadPoolExecutor); SEND_MESSAGE_SERVICE.sendAlarmMessage(threadPoolExecutor);
} }
}
/** /**
* Send thread pool configuration change message. * Send thread pool configuration change message.
@ -73,7 +80,9 @@ public class ThreadPoolAlarmManage {
* @param parameter * @param parameter
*/ */
public static void sendPoolConfigChange(PoolParameterInfo parameter) { public static void sendPoolConfigChange(PoolParameterInfo parameter) {
if (SEND_MESSAGE_SERVICE != null) {
SEND_MESSAGE_SERVICE.sendChangeMessage(parameter); SEND_MESSAGE_SERVICE.sendChangeMessage(parameter);
} }
}
} }

@ -26,8 +26,10 @@ public class MessageAlarmConfig {
private ConfigurableEnvironment environment; private ConfigurableEnvironment environment;
@Bean public static final String SEND_MESSAGE_BEAN_NAME = "sendMessageService";
@DependsOn("applicationContextHolder") @DependsOn("applicationContextHolder")
@Bean(MessageAlarmConfig.SEND_MESSAGE_BEAN_NAME)
public SendMessageService sendMessageService() { public SendMessageService sendMessageService() {
return new BaseSendMessageService(properties.getNotifys()); return new BaseSendMessageService(properties.getNotifys());
} }

Loading…
Cancel
Save