com.taotao.boot.monitor.collect.task.ElasticSearchCollectTask Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.boot.monitor.collect.task; // package com.taotao.cloud.health.collect;
import com.taotao.boot.common.utils.context.ContextUtils;
import com.taotao.boot.common.utils.reflect.ClassUtils;
import com.taotao.boot.common.utils.reflect.ReflectionUtils;
import com.taotao.boot.core.support.Collector;
import com.taotao.boot.monitor.annotation.FieldReport;
import com.taotao.boot.monitor.collect.AbstractCollectTask;
import com.taotao.boot.monitor.collect.CollectInfo;
import com.taotao.boot.monitor.properties.CollectTaskProperties;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.ScheduledThreadPoolExecutor;
public class ElasticSearchCollectTask extends AbstractCollectTask {
private static final String TASK_NAME = "ttc.monitor.collect.elasticsearch";
private final ThreadMXBean threadMXBean;
private final CollectTaskProperties properties;
private HashMap lastThreadUserTime = new HashMap<>();
private final boolean classExist;
public ElasticSearchCollectTask(CollectTaskProperties properties) {
threadMXBean = ManagementFactory.getThreadMXBean();
this.properties = properties;
this.classExist = ClassUtils.isExist("com.taotao.boot.monitor.elasticsearch.impl.ElasticSearchProvider");
}
@Override
public int getTimeSpan() {
return properties.getElasticSearchTimeSpan();
}
@Override
public boolean getEnabled() {
return properties.isElasticSearchEnabled() && classExist;
}
@Override
public String getDesc() {
return this.getClass().getName();
}
@Override
public String getName() {
return TASK_NAME;
}
@Override
protected CollectInfo getData() {
ElasticSearchData data = new ElasticSearchData();
if (ContextUtils.getBean(
ReflectionUtils.tryClassForName(
"com.taotao.boot.monitor.elasticsearch.impl.ElasticSearchProvider"),
false)
!= null) {
Collector.Hook hook = (Collector.Hook) ReflectionUtils.callMethod(
ReflectionUtils.tryClassForName("com.taotao.boot.monitor.elasticsearch.impl.ElasticSearchMonitor"),
"hook",
null);
if (hook != null) {
data.hookCurrent = hook.getCurrent();
data.hookError = hook.getLastErrorPerSecond();
data.hookSuccess = hook.getLastSuccessPerSecond();
data.hookList = hook.getMaxTimeSpanList().toText();
data.hookListPerMinute = hook.getMaxTimeSpanListPerMinute().toText();
}
Object transportClient = ContextUtils.getBean(
ReflectionUtils.tryClassForName("org.elasticsearch.client.transport.TransportClient"), false);
if (transportClient != null) {
data.threadsTotal = 0;
data.activateTotal = 0;
data.queueSize = 0;
// 线程池
Iterator