Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-8244

Allow Customization of Object Name Attributes

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.0.5
    • Fix Version/s: 3.2 RC1
    • Component/s: None
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      MBeanExporters in Spring Integration and Spring Batch provide an attribute to specify additional components of the object name. It would be nice if core could do the same. When using @ManagedResource, one has to use a static name; this causes issues if multiple instances are deployed in the same JVM.

      Currently using this as a work-around

      public class EnhancedMetadataNamingStrategy extends MetadataNamingStrategy {
      
      	private String staticNameParts = "";
      	
      	public EnhancedMetadataNamingStrategy(
      			AnnotationJmxAttributeSource annotationSource) {
      		super(annotationSource);
      	}
      
      	@Override
      	public ObjectName getObjectName(Object managedBean, String beanKey)
      			throws MalformedObjectNameException {
      		ObjectName objectName = super.getObjectName(managedBean, beanKey);
      		if (this.staticNameParts.length() == 0) {
      			return objectName;
      		}
      		return new ObjectName(objectName.getCanonicalName() + this.staticNameParts);
      	}
      
      	/**
      	 * Static properties that will be added to all object names.
      	 * 
      	 * @param objectNameStaticProperties the objectNameStaticProperties to set
      	 */
      	public void setObjectNameStaticProperties(Map<String, String> objectNameStaticProperties) {
      		StringBuilder builder = new StringBuilder();
      		for (String key : objectNameStaticProperties.keySet()) {
      			builder.append("," + key + "=" + objectNameStaticProperties.get(key));
      		}
      		this.staticNameParts = builder.toString();
      	}
      
      }
      
      <bean class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
      	<property name="defaultDomain" value="spring.application"/>
      	<property name="namingStrategy">
      		<bean class="org.springframework.integration.cluster.health.EnhancedMetadataNamingStrategy">
      			<constructor-arg>
      				<bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
      			</constructor-arg>
      			<property name="objectNameStaticProperties">
      				<map>
      					<entry key="application" value="${application.id}"/>
      				</map>
      			</property>
      		</bean>
      	</property>
      </bean>
      

      See also INT-1817 (SI did not expose the map via the namespace).

        Attachments

          Activity

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              grussell Gary Russell
              Last updater:
              Chris Beams
            • Votes:
              0 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                5 years, 47 weeks, 6 days ago