我使用org.springframework.jmx.export.annotation.@ ManagedOperation将方法公开为MBean.
我希望操作名称与方法名称不同,但托管操作没有任何属性.
例如:
@ManagedOperation
public synchronized void clearCache()
{
// do something
}
我希望使用name =“ResetCache”公开此操作.
解决方法:
创建自定义注释:
@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>