Spring JMX中@ManagedOperation的名称

Sam*_*Sam 3 java spring jmx mbeans spring-jmx

我曾经org.springframework.jmx.export.annotation.@ManagedOperation把一个方法公开为MBean.

我希望操作名称与方法名称不同,但托管操作没有任何属性.

例如:

@ManagedOperation
public synchronized void clearCache() 
{
   // do something
}
Run Code Online (Sandbox Code Playgroud)

我希望使用name ="ResetCache"公开此操作.

Gra*_*ray 10

我只想定义另一个委托的方法clearCache().当接口名称令人困惑时,我们会一直这样做.一个description = "resets the cache"在里面@ManagedOperation也可能是一个好主意.

@ManagedOperation(description = "resets the cache")
public void resetCache() {
   clearCache();
}
Run Code Online (Sandbox Code Playgroud)


Sea*_*oyd 5

创建自定义注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JmxName {
    String value();
}
Run Code Online (Sandbox Code Playgroud)

以及一个自定义子类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);
    }

}
Run Code Online (Sandbox Code Playgroud)

如果你连接CustomMetadataMBeanInfoAssembler(并使用注释),你应该让它工作:

<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>
Run Code Online (Sandbox Code Playgroud)