mirror of https://github.com/longtai-cn/hippo4j
parent
642461aa8d
commit
984a7d44f2
@ -0,0 +1,29 @@
|
||||
package com.github.dynamic.threadpool.starter.alarm;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Alarm Config.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 16:09
|
||||
*/
|
||||
@Data
|
||||
public class AlarmConfig {
|
||||
|
||||
/**
|
||||
* type
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* url
|
||||
*/
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* token
|
||||
*/
|
||||
private String token;
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.github.dynamic.threadpool.starter.alarm;
|
||||
|
||||
import com.github.dynamic.threadpool.common.config.ApplicationContextHolder;
|
||||
import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Base Send Message Service.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 15:34
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class BaseSendMessageService implements InitializingBean, SendMessageService {
|
||||
|
||||
@NonNull
|
||||
private final List<AlarmConfig> alarmConfigs;
|
||||
|
||||
private final List<SendMessageHandler> sendMessageHandlers = new ArrayList(4);
|
||||
|
||||
@Override
|
||||
public void sendMessage(CustomThreadPoolExecutor threadPoolExecutor) {
|
||||
for (SendMessageHandler messageHandler : sendMessageHandlers) {
|
||||
try {
|
||||
messageHandler.sendMessage(alarmConfigs, threadPoolExecutor);
|
||||
} catch (Exception ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
Map<String, SendMessageHandler> sendMessageHandlerMap =
|
||||
ApplicationContextHolder.getBeansOfType(SendMessageHandler.class);
|
||||
sendMessageHandlerMap.values().forEach(each -> sendMessageHandlers.add(each));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.github.dynamic.threadpool.starter.alarm;
|
||||
|
||||
/**
|
||||
* Send Message Enum.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 15:50
|
||||
*/
|
||||
public enum SendMessageEnum {
|
||||
|
||||
DING
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.github.dynamic.threadpool.starter.alarm;
|
||||
|
||||
import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Send Message Handler.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 15:44
|
||||
*/
|
||||
public interface SendMessageHandler {
|
||||
|
||||
/**
|
||||
* getType.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getType();
|
||||
|
||||
/**
|
||||
* sendMessage.
|
||||
*
|
||||
* @param alarmConfigs
|
||||
* @param threadPoolExecutor
|
||||
*/
|
||||
void sendMessage(List<AlarmConfig> alarmConfigs, CustomThreadPoolExecutor threadPoolExecutor);
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.github.dynamic.threadpool.starter.alarm;
|
||||
|
||||
import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* Send Msg.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 15:31
|
||||
*/
|
||||
public interface SendMessageService {
|
||||
|
||||
/**
|
||||
* sendMessage.
|
||||
*
|
||||
* @param threadPoolExecutor
|
||||
*/
|
||||
void sendMessage(CustomThreadPoolExecutor threadPoolExecutor);
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.github.dynamic.threadpool.starter.alarm;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* ThreadPool Alarm.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 13:13
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ThreadPoolAlarm {
|
||||
|
||||
/**
|
||||
* 是否报警
|
||||
*/
|
||||
private Boolean isAlarm;
|
||||
|
||||
/**
|
||||
* 活跃度报警
|
||||
*/
|
||||
private Integer livenessAlarm;
|
||||
|
||||
/**
|
||||
* 容量报警
|
||||
*/
|
||||
private Integer capacityAlarm;
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.github.dynamic.threadpool.starter.alarm;
|
||||
|
||||
import com.github.dynamic.threadpool.common.config.ApplicationContextHolder;
|
||||
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.ResizableCapacityLinkedBlockIngQueue;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Alarm Manage.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 14:13
|
||||
*/
|
||||
@Slf4j
|
||||
public class ThreadPoolAlarmManage {
|
||||
|
||||
private static final SendMessageService SEND_MESSAGE_SERVICE;
|
||||
|
||||
static {
|
||||
SEND_MESSAGE_SERVICE = ApplicationContextHolder.getBean("sendMessageService", SendMessageService.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* checkPoolCapacityAlarm.
|
||||
*
|
||||
* @param threadPoolExecutor
|
||||
*/
|
||||
public static void checkPoolCapacityAlarm(CustomThreadPoolExecutor threadPoolExecutor) {
|
||||
ThreadPoolAlarm threadPoolAlarm = threadPoolExecutor.getThreadPoolAlarm();
|
||||
ResizableCapacityLinkedBlockIngQueue blockIngQueue =
|
||||
(ResizableCapacityLinkedBlockIngQueue) threadPoolExecutor.getQueue();
|
||||
|
||||
int queueSize = blockIngQueue.size();
|
||||
int capacity = queueSize + blockIngQueue.remainingCapacity();
|
||||
int divide = CalculateUtil.divide(queueSize, capacity);
|
||||
if (divide > threadPoolAlarm.getCapacityAlarm()) {
|
||||
SEND_MESSAGE_SERVICE.sendMessage(threadPoolExecutor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checkPoolLivenessAlarm.
|
||||
*
|
||||
* @param isCore
|
||||
* @param threadPoolExecutor
|
||||
*/
|
||||
public static void checkPoolLivenessAlarm(boolean isCore, CustomThreadPoolExecutor threadPoolExecutor) {
|
||||
if (isCore) {
|
||||
return;
|
||||
}
|
||||
int activeCount = threadPoolExecutor.getActiveCount();
|
||||
int maximumPoolSize = threadPoolExecutor.getMaximumPoolSize();
|
||||
int divide = CalculateUtil.divide(activeCount, maximumPoolSize);
|
||||
if (divide > threadPoolExecutor.getThreadPoolAlarm().getLivenessAlarm()) {
|
||||
SEND_MESSAGE_SERVICE.sendMessage(threadPoolExecutor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checkPoolRejectAlarm.
|
||||
*
|
||||
* @param threadPoolExecutor
|
||||
*/
|
||||
public static void checkPoolRejectAlarm(CustomThreadPoolExecutor threadPoolExecutor) {
|
||||
SEND_MESSAGE_SERVICE.sendMessage(threadPoolExecutor);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.github.dynamic.threadpool.starter.config;
|
||||
|
||||
import com.github.dynamic.threadpool.common.model.InstanceInfo;
|
||||
import com.github.dynamic.threadpool.starter.alarm.BaseSendMessageService;
|
||||
import com.github.dynamic.threadpool.starter.alarm.DingSendMessageHandler;
|
||||
import com.github.dynamic.threadpool.starter.alarm.SendMessageHandler;
|
||||
import com.github.dynamic.threadpool.starter.alarm.SendMessageService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* Message Alarm Config.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/8/15 15:39
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class MessageAlarmConfig {
|
||||
|
||||
private final BootstrapProperties properties;
|
||||
|
||||
private final InstanceInfo instanceInfo;
|
||||
|
||||
private ConfigurableEnvironment environment;
|
||||
|
||||
@Bean
|
||||
@DependsOn("applicationContextHolder")
|
||||
public SendMessageService sendMessageService() {
|
||||
return new BaseSendMessageService(properties.getAlarms());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SendMessageHandler dingSendMessageHandler() {
|
||||
String active = environment.getProperty("spring.profiles.active", Strings.EMPTY);
|
||||
return new DingSendMessageHandler(active, instanceInfo);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue