zoukankan      html  css  js  c++  java
  • 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;
    	}
    
    }
    
  • 相关阅读:
    springCloud学习6(Spring Cloud Sleuth 分布式跟踪)
    springCloud学习5(Spring-Cloud-Stream事件驱动)
    springCloud学习4(Zuul服务路由)
    springCloud学习3(Netflix Hystrix弹性客户端)
    springCloud学习笔记2(服务发现)
    EFCore
    webBrowser.DocumentText重新赋值无效解决方法
    C# SQLite写入和读取DateTime类型
    自定义协议链接 ------2.实践篇
    自定义协议链接 ------1.原理篇
  • 原文地址:https://www.cnblogs.com/wei-zw/p/8797764.html
Copyright © 2011-2022 走看看