2012-01-18 6 views
3

[email protected]を使用して、メソッドをMBeanとして公開しました。Springの@ManagedOperationの名前JMX

操作名はメソッド名とは異なりますが、管理操作には属性がありません。例えば

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

と私は、この操作は、名前= "ResetCache" と暴露します。

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を配線すると、それは仕事を得る必要があります:

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

そしてMetadataMBeanInfoAssemblerのカスタムサブクラス:

答えて

5

カスタムアノテーションを作成

<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> 
+0

あなたのソリューションはありがたいですが、このソリューションではメソッド操作の説明が変更されていますが、操作の名前を変更する必要があります。 – MJM

+0

@MJM ok、この更新されたソリューションを代わりに試してください –

+0

"更新されたソリューション"が見つかりません。 – MJM

9

私はちょうどde legates to clearCache()。インタフェース名が混乱しているときは、これをすべて行います。 @ManagedOperationの中のdescription = "resets the cache"も良い考えです。

@ManagedOperation(description = "resets the cache") 
public void resetCache() { 
    clearCache(); 
} 
+4

+1シンプルで効果的 – skaffman

関連する問題