AnnotationMBeanExporter源码分析

本文介绍如何使用Spring框架中的JMX功能通过注解方式自动导出管理资源及属性。具体包括如何读取注解并转换为ManagedResource、ManagedAttribute等元数据。

/**
 *通过注解方式MBean导出器,继承MBeanExporter
 */
public class AnnotationMBeanExporter extends MBeanExporter {
	
	
	private final AnnotationJmxAttributeSource annotationSource =
			new AnnotationJmxAttributeSource();

	private final MetadataNamingStrategy metadataNamingStrategy =
			new MetadataNamingStrategy(this.annotationSource);

	private final MetadataMBeanInfoAssembler metadataAssembler =
			new MetadataMBeanInfoAssembler(this.annotationSource);


	public AnnotationMBeanExporter() {
		// 设置Naming策略
		setNamingStrategy(this.metadataNamingStrategy);
		//设置metadata组装器
		setAssembler(this.metadataAssembler);
		// 自动检测所有
		setAutodetectMode(AUTODETECT_ALL);
	}
}

/**
 * JmxAttributeSource接口的实现类,它读取注解同时导出相应的属性
 *
 */
public class AnnotationJmxAttributeSource implements JmxAttributeSource {

// 获取管理资源
	public ManagedResource getManagedResource(Class<?> beanClass) throws InvalidMetadataException {
		// 获取beanClass上的ManagedResource注解
		org.springframework.jmx.export.annotation.ManagedResource ann =
				beanClass.getAnnotation(org.springframework.jmx.export.annotation.ManagedResource.class);
		if (ann == null) {
			return null;
		}
		//创建一个ManagedResource
		ManagedResource managedResource = new ManagedResource();
		// 复制属性
		AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource);
		// 如果注解的value()值不为空且ManangedResource的objectName属性长度为空,则将ObjectName设置为value
		if (!"".equals(ann.value()) && !StringUtils.hasLength(managedResource.getObjectName())) {
			managedResource.setObjectName(ann.value());
		}
		// 返回 managedResource
		return managedResource;
	}

	// 获取管理属性
	public ManagedAttribute getManagedAttribute(Method method) throws InvalidMetadataException {
	// 获取当前方法上的ManagedAttribute注解
		org.springframework.jmx.export.annotation.ManagedAttribute ann =
				AnnotationUtils.findAnnotation(method, org.springframework.jmx.export.annotation.ManagedAttribute.class);
		if (ann == null) {
			return null;
		}
		ManagedAttribute managedAttribute = new ManagedAttribute();
		AnnotationBeanUtils.copyPropertiesToBean(ann, managedAttribute, "defaultValue");
		// 如果ManagedAttribute注解的默认值不为空则设置默认值
		if (ann.defaultValue().length() > 0) {
			managedAttribute.setDefaultValue(ann.defaultValue());
		}
		return managedAttribute;
	}
	
	// 获取ManagedMetric
	public ManagedMetric getManagedMetric(Method method) throws InvalidMetadataException {
		org.springframework.jmx.export.annotation.ManagedMetric ann =
				AnnotationUtils.findAnnotation(method, org.springframework.jmx.export.annotation.ManagedMetric.class);
		if (ann == null) {
			return null;
		}
		ManagedMetric managedMetric = new ManagedMetric();
		AnnotationBeanUtils.copyPropertiesToBean(ann, managedMetric);
		return managedMetric;
	}
	
	// 获取管理操作
	public ManagedOperation getManagedOperation(Method method) throws InvalidMetadataException {
		Annotation ann = AnnotationUtils.findAnnotation(method, org.springframework.jmx.export.annotation.ManagedOperation.class);
		if (ann == null) {
			return null;
		}
		ManagedOperation op = new ManagedOperation();
		AnnotationBeanUtils.copyPropertiesToBean(ann, op);
		return op;
	}
	
	// 获取管理操作的参数
	public ManagedOperationParameter[] getManagedOperationParameters(Method method)
			throws InvalidMetadataException {

		ManagedOperationParameters params = AnnotationUtils.findAnnotation(method, ManagedOperationParameters.class);
		ManagedOperationParameter[] result = null;
		if (params == null) {
			result = new ManagedOperationParameter[0];
		}
		else {
			Annotation[] paramData = params.value();
			result = new ManagedOperationParameter[paramData.length];
			for (int i = 0; i < paramData.length; i++) {
				Annotation annotation = paramData[i];
				ManagedOperationParameter managedOperationParameter = new ManagedOperationParameter();
				AnnotationBeanUtils.copyPropertiesToBean(annotation, managedOperationParameter);
				result[i] = managedOperationParameter;
			}
		}
		return result;
	}
	
	// 获取管理通知
	public ManagedNotification[] getManagedNotifications(Class<?> clazz) throws InvalidMetadataException {
	// 获取当前类的ManangedNotifications注解
		ManagedNotifications notificationsAnn = clazz.getAnnotation(ManagedNotifications.class);
		if(notificationsAnn == null) {
			return new ManagedNotification[0];
		}
		// 获取当前注解的值
		Annotation[] notifications = notificationsAnn.value();
		ManagedNotification[] result = new ManagedNotification[notifications.length];
		// 遍历注解
		for (int i = 0; i < notifications.length; i++) {
			Annotation notification = notifications[i];
			ManagedNotification managedNotification = new ManagedNotification();
			AnnotationBeanUtils.copyPropertiesToBean(notification, managedNotification);
			result[i] = managedNotification;
		}
		return result;
	}

}

[traceId:] 2025-08-07T15:27:59.983+08:00 WARN 571092 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop' [traceId:] 2025-08-07T15:27:59.989+08:00 INFO 571092 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Could not unregister MBean [com.github.tobato.fastdfs.domain.conn:name=fdfsConnectionPool,type=FdfsConnectionPool] as said MBean is not registered (perhaps already unregistered by an external process) [traceId:] 2025-08-07T15:27:59.993+08:00 DEBUG 571092 --- [ main] c.y.s.c.t.TraceThreadPoolTaskExceutor : Shutting down ExecutorService 'syncExecutor' [traceId:] 2025-08-07T15:28:00.001+08:00 DEBUG 571092 --- [ main] c.y.s.c.t.TraceThreadPoolTaskExceutor : Shutting down ExecutorService 'taskExecutor' [traceId:] 2025-08-07T15:28:00.040+08:00 INFO 571092 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] [traceId:] 2025-08-07T15:28:00.047+08:00 WARN 571092 --- [ main] o.a.c.loader.WebappClassLoaderBase : The web application [ROOT] appears to have started a thread named [Thread-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.base@17.0.15/sun.net.dns.ResolverConfigurationImpl.notifyAddrChange0(Native Method) java.base@17.0.15/sun.net.dns.ResolverConfigurationImpl$AddressChangeListener.run(ResolverConfigurationImpl.java:176) [traceId:] 2025-08-07T15:28:00.069+08:00 INFO 571092 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. [traceId:] 2025-08-07T15:28:00.103+08:00 ERROR 571092 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈脩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值