@ -6,7 +6,12 @@ import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient ;
import com.dingtalk.api.request.OapiRobotSendRequest ;
import com.github.dynamic.threadpool.common.model.InstanceInfo ;
import com.github.dynamic.threadpool.common.model.PoolParameterInfo ;
import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage ;
import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor ;
import com.github.dynamic.threadpool.starter.toolkit.thread.QueueTypeEnum ;
import com.github.dynamic.threadpool.starter.toolkit.thread.RejectedTypeEnum ;
import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap ;
import com.taobao.api.ApiException ;
import lombok.NonNull ;
import lombok.RequiredArgsConstructor ;
@ -16,9 +21,10 @@ import java.util.List;
import java.util.Objects ;
import java.util.Optional ;
import java.util.concurrent.BlockingQueue ;
import java.util.concurrent.TimeUnit ;
/ * *
* Ding Send .
* Send ding notification message .
*
* @author chen . ma
* @date 2021 / 8 / 15 15 : 49
@ -39,22 +45,28 @@ public class DingSendMessageHandler implements SendMessageHandler {
}
@Override
public void send Message( List < AlarmConfig > alarmConfigs , CustomThreadPoolExecutor pool ) {
public void send Alarm Message( List < AlarmConfig > alarmConfigs , CustomThreadPoolExecutor pool ) {
Optional < AlarmConfig > alarmConfigOptional = alarmConfigs . stream ( )
. filter ( each - > Objects . equals ( each . getType ( ) , getType ( ) ) )
. findFirst ( ) ;
alarmConfigOptional . ifPresent ( each - > ding SendMessage( each , pool ) ) ;
alarmConfigOptional . ifPresent ( each - > ding Alarm SendMessage( each , pool ) ) ;
}
public void dingSendMessage ( AlarmConfig alarmConfig , CustomThreadPoolExecutor pool ) {
String serverUrl = alarmConfig . getUrl ( ) + alarmConfig . getToken ( ) ;
@Override
public void sendChangeMessage ( List < AlarmConfig > alarmConfigs , PoolParameterInfo parameter ) {
Optional < AlarmConfig > changeConfigOptional = alarmConfigs . stream ( )
. filter ( each - > Objects . equals ( each . getType ( ) , getType ( ) ) )
. findFirst ( ) ;
changeConfigOptional . ifPresent ( each - > dingChangeSendMessage ( each , parameter ) ) ;
}
private void dingAlarmSendMessage ( AlarmConfig alarmConfig , CustomThreadPoolExecutor pool ) {
BlockingQueue < Runnable > queue = pool . getQueue ( ) ;
String text = String . format (
"<font color='#FF0000'>[警报] </font>%s - 动态线程池运行告警 \n\n" +
" --- \n\n " +
"<font color='#708090' size=2>线程池ID: %s</font> \n\n " +
"<font color='#778899' size=2>应用实例:%s</font> \n\n " +
"<font color='#708090' size=2>线程池名称:%s</font> \n\n " +
" --- \n\n " +
"<font color='#708090' size=2>核心线程数:%d</font> \n\n " +
"<font color='#708090' size=2>最大线程数:%d</font> \n\n " +
@ -64,11 +76,12 @@ public class DingSendMessageHandler implements SendMessageHandler {
"<font color='#708090' size=2>线程池任务总量:%d</font> \n\n " +
" --- \n\n " +
"<font color='#708090' size=2>队列类型:%s</font> \n\n " +
"<font color='#708090' size=2>队列 总 容量:%d</font> \n\n " +
"<font color='#708090' size=2>队列 容量:%d</font> \n\n " +
"<font color='#708090' size=2>队列元素个数:%d</font> \n\n " +
"<font color='#708090' size=2>队列剩余个数:%d</font> \n\n " +
" --- \n\n " +
"<font color='#708090' size=2>拒绝策略次数:</font><font color='#FF0000' size=2>%d</font> \n\n " +
"<font color='#708090' size=2>拒绝策略:%s</font> \n\n" +
"<font color='#708090' size=2>拒绝策略执行次数:</font><font color='#FF0000' size=2>%d</font> \n\n " +
"<font color='#708090' size=2>OWNER: @%s</font> \n\n" +
"<font color='#708090' size=2>提示: 5 分钟内此线程池不会重复告警(可配置)</font> \n\n" +
" --- \n\n " +
@ -76,10 +89,10 @@ public class DingSendMessageHandler implements SendMessageHandler {
// 环境
active . toUpperCase ( ) ,
// 节点信息
instanceInfo . getIpApplicationName ( ) ,
// 线程池ID
pool . getThreadPoolId ( ) ,
// 节点信息
instanceInfo . getIpApplicationName ( ) ,
// 核心线程数
pool . getCorePoolSize ( ) ,
// 最大线程数
@ -100,6 +113,8 @@ public class DingSendMessageHandler implements SendMessageHandler {
queue . size ( ) ,
// 队列剩余个数
queue . remainingCapacity ( ) ,
// 拒绝策略名称
pool . getRejectedExecutionHandler ( ) . getClass ( ) . getSimpleName ( ) ,
// 拒绝策略次数
pool . getRejectCount ( ) ,
// 告警手机号
@ -109,16 +124,81 @@ public class DingSendMessageHandler implements SendMessageHandler {
) ;
execute ( alarmConfig , "动态线程池告警" , text , CollUtil . newArrayList ( "15601166691" ) ) ;
}
private void dingChangeSendMessage ( AlarmConfig alarmConfig , PoolParameterInfo parameter ) {
String threadPoolId = parameter . getTpId ( ) ;
DynamicThreadPoolWrap poolWrap = GlobalThreadPoolManage . getExecutorService ( threadPoolId ) ;
if ( poolWrap = = null ) {
log . warn ( "Thread pool is empty when sending change notification, threadPoolId :: {}" , threadPoolId ) ;
return ;
}
CustomThreadPoolExecutor customPool = poolWrap . getPool ( ) ;
/ * *
* hesitant e . g . ➲ ➜ ⇨ ➪
* /
String text = String . format (
"<font color='#2a9d8f'>[通知] </font>%s - 动态线程池参数变更 \n\n" +
" --- \n\n " +
"<font color='#708090' size=2>线程池ID: %s</font> \n\n " +
"<font color='#778899' size=2>应用实例:%s</font> \n\n " +
" --- \n\n " +
"<font color='#708090' size=2>核心线程数:%s</font> \n\n " +
"<font color='#708090' size=2>最大线程数:%s</font> \n\n " +
"<font color='#708090' size=2>线程存活时间:%s / SECONDS</font> \n\n" +
" --- \n\n " +
"<font color='#708090' size=2>队列类型:%s</font> \n\n " +
"<font color='#708090' size=2>队列容量:%s</font> \n\n " +
" --- \n\n " +
"<font color='#708090' size=2>AGO 拒绝策略:%s</font> \n\n" +
"<font color='#708090' size=2>NOW 拒绝策略:%s</font> \n\n" +
" --- \n\n " +
"<font color='#708090' size=2>提示:动态线程池配置变更实时通知(无限制)</font> \n\n" +
"<font color='#708090' size=2>OWNER: @%s</font> \n\n" +
" --- \n\n " +
"**播报时间:%s**" ,
// 环境
active . toUpperCase ( ) ,
// 线程池名称
threadPoolId ,
// 节点信息
instanceInfo . getIpApplicationName ( ) ,
// 核心线程数
customPool . getCorePoolSize ( ) + " ➲ " + parameter . getCoreSize ( ) ,
// 最大线程数
customPool . getMaximumPoolSize ( ) + " ➲ " + parameter . getMaxSize ( ) ,
// 线程存活时间
customPool . getKeepAliveTime ( TimeUnit . SECONDS ) + " ➲ " + parameter . getKeepAliveTime ( ) ,
// 阻塞队列
QueueTypeEnum . getBlockingQueueNameByType ( parameter . getQueueType ( ) ) ,
// 阻塞队列容量
( customPool . getQueue ( ) . size ( ) + customPool . getQueue ( ) . remainingCapacity ( ) ) + " ➲ " + parameter . getCapacity ( ) ,
// 拒绝策略
customPool . getRejectedExecutionHandler ( ) . getClass ( ) . getSimpleName ( ) ,
RejectedTypeEnum . getRejectedNameByType ( parameter . getRejectedType ( ) ) ,
// 告警手机号
"15601166691" ,
// 当前时间
DateUtil . now ( )
) ;
execute ( alarmConfig , "动态线程池通知" , text , CollUtil . newArrayList ( "15601166691" ) ) ;
}
private void execute ( AlarmConfig alarmConfig , String title , String text , List < String > mobiles ) {
String serverUrl = alarmConfig . getUrl ( ) + alarmConfig . getToken ( ) ;
DingTalkClient dingTalkClient = new DefaultDingTalkClient ( serverUrl ) ;
OapiRobotSendRequest request = new OapiRobotSendRequest ( ) ;
request . setMsgtype ( "markdown" ) ;
OapiRobotSendRequest . Markdown markdown = new OapiRobotSendRequest . Markdown ( ) ;
markdown . setTitle ( "动态线程池告警" ) ;
markdown . setTitle ( title ) ;
markdown . setText ( text ) ;
OapiRobotSendRequest . At at = new OapiRobotSendRequest . At ( ) ;
at . setAtMobiles ( CollUtil . newArrayList ( "15601166691" ) ) ;
at . setAtMobiles ( mobiles ) ;
request . setAt ( at ) ;
request . setMarkdown ( markdown ) ;