Name for @ManagedOperation in Spring JMX

别等时光非礼了梦想. 提交于 2019-12-04 14:18:41

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>

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!