我正在尝试使用jmx-exported方法运行一个简单的应用程序.我喜欢(spring-context和"@Configuration"的cglib在classpath中):
package com.sopovs.moradanen.jmx;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.stereotype.Component;
@Component
@Configuration
public class SpringJmxTest {
public static void main(String[] args) {
new AnnotationConfigApplicationContext("com.sopovs.moradanen.jmx");
while (true) {
Thread.yield();
}
}
@Bean
public MBeanExporter createJmxExporter() {
return new MBeanExporter();
}
public interface FooBarMBean {
public String hello();
}
@Component
public static class FooBar implements FooBarMBean {
@Override
public String hello() {
return "Hello";
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,我得到:javax.management.MalformedObjectNameException:键属性不能为空.我尝试调试并解决它:
@Component
public static class FooBar implements FooBarMBean, SelfNaming {
@Override
public String hello() {
return "Hello";
}
@Override
public ObjectName getObjectName() throws MalformedObjectNameException {
return new ObjectName("fooBar:name=" + getClass().getName());
}
}
Run Code Online (Sandbox Code Playgroud)
但是有更好的方法为MBean提供名称吗?
Don*_*llo 14
您可以使用Spring Context @ Managed*提供的描述注释:
为此,您不得使用"MBean"或"MXBean"后缀实现接口,也不得使用SelfNaming实现.然后,当MBeanExporter将registerBeanInstance(..)时,bean将被检测为标准spring"托管bean",并将使用所有spring注释转换为ModelMBean,包括属性,操作,参数等的描述.
作为一项要求,您应该在Spring上下文中声明带有AnnotationJmxAttributeSource,MetadataNamingStrategy和MetadataMBeanInfoAssembler属性的MBeanExporter,它们可以简化为:
<bean id="mbeanExporter"
class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter" />
Run Code Online (Sandbox Code Playgroud)
要么
<context:mbean-export />
Run Code Online (Sandbox Code Playgroud)
您的托管bean应如下所示:
@Component("myManagedBean")
@ManagedResource(objectName="your.domain.jmx:name=MyMBean",
description="My MBean goal")
public class AnnotationTestBean {
private int age;
@ManagedAttribute(description="The age attribute", currencyTimeLimit=15)
public int getAge() {
return age;
}
@ManagedOperation(description = "Check permissions for the given activity")
@ManagedOperationParameters( {
@ManagedOperationParameter(name = "activity",
description = "The activity to check")
})
public boolean isAllowedTo(final String activity) {
// impl
}
}
Run Code Online (Sandbox Code Playgroud)
记住不要实现一个MBean接口,它将是一个StandardMBean,以及SelfNaming接口,它将绕过Spring命名管理!
| 归档时间: |
|
| 查看次数: |
21513 次 |
| 最近记录: |