Имя для @ManagedOperation в Spring JMX - PullRequest
       56

Имя для @ManagedOperation в Spring JMX

3 голосов
/ 18 января 2012

Я использовал org.springframework.jmx.export.annotation.@ManagedOperation для представления метода как MBean.

Я хочу, чтобы имя операции отличалось от имени метода, но управляемая операция не имеет никакого атрибута для него.

Например:

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

, и я хочу, чтобы эта операция была открыта с именем = "ResetCache".

Ответы [ 2 ]

10 голосов
/ 18 января 2012

Я бы просто определил другой метод, который просто делегирует clearCache(). Мы делаем это все время, когда имя интерфейса сбивает с толку. description = "resets the cache" внутри @ManagedOperation также может быть хорошей идеей.

@ManagedOperation(description = "resets the cache")
public void resetCache() {
   clearCache();
}
5 голосов
/ 18 января 2012

Создайте пользовательскую аннотацию:

@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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...