Name for @ManagedOperation in Spring JMX
Asked Answered
D

2

5

I used org.springframework.jmx.export.annotation.@ManagedOperation to expose a method as MBean.

I want the operation name different from the method name, but managed operation doesn't have any attribute for it.

For example:

@ManagedOperation
public synchronized void clearCache() 
{
   // do something
}

and I want this operation exposed with name = "ResetCache".

Drunkard answered 18/1, 2012 at 12:25 Comment(0)
H
5

Create a custom annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JmxName {
    String value();
}

And a custom subclass of MetadataMBeanInfoAssembler:

public class CustomMetadataMBeanInfoAssembler extends MetadataMBeanInfoAssembler {

    private String getName(final Method method) {
        final JmxName annotation = method.getAnnotation(JmxName.class);
        if (annotation != null) {
            return annotation.value();
        }else
            return method.getName();
        }
    }
    protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
            return new ModelMBeanOperationInfo(getName(method),
                getOperationDescription(method, beanKey),
                getOperationParameters(method, beanKey),
                method.getReturnType().getName(),
                MBeanOperationInfo.UNKNOWN);
    }

}

and you should get it to work if you wire the CustomMetadataMBeanInfoAssembler (and use the annotation):

<bean id="jmxAttributeSource"
      class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

<!-- will create management interface using annotation metadata -->
<bean id="assembler"
      class="com.yourcompany.some.path.CustomMetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="jmxAttributeSource"/>
</bean>
Homochromous answered 18/1, 2012 at 12:49 Comment(2)
thanks for your solution, but this solution cann't help me because it changes description of method operation but i want changes name of operation.Drunkard
@MJM look at the history and you'll see the changeHomochromous
P
10

I would just define another method that just delegates to clearCache(). We do this all of the time when the interface name is confusing. A description = "resets the cache" inside of the @ManagedOperation might also be a good idea.

@ManagedOperation(description = "resets the cache")
public void resetCache() {
   clearCache();
}
Paschall answered 18/1, 2012 at 13:55 Comment(0)
H
5

Create a custom annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JmxName {
    String value();
}

And a custom subclass of MetadataMBeanInfoAssembler:

public class CustomMetadataMBeanInfoAssembler extends MetadataMBeanInfoAssembler {

    private String getName(final Method method) {
        final JmxName annotation = method.getAnnotation(JmxName.class);
        if (annotation != null) {
            return annotation.value();
        }else
            return method.getName();
        }
    }
    protected ModelMBeanOperationInfo createModelMBeanOperationInfo(Method method, String name, String beanKey) {
            return new ModelMBeanOperationInfo(getName(method),
                getOperationDescription(method, beanKey),
                getOperationParameters(method, beanKey),
                method.getReturnType().getName(),
                MBeanOperationInfo.UNKNOWN);
    }

}

and you should get it to work if you wire the CustomMetadataMBeanInfoAssembler (and use the annotation):

<bean id="jmxAttributeSource"
      class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

<!-- will create management interface using annotation metadata -->
<bean id="assembler"
      class="com.yourcompany.some.path.CustomMetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="jmxAttributeSource"/>
</bean>
Homochromous answered 18/1, 2012 at 12:49 Comment(2)
thanks for your solution, but this solution cann't help me because it changes description of method operation but i want changes name of operation.Drunkard
@MJM look at the history and you'll see the changeHomochromous

© 2022 - 2024 — McMap. All rights reserved.