All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.monitor.collect.task.AsyncThreadPoolCollectTask 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;

import com.taotao.boot.common.utils.context.ContextUtils;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.core.properties.AsyncProperties;
import com.taotao.boot.core.support.Collector;
import com.taotao.boot.core.support.Collector.Hook;
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 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.Objects;

/**
 * MonitorThreadPoolCollectTask
 *
 * @author shuigedeng
 * @version 2021.9
 * @since 2021-09-10 19:14:28
 */
public class AsyncThreadPoolCollectTask extends AbstractCollectTask {

	private static final String TASK_NAME = "ttc.monitor.collect.executor.async";

	private final CollectTaskProperties properties;
	private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

	public AsyncThreadPoolCollectTask(CollectTaskProperties properties,
                                      ThreadPoolTaskExecutor threadPoolTaskExecutor) {
		this.properties = properties;
		this.threadPoolTaskExecutor = threadPoolTaskExecutor;
	}

	@Override
	public int getTimeSpan() {
		return properties.getAsyncThreadTimeSpan();
	}

	@Override
	public String getDesc() {
		return getClass().getName();
	}

	@Override
	public String getName() {
		return TASK_NAME;
	}

	@Override
	public boolean getEnabled() {
		return properties.getAsyncThreadEnabled();
	}

	@Override
	protected CollectInfo getData() {
		try {
			Collector collector = Collector.getCollector();
			AsyncProperties asyncProperties = ContextUtils.getBean(AsyncProperties.class, true);

			if (Objects.nonNull(collector) && Objects.nonNull(asyncProperties)) {

				String asyncThreadName = asyncProperties.getThreadNamePrefix().replace("-", ".");

				AsyncExecutorCollectInfo info = new AsyncExecutorCollectInfo();
				info.asyncExecutorActiveCount = (Integer)
					collector
						.call(asyncThreadName + ".active.count")
						.set(threadPoolTaskExecutor::getActiveCount)
						.run();
				info.asyncExecutorCorePoolSize = (Integer)
					collector
						.call(asyncThreadName + ".core.pool.size")
						.set(threadPoolTaskExecutor::getCorePoolSize)
						.run();
				info.asyncExecutorPoolSizeLargest = (Integer)
					collector
						.call(asyncThreadName + ".pool.size.largest")
						.set(threadPoolTaskExecutor::getPoolSize)
						.run();
				info.asyncExecutorPoolSizeMax = (Integer)
					collector
						.call(asyncThreadName + ".pool.size.max")
						.set(threadPoolTaskExecutor::getMaxPoolSize)
						.run();
				info.asyncExecutorPoolSizeCount = (Integer)
					collector
						.call(asyncThreadName + ".pool.size.count")
						.set(threadPoolTaskExecutor::getCorePoolSize)
						.run();
				info.asyncExecutorQueueSize = (Integer)
					collector
						.call(asyncThreadName + ".queue.size")
						.set(threadPoolTaskExecutor::getQueueSize)
						.run();
				info.asyncExecutorTaskCount = Long.valueOf(String.valueOf(collector
					.call(asyncThreadName + ".task.count")
					.set(threadPoolTaskExecutor::getActiveCount)
					.run()));
				info.asyncExecutorTaskCompleted = Long.valueOf(String.valueOf(collector
					.call(asyncThreadName + ".task.completed")
					.set(threadPoolTaskExecutor::getActiveCount)
					.run()));

				Hook hook = collector.hook(asyncThreadName + ".hook");
				info.asyncExecutorTaskHookCurrent = hook.getCurrent();
				info.asyncExecutorTaskHookError = hook.getLastErrorPerSecond();
				info.asyncExecutorTaskHookSuccess = hook.getLastSuccessPerSecond();
				info.asyncExecutorTaskHookList = hook.getMaxTimeSpanList().toText();
				info.asyncExecutorTaskHookListPerMinute =
					hook.getMaxTimeSpanListPerMinute().toText();
				return info;
			}
		}
		catch (Exception e) {
			if (LogUtils.isErrorEnabled()) {
				LogUtils.error(e);
			}
		}
		return null;
	}

	private static class AsyncExecutorCollectInfo implements CollectInfo {

		@FieldReport(name = TASK_NAME + ".active.count", desc = "异步核心线程池活动线程数")
		private Integer asyncExecutorActiveCount = 0;

		@FieldReport(name = TASK_NAME + ".core.pool.size", desc = "异步核心线程池核心线程数")
		private Integer asyncExecutorCorePoolSize = 0;

		@FieldReport(name = TASK_NAME + ".pool.size.largest", desc = "异步核心线程池历史最大线程数")
		private Integer asyncExecutorPoolSizeLargest = 0;

		@FieldReport(name = TASK_NAME + ".pool.size.max", desc = "异步核心线程池最大线程数")
		private Integer asyncExecutorPoolSizeMax = 0;

		@FieldReport(name = TASK_NAME + ".pool.size.count", desc = "异步核心线程池当前线程数")
		private Integer asyncExecutorPoolSizeCount = 0;

		@FieldReport(name = TASK_NAME + ".queue.size", desc = "异步核心线程池当前排队等待任务数")
		private Integer asyncExecutorQueueSize = 0;

		@FieldReport(name = TASK_NAME + ".task.count", desc = "异步核心线程池历史任务数")
		private Long asyncExecutorTaskCount = 0L;

		@FieldReport(name = TASK_NAME + ".task.completed", desc = "异步核心线程池已完成任务数")
		private Long asyncExecutorTaskCompleted = 0L;

		@FieldReport(name = TASK_NAME
			+ ".task.hook.error", desc = "异步核心线程池拦截上一次每秒出错次数")
		private Long asyncExecutorTaskHookError = 0L;

		@FieldReport(name = TASK_NAME
			+ ".task.hook.success", desc = "异步核心线程池拦截上一次每秒成功次数")
		private Long asyncExecutorTaskHookSuccess = 0L;

		@FieldReport(name = TASK_NAME
			+ ".task.hook.current", desc = "异步核心线程池拦截当前执行任务数")
		private Long asyncExecutorTaskHookCurrent = 0L;

		@FieldReport(name = TASK_NAME
			+ ".task.hook.list.detail", desc = "异步核心线程池拦截历史最大耗时任务列表")
		private String asyncExecutorTaskHookList = "";

		@FieldReport(name = TASK_NAME
			+ ".task.hook.list.minute.detail", desc = "异步核心线程池拦截历史最大耗时任务列表(每分钟)")
		private String asyncExecutorTaskHookListPerMinute = "";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy