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).

        Activity

        Hide
        grussell Gary Russell added a comment - - edited

        Correction to config...

         
        	<bean
        		class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
        		<property name="namingStrategy">
        			<bean class="org.springframework.foo.jmx.EnhancedMetadataNamingStrategy">
        				<constructor-arg>
        					<bean
        						class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
        				</constructor-arg>
        				<property name="defaultDomain" value="spring.application" />
        				<property name="objectNameStaticProperties">
        					<map>
        						<entry key="application" value="${application.configuration.name}" />
        					</map>
        				</property>
        			</bean>
        		</property>
        	</bean>

        Show
        grussell Gary Russell added a comment - - edited Correction to config...   <bean class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"> <property name="namingStrategy"> <bean class="org.springframework.foo.jmx.EnhancedMetadataNamingStrategy"> <constructor-arg> <bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" /> </constructor-arg> <property name="defaultDomain" value="spring.application" /> <property name="objectNameStaticProperties"> <map> <entry key="application" value="${application.configuration.name}" /> </map> </property> </bean> </property> </bean>
        Hide
        grussell Gary Russell added a comment -

        As discussed today, another possibility - perhaps even better - would be to allow a SpEL expression in the @ ManagedResource objectName attribute so the object name can be easily externalized.

        Show
        grussell Gary Russell added a comment - As discussed today, another possibility - perhaps even better - would be to allow a SpEL expression in the @ ManagedResource objectName attribute so the object name can be easily externalized.
        Hide
        grussell Gary Russell added a comment -

        The use of SpEL could perhaps also be made available in other selected Annotations (e.g. @RequestMapping).

        Show
        grussell Gary Russell added a comment - The use of SpEL could perhaps also be made available in other selected Annotations (e.g. @RequestMapping).
        Hide
        juergen.hoeller Juergen Hoeller added a comment -

        I've finally addressed this for 3.2 RC1, through support for regular Spring placeholders (as parsed by PropertyPlaceholderConfigurer) in all applicable ManagedResource annotation attributes.

        Juergen

        Show
        juergen.hoeller Juergen Hoeller added a comment - I've finally addressed this for 3.2 RC1, through support for regular Spring placeholders (as parsed by PropertyPlaceholderConfigurer) in all applicable ManagedResource annotation attributes. Juergen

          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, 4 weeks, 3 days ago