AnnotationMBeanExporter源码分析

/**
 *通过注解方式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;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈脩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值