Skip to content

Allow Customization of Object Name Attributes [SPR-8244] #12892

Closed

Description

@spring-projects-issues

Gary Russell opened SPR-8244 and commented

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


Affects: 3.0.5

Sub-tasks:

Referenced from: commits 0e02007

0 votes, 10 watchers

Activity

  1. spring-projects-issues commented on Jul 18, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Gary Russell commented

    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>
    
  2. spring-projects-issues commented on Oct 19, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Gary Russell commented

    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.

  3. spring-projects-issues commented on Oct 19, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Gary Russell commented

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

  4. spring-projects-issues commented on Oct 30, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions