4

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

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

例如:

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

我希望这个操作以 name = "ResetCache" 公开。

4

2 回答 2

10

我只想定义另一个仅委托给clearCache(). 当接口名称令人困惑时,我们总是这样做。description = "resets the cache"内部@ManagedOperation也可能是一个好主意。

@ManagedOperation(description = "resets the cache")
public void resetCache() {
   clearCache();
}
于 2012-01-18T13:55:31.600 回答
5

创建自定义注释:

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

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

}

如果你连接 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>
于 2012-01-18T12:49:33.807 回答